Best Python code snippet using localstack_python
test_patch.py
Source:test_patch.py
...4class MyEchoer:5 def do_echo(self, arg):6 return f"do_echo: {arg}"7 @classmethod8 def do_class_echo(cls, arg):9 return f"do_class_echo: {arg}"10 @staticmethod11 def do_static_echo(arg):12 return f"do_static_echo: {arg}"13def test_patch_context_manager():14 assert echo("foo") == "echo: foo"15 def monkey(arg):16 return f"monkey: {arg}"17 with Patch(get_defining_object(echo), "echo", monkey):18 assert echo("foo") == "monkey: foo"19 assert echo("foo") == "echo: foo"20def test_patch_with_pass_target_context_manager():21 assert echo("foo") == "echo: foo"22 def uppercase(target, arg):23 return target(arg).upper()24 with Patch(get_defining_object(echo), "echo", uppercase):25 assert echo("foo") == "ECHO: FOO"26 assert echo("foo") == "echo: foo"27def test_patch_decorator():28 @patch(target=echo, pass_target=False)29 def monkey(arg):30 return f"monkey: {arg}"31 assert echo("foo") == "monkey: foo"32 monkey.patch.undo()33 assert echo("foo") == "echo: foo"34def test_patch_decorator_with_pass_target():35 @patch(target=echo)36 def uppercase(target, arg):37 return target(arg).upper()38 assert echo("foo") == "ECHO: FOO"39 uppercase.patch.undo()40 assert echo("foo") == "echo: foo"41def test_patch_decorator_on_method():42 @patch(target=MyEchoer.do_echo)43 def uppercase(target, self, arg):44 return target(self, arg).upper()45 obj = MyEchoer()46 assert obj.do_echo("foo") == "DO_ECHO: FOO"47 uppercase.patch.undo()48 assert obj.do_echo("foo") == "do_echo: foo"49 assert MyEchoer().do_echo("foo") == "do_echo: foo"50def test_patch_decorator_on_bound_method_with_pass_target():51 obj = MyEchoer()52 @patch(target=obj.do_echo)53 def uppercase(self, target, arg):54 return target(arg).upper()55 assert obj.do_echo("foo") == "DO_ECHO: FOO"56 assert MyEchoer().do_echo("foo") == "do_echo: foo"57 uppercase.patch.undo()58 assert obj.do_echo("foo") == "do_echo: foo"59 assert MyEchoer().do_echo("foo") == "do_echo: foo"60def test_patch_decorator_on_bound_method():61 obj = MyEchoer()62 @patch(target=obj.do_echo, pass_target=False)63 def monkey(self, arg):64 return f"monkey: {arg}"65 assert obj.do_echo("foo") == "monkey: foo"66 assert MyEchoer().do_echo("foo") == "do_echo: foo"67 monkey.patch.undo()68 assert obj.do_echo("foo") == "do_echo: foo"69 assert MyEchoer().do_echo("foo") == "do_echo: foo"70def test_patch_decorator_on_class_method():71 @patch(target=MyEchoer.do_class_echo)72 def uppercase(target, *args):73 if len(args) > 1:74 # this happens when the method is called on an object, the first arg will be the object75 arg = args[1]76 else:77 arg = args[0]78 return target(arg).upper()79 assert MyEchoer.do_class_echo("foo") == "DO_CLASS_ECHO: FOO"80 assert MyEchoer().do_class_echo("foo") == "DO_CLASS_ECHO: FOO"81 uppercase.patch.undo()82 assert MyEchoer.do_class_echo("foo") == "do_class_echo: foo"83 assert MyEchoer().do_class_echo("foo") == "do_class_echo: foo"84def test_get_defining_object():85 from localstack.utils import strings86 from localstack.utils.strings import short_uid87 # module88 assert get_defining_object(short_uid) == strings89 # unbound method (=function defined by a class)90 assert get_defining_object(MyEchoer.do_echo) == MyEchoer91 obj = MyEchoer()92 # bound method93 assert get_defining_object(obj.do_echo) == obj94 # class method referenced by an object95 assert get_defining_object(obj.do_class_echo) == MyEchoer96 # class method referenced by the class97 assert get_defining_object(MyEchoer.do_class_echo) == MyEchoer...
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!!