How to use create_mock_class method in autotest

Best Python code snippet using autotest_python

mock_demo.py

Source:mock_demo.py Github

copy

Full Screen

...49 except:50 print "caught error"51def main():52 god = mock.mock_god()53 m1 = god.create_mock_class(A, "A")54 print m1.var55 m2 = god.create_mock_class(B, "B")56 f = god.create_mock_function("func")57 print dir(m1)58 print dir(m2)59 # sets up the "recording"60 m2.method1.expect_call().and_return(1)61 m2.method3.expect_call(10).and_return(10)62 f.expect_call("how many").and_return(42)63 m1.method2.expect_call(5).and_return(0)64 m2.method1.expect_call().and_return(2)65 m2.method4.expect_call(1, 4).and_return(6)66 m2.method2.expect_call(3).and_return(6)67 m2.method2.expect_call(mock.is_string_comparator()).and_return("foo")68 # check the recording order69 for func_call in god.recording:70 print func_call71 # once we start making calls into the methods we are in72 # playback mode73 do_stuff(m1, m2, f)74 # we can now check that playback succeeded75 god.check_playback()76 # now test the ability to mock out all methods of an object77 # except those under test78 c = C()79 god.mock_up(c, "c")80 # setup recording81 c.method1.expect_call()82 c.method2.expect_call(4).and_return(4)83 c.method3.expect_call(4).and_return(5)84 # perform the test85 answer = c.method5.run_original_function()86 # check playback87 print "answer = %s" % (answer)88 god.check_playback()89 # check exception returns too90 m3 = god.create_mock_class(D, "D")91 m3.method6.expect_call(False).and_return(10)92 m3.method6.expect_call(True).and_raises(MyError("woops"))93 do_more_stuff(m3)94 god.check_playback()95 # now check we can mock out a whole class (rather than just an instance)96 mockE = god.create_mock_class_obj(E, "E")97 oldE = mock_demo_MUT.E98 mock_demo_MUT.E = mockE99 m4 = mockE.expect_new(val=7)100 m4.method1.expect_call().and_return(1)101 mock_demo_MUT.do_create_stuff()102 god.check_playback()103 mock_demo_MUT.E = oldE104if __name__ == "__main__":...

Full Screen

Full Screen

utils.py

Source:utils.py Github

copy

Full Screen

...3from django.http import HttpRequest4from django.http import HttpResponse5from permission.handlers import PermissionHandler6from permission.tests.compat import MagicMock7def create_mock_class(name, base, instance=None):8 instance = instance or MagicMock()9 mock_class = MagicMock(name=name,10 return_value=instance)11 mock_class.__bases__ = (type, base)12 mock_class.__class__ = type13 return mock_class14def create_mock_handler():15 instance = MagicMock(**{16 'has_perm.return_value': False,17 'get_permissions.return_value': [18 'permission.add_article',19 ],20 })21 handler = create_mock_class('MockPermissionHandler',22 base=PermissionHandler,23 instance=instance)24 return handler25def create_mock_request(mock_permission_handler):26 request = MagicMock(spec=HttpRequest)27 request.build_absolute_uri = MagicMock(return_value="/")28 request.META = MagicMock()29 request.user = MagicMock(**{30 'is_active.return_value': True,31 'is_authenticated.return_value': True,32 'has_perm.side_effect': mock_permission_handler.has_perm,33 })34 return request35def create_mock_view_func():...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful