Best Python code snippet using freezegun
test_decorators.py
Source:test_decorators.py
...7 ((int, float), [-10, 10]),8 (int, [0, 10]),9 ("s", "s")10 )11 def to_decorate(a, str1, b=2.2, c=4, str2="blobfish"):12 val = round(a*b,0) % c13 val = int(val)14 return str(val) + "-" + str1 + "-" + str215 assert to_decorate(7, "pferd") == '3-pferd-blobfish'16 with self.assertRaises(AssertionError) as ctx:17 to_decorate(7, 8)18 self.assertEqual("Type of parameter '8' does not match <class 'str'>",19 str(ctx.exception))20 assert to_decorate(4, "gorilla", c=6) == '3-gorilla-blobfish'21 with self.assertRaises(AssertionError) as ctx:22 to_decorate(4, "gorilla", c=6.6)23 self.assertEqual("Type of parameter c does not match <class 'int'>",24 str(ctx.exception))25 with self.assertRaises(AssertionError) as ctx:26 to_decorate(4, "gorilla", c=12)27 self.assertEqual("Parameter c=12 must be inside bounds [0, 10]",28 str(ctx.exception))29 with self.assertRaises(TypeError) as ctx:30 to_decorate(4, "gorilla", str2=44)31 self.assertEqual("""can only concatenate str (not "int") to str""",32 str(ctx.exception))33 def test_decorator_accepts(self):34 @accepts("s",35 str,36 (int, float),37 int,38 "s"39 )40 def to_decorate(a, str1, b=2.2, c=4, str2="blobfish"):41 val = round(a*b,0) % c42 val = int(val)43 return str(val) +"-" + str1 + "-" + str244 assert to_decorate(a=True, str1="belly") == '2-belly-blobfish'45 with self.assertRaises(TypeError) as ctx:46 to_decorate(True, "belly", str2=44)47 self.assertEqual("""can only concatenate str (not "int") to str""",48 str(ctx.exception))49 with self.assertRaises(AssertionError) as ctx:50 to_decorate(True, "belly", c=4.44)51 self.assertEqual("Type of parameter c does not match <class 'int'>",52 str(ctx.exception))53 with self.assertRaises(AssertionError) as ctx:54 to_decorate(4, "belly", b=[4, 6])55 msg = "Type of parameter b does not match (<class 'int'>, "\56 "<class 'float'>)"57 self.assertEqual(msg, str(ctx.exception))58 def test_decorator_bounds(self):59 @arg_bounds("s",60 [-10, 10],61 [0, 10],62 )63 def to_decorate(a, b=2.2, c=4):64 return round(a * b, 0) % c65 assert to_decorate(4444, 5, 5) == 066 assert round(to_decorate(12, c=5.4), 1) == 4.467 with self.assertRaises(AssertionError) as ctx:68 to_decorate(4444, 10.1, 5)69 self.assertEqual("Parameter '10.1' must be inside bounds [-10, 10]",70 str(ctx.exception))71 with self.assertRaises(AssertionError) as ctx:72 to_decorate(4444, b=10.1, c=5)73 self.assertEqual("Parameter b=10.1 must be inside bounds [-10, 10]",74 str(ctx.exception))75if __name__ == '__main__':...
decorators.py
Source:decorators.py
2def decorator(f):3 print('Decorated!')4 return f5@decorator6def to_decorate():7 print('To Decorate')8 return 19to_decorate()10to_decorate = decorator(to_decorate)11# Decorator with Wrapper12def decorator(f):13 def wrapper():14 print('Decorated!')15 f()16 return wrapper17@decorator18def to_decorate():19 print('To Decorate')20 return 121to_decorate()22to_decorate = decorator(to_decorate)23# Decorator that can handle function parameters24def decorator(f):25 def wrapper(*args, **kwargs):26 print('Decorated!')27 f(*args, **kwargs)28 return wrapper29@decorator_with_wrapper30def to_decorate(x, y, **kwargs):31 print('To Decorate')32 print(x)33 print(y)34 print(kwargs)35 return 136to_decorate('searingfrost', 'another parameter', **{'key': 'value'})37to_decorate = decorator(to_decorate)38# Decorator factory to handle decorator parameters39def decorator_factory(*factory_args, **factory_kwargs):40 def decorator(f):41 def wrapper(*args, **kwargs):42 print(f'Args Passed to factory: {factory_args}')43 print(f'KeywordArgs passed to factory: {factory_kwargs}')44 print('Decorated!')45 f(*args, **kwargs)46 return wrapper47 return decorator_with_wrapper48@decorator_factory('someargument', a=5, b=10)49def to_decorate(x, y, **kwargs):50 print('To Decorate')51 print(x)52 print(y)53 print(kwargs)54to_decorate('searingfrost', 'another parameter', **{'key': 'value'})...
profile.py
Source:profile.py
...15 def wrapper(*args, **kwargs):16 name = match.group('name')17 print(f'\'{name}\' started')18 start_time = time.time()19 result = to_decorate(*args, **kwargs)20 time_spend = start_time - time.time()21 print(f'\'{name}\' finished in {time_spend}s')22 return result23 return wrapper24 elif inspect.isclass(to_decorate):25 for attribute in to_decorate.__dict__:26 if inspect.isfunction(getattr(to_decorate, attribute)):27 setattr(to_decorate, attribute, profile(getattr(to_decorate, attribute)))28 return to_decorate29 else:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!