1 #!/usr/bin/python 2 3 __author__ = "raphtee (at] google.com (Travis Miller)" 4 5 import mock, mock_demo_MUT 6 7 class MyError(Exception): 8 pass 9 10 11 class A(object): 12 var = 8 13 14 def __init__(self): 15 self.x = 0 16 17 def method1(self): 18 self.x += 1 19 return self.x 20 21 def method2(self, y): 22 return y * self.x 23 24 class B(A): 25 def method3(self, z): 26 return self.x + z 27 28 def method4(self, z, w): 29 return self.x * z + w 30 31 32 class C(B): 33 def method5(self): 34 self.method1() 35 t = self.method2(4) 36 u = self.method3(t) 37 return u 38 39 40 class D(C): 41 def method6(self, error): 42 if error: 43 raise MyError("woops") 44 else: 45 return 10 46 47 class E(D): 48 def __init__(self, val): 49 self.val = val 50 51 52 # say we want to test that do_stuff is doing what we think it is doing 53 def do_stuff(a, b, func): 54 print b.method1() 55 print b.method3(10) 56 print func("how many") 57 print a.method2(5) 58 print b.method1() 59 print b.method4(1, 4) 60 print b.method2(3) 61 print b.method2("hello") 62 63 64 def do_more_stuff(d): 65 print d.method6(False) 66 try: 67 d.method6(True) 68 except: 69 print "caught error" 70 71 72 def main(): 73 god = mock.mock_god() 74 75 m1 = god.create_mock_class(A, "A") 76 print m1.var 77 m2 = god.create_mock_class(B, "B") 78 f = god.create_mock_function("func") 79 80 print dir(m1) 81 print dir(m2) 82 83 # sets up the "recording" 84 m2.method1.expect_call().and_return(1) 85 m2.method3.expect_call(10).and_return(10) 86 f.expect_call("how many").and_return(42) 87 m1.method2.expect_call(5).and_return(0) 88 m2.method1.expect_call().and_return(2) 89 m2.method4.expect_call(1, 4).and_return(6) 90 m2.method2.expect_call(3).and_return(6) 91 m2.method2.expect_call(mock.is_string_comparator()).and_return("foo") 92 93 # check the recording order 94 for func_call in god.recording: 95 print func_call 96 97 # once we start making calls into the methods we are in 98 # playback mode 99 do_stuff(m1, m2, f) 100 101 # we can now check that playback succeeded 102 god.check_playback() 103 104 # now test the ability to mock out all methods of an object 105 # except those under test 106 c = C() 107 god.mock_up(c, "c") 108 109 # setup recording 110 c.method1.expect_call() 111 c.method2.expect_call(4).and_return(4) 112 c.method3.expect_call(4).and_return(5) 113 114 # perform the test 115 answer = c.method5.run_original_function() 116 117 # check playback 118 print "answer = %s" % (answer) 119 god.check_playback() 120 121 # check exception returns too 122 m3 = god.create_mock_class(D, "D") 123 m3.method6.expect_call(False).and_return(10) 124 m3.method6.expect_call(True).and_raises(MyError("woops")) 125 126 do_more_stuff(m3) 127 god.check_playback() 128 129 # now check we can mock out a whole class (rather than just an instance) 130 mockE = god.create_mock_class_obj(E, "E") 131 oldE = mock_demo_MUT.E 132 mock_demo_MUT.E = mockE 133 134 m4 = mockE.expect_new(val=7) 135 m4.method1.expect_call().and_return(1) 136 137 mock_demo_MUT.do_create_stuff() 138 god.check_playback() 139 140 mock_demo_MUT.E = oldE 141 142 143 if __name__ == "__main__": 144 main() 145