1 | n/a | import unittest |
---|
2 | n/a | |
---|
3 | n/a | from unittest.mock import ( |
---|
4 | n/a | call, _Call, create_autospec, MagicMock, |
---|
5 | n/a | Mock, ANY, _CallList, patch, PropertyMock |
---|
6 | n/a | ) |
---|
7 | n/a | |
---|
8 | n/a | from datetime import datetime |
---|
9 | n/a | |
---|
10 | n/a | class SomeClass(object): |
---|
11 | n/a | def one(self, a, b): |
---|
12 | n/a | pass |
---|
13 | n/a | def two(self): |
---|
14 | n/a | pass |
---|
15 | n/a | def three(self, a=None): |
---|
16 | n/a | pass |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | class AnyTest(unittest.TestCase): |
---|
21 | n/a | |
---|
22 | n/a | def test_any(self): |
---|
23 | n/a | self.assertEqual(ANY, object()) |
---|
24 | n/a | |
---|
25 | n/a | mock = Mock() |
---|
26 | n/a | mock(ANY) |
---|
27 | n/a | mock.assert_called_with(ANY) |
---|
28 | n/a | |
---|
29 | n/a | mock = Mock() |
---|
30 | n/a | mock(foo=ANY) |
---|
31 | n/a | mock.assert_called_with(foo=ANY) |
---|
32 | n/a | |
---|
33 | n/a | def test_repr(self): |
---|
34 | n/a | self.assertEqual(repr(ANY), '<ANY>') |
---|
35 | n/a | self.assertEqual(str(ANY), '<ANY>') |
---|
36 | n/a | |
---|
37 | n/a | |
---|
38 | n/a | def test_any_and_datetime(self): |
---|
39 | n/a | mock = Mock() |
---|
40 | n/a | mock(datetime.now(), foo=datetime.now()) |
---|
41 | n/a | |
---|
42 | n/a | mock.assert_called_with(ANY, foo=ANY) |
---|
43 | n/a | |
---|
44 | n/a | |
---|
45 | n/a | def test_any_mock_calls_comparison_order(self): |
---|
46 | n/a | mock = Mock() |
---|
47 | n/a | d = datetime.now() |
---|
48 | n/a | class Foo(object): |
---|
49 | n/a | def __eq__(self, other): |
---|
50 | n/a | return False |
---|
51 | n/a | def __ne__(self, other): |
---|
52 | n/a | return True |
---|
53 | n/a | |
---|
54 | n/a | for d in datetime.now(), Foo(): |
---|
55 | n/a | mock.reset_mock() |
---|
56 | n/a | |
---|
57 | n/a | mock(d, foo=d, bar=d) |
---|
58 | n/a | mock.method(d, zinga=d, alpha=d) |
---|
59 | n/a | mock().method(a1=d, z99=d) |
---|
60 | n/a | |
---|
61 | n/a | expected = [ |
---|
62 | n/a | call(ANY, foo=ANY, bar=ANY), |
---|
63 | n/a | call.method(ANY, zinga=ANY, alpha=ANY), |
---|
64 | n/a | call(), call().method(a1=ANY, z99=ANY) |
---|
65 | n/a | ] |
---|
66 | n/a | self.assertEqual(expected, mock.mock_calls) |
---|
67 | n/a | self.assertEqual(mock.mock_calls, expected) |
---|
68 | n/a | |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | class CallTest(unittest.TestCase): |
---|
72 | n/a | |
---|
73 | n/a | def test_call_with_call(self): |
---|
74 | n/a | kall = _Call() |
---|
75 | n/a | self.assertEqual(kall, _Call()) |
---|
76 | n/a | self.assertEqual(kall, _Call(('',))) |
---|
77 | n/a | self.assertEqual(kall, _Call(((),))) |
---|
78 | n/a | self.assertEqual(kall, _Call(({},))) |
---|
79 | n/a | self.assertEqual(kall, _Call(('', ()))) |
---|
80 | n/a | self.assertEqual(kall, _Call(('', {}))) |
---|
81 | n/a | self.assertEqual(kall, _Call(('', (), {}))) |
---|
82 | n/a | self.assertEqual(kall, _Call(('foo',))) |
---|
83 | n/a | self.assertEqual(kall, _Call(('bar', ()))) |
---|
84 | n/a | self.assertEqual(kall, _Call(('baz', {}))) |
---|
85 | n/a | self.assertEqual(kall, _Call(('spam', (), {}))) |
---|
86 | n/a | |
---|
87 | n/a | kall = _Call(((1, 2, 3),)) |
---|
88 | n/a | self.assertEqual(kall, _Call(((1, 2, 3),))) |
---|
89 | n/a | self.assertEqual(kall, _Call(('', (1, 2, 3)))) |
---|
90 | n/a | self.assertEqual(kall, _Call(((1, 2, 3), {}))) |
---|
91 | n/a | self.assertEqual(kall, _Call(('', (1, 2, 3), {}))) |
---|
92 | n/a | |
---|
93 | n/a | kall = _Call(((1, 2, 4),)) |
---|
94 | n/a | self.assertNotEqual(kall, _Call(('', (1, 2, 3)))) |
---|
95 | n/a | self.assertNotEqual(kall, _Call(('', (1, 2, 3), {}))) |
---|
96 | n/a | |
---|
97 | n/a | kall = _Call(('foo', (1, 2, 4),)) |
---|
98 | n/a | self.assertNotEqual(kall, _Call(('', (1, 2, 4)))) |
---|
99 | n/a | self.assertNotEqual(kall, _Call(('', (1, 2, 4), {}))) |
---|
100 | n/a | self.assertNotEqual(kall, _Call(('bar', (1, 2, 4)))) |
---|
101 | n/a | self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {}))) |
---|
102 | n/a | |
---|
103 | n/a | kall = _Call(({'a': 3},)) |
---|
104 | n/a | self.assertEqual(kall, _Call(('', (), {'a': 3}))) |
---|
105 | n/a | self.assertEqual(kall, _Call(('', {'a': 3}))) |
---|
106 | n/a | self.assertEqual(kall, _Call(((), {'a': 3}))) |
---|
107 | n/a | self.assertEqual(kall, _Call(({'a': 3},))) |
---|
108 | n/a | |
---|
109 | n/a | |
---|
110 | n/a | def test_empty__Call(self): |
---|
111 | n/a | args = _Call() |
---|
112 | n/a | |
---|
113 | n/a | self.assertEqual(args, ()) |
---|
114 | n/a | self.assertEqual(args, ('foo',)) |
---|
115 | n/a | self.assertEqual(args, ((),)) |
---|
116 | n/a | self.assertEqual(args, ('foo', ())) |
---|
117 | n/a | self.assertEqual(args, ('foo',(), {})) |
---|
118 | n/a | self.assertEqual(args, ('foo', {})) |
---|
119 | n/a | self.assertEqual(args, ({},)) |
---|
120 | n/a | |
---|
121 | n/a | |
---|
122 | n/a | def test_named_empty_call(self): |
---|
123 | n/a | args = _Call(('foo', (), {})) |
---|
124 | n/a | |
---|
125 | n/a | self.assertEqual(args, ('foo',)) |
---|
126 | n/a | self.assertEqual(args, ('foo', ())) |
---|
127 | n/a | self.assertEqual(args, ('foo',(), {})) |
---|
128 | n/a | self.assertEqual(args, ('foo', {})) |
---|
129 | n/a | |
---|
130 | n/a | self.assertNotEqual(args, ((),)) |
---|
131 | n/a | self.assertNotEqual(args, ()) |
---|
132 | n/a | self.assertNotEqual(args, ({},)) |
---|
133 | n/a | self.assertNotEqual(args, ('bar',)) |
---|
134 | n/a | self.assertNotEqual(args, ('bar', ())) |
---|
135 | n/a | self.assertNotEqual(args, ('bar', {})) |
---|
136 | n/a | |
---|
137 | n/a | |
---|
138 | n/a | def test_call_with_args(self): |
---|
139 | n/a | args = _Call(((1, 2, 3), {})) |
---|
140 | n/a | |
---|
141 | n/a | self.assertEqual(args, ((1, 2, 3),)) |
---|
142 | n/a | self.assertEqual(args, ('foo', (1, 2, 3))) |
---|
143 | n/a | self.assertEqual(args, ('foo', (1, 2, 3), {})) |
---|
144 | n/a | self.assertEqual(args, ((1, 2, 3), {})) |
---|
145 | n/a | |
---|
146 | n/a | |
---|
147 | n/a | def test_named_call_with_args(self): |
---|
148 | n/a | args = _Call(('foo', (1, 2, 3), {})) |
---|
149 | n/a | |
---|
150 | n/a | self.assertEqual(args, ('foo', (1, 2, 3))) |
---|
151 | n/a | self.assertEqual(args, ('foo', (1, 2, 3), {})) |
---|
152 | n/a | |
---|
153 | n/a | self.assertNotEqual(args, ((1, 2, 3),)) |
---|
154 | n/a | self.assertNotEqual(args, ((1, 2, 3), {})) |
---|
155 | n/a | |
---|
156 | n/a | |
---|
157 | n/a | def test_call_with_kwargs(self): |
---|
158 | n/a | args = _Call(((), dict(a=3, b=4))) |
---|
159 | n/a | |
---|
160 | n/a | self.assertEqual(args, (dict(a=3, b=4),)) |
---|
161 | n/a | self.assertEqual(args, ('foo', dict(a=3, b=4))) |
---|
162 | n/a | self.assertEqual(args, ('foo', (), dict(a=3, b=4))) |
---|
163 | n/a | self.assertEqual(args, ((), dict(a=3, b=4))) |
---|
164 | n/a | |
---|
165 | n/a | |
---|
166 | n/a | def test_named_call_with_kwargs(self): |
---|
167 | n/a | args = _Call(('foo', (), dict(a=3, b=4))) |
---|
168 | n/a | |
---|
169 | n/a | self.assertEqual(args, ('foo', dict(a=3, b=4))) |
---|
170 | n/a | self.assertEqual(args, ('foo', (), dict(a=3, b=4))) |
---|
171 | n/a | |
---|
172 | n/a | self.assertNotEqual(args, (dict(a=3, b=4),)) |
---|
173 | n/a | self.assertNotEqual(args, ((), dict(a=3, b=4))) |
---|
174 | n/a | |
---|
175 | n/a | |
---|
176 | n/a | def test_call_with_args_call_empty_name(self): |
---|
177 | n/a | args = _Call(((1, 2, 3), {})) |
---|
178 | n/a | self.assertEqual(args, call(1, 2, 3)) |
---|
179 | n/a | self.assertEqual(call(1, 2, 3), args) |
---|
180 | n/a | self.assertIn(call(1, 2, 3), [args]) |
---|
181 | n/a | |
---|
182 | n/a | |
---|
183 | n/a | def test_call_ne(self): |
---|
184 | n/a | self.assertNotEqual(_Call(((1, 2, 3),)), call(1, 2)) |
---|
185 | n/a | self.assertFalse(_Call(((1, 2, 3),)) != call(1, 2, 3)) |
---|
186 | n/a | self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3)) |
---|
187 | n/a | |
---|
188 | n/a | |
---|
189 | n/a | def test_call_non_tuples(self): |
---|
190 | n/a | kall = _Call(((1, 2, 3),)) |
---|
191 | n/a | for value in 1, None, self, int: |
---|
192 | n/a | self.assertNotEqual(kall, value) |
---|
193 | n/a | self.assertFalse(kall == value) |
---|
194 | n/a | |
---|
195 | n/a | |
---|
196 | n/a | def test_repr(self): |
---|
197 | n/a | self.assertEqual(repr(_Call()), 'call()') |
---|
198 | n/a | self.assertEqual(repr(_Call(('foo',))), 'call.foo()') |
---|
199 | n/a | |
---|
200 | n/a | self.assertEqual(repr(_Call(((1, 2, 3), {'a': 'b'}))), |
---|
201 | n/a | "call(1, 2, 3, a='b')") |
---|
202 | n/a | self.assertEqual(repr(_Call(('bar', (1, 2, 3), {'a': 'b'}))), |
---|
203 | n/a | "call.bar(1, 2, 3, a='b')") |
---|
204 | n/a | |
---|
205 | n/a | self.assertEqual(repr(call), 'call') |
---|
206 | n/a | self.assertEqual(str(call), 'call') |
---|
207 | n/a | |
---|
208 | n/a | self.assertEqual(repr(call()), 'call()') |
---|
209 | n/a | self.assertEqual(repr(call(1)), 'call(1)') |
---|
210 | n/a | self.assertEqual(repr(call(zz='thing')), "call(zz='thing')") |
---|
211 | n/a | |
---|
212 | n/a | self.assertEqual(repr(call().foo), 'call().foo') |
---|
213 | n/a | self.assertEqual(repr(call(1).foo.bar(a=3).bing), |
---|
214 | n/a | 'call().foo.bar().bing') |
---|
215 | n/a | self.assertEqual( |
---|
216 | n/a | repr(call().foo(1, 2, a=3)), |
---|
217 | n/a | "call().foo(1, 2, a=3)" |
---|
218 | n/a | ) |
---|
219 | n/a | self.assertEqual(repr(call()()), "call()()") |
---|
220 | n/a | self.assertEqual(repr(call(1)(2)), "call()(2)") |
---|
221 | n/a | self.assertEqual( |
---|
222 | n/a | repr(call()().bar().baz.beep(1)), |
---|
223 | n/a | "call()().bar().baz.beep(1)" |
---|
224 | n/a | ) |
---|
225 | n/a | |
---|
226 | n/a | |
---|
227 | n/a | def test_call(self): |
---|
228 | n/a | self.assertEqual(call(), ('', (), {})) |
---|
229 | n/a | self.assertEqual(call('foo', 'bar', one=3, two=4), |
---|
230 | n/a | ('', ('foo', 'bar'), {'one': 3, 'two': 4})) |
---|
231 | n/a | |
---|
232 | n/a | mock = Mock() |
---|
233 | n/a | mock(1, 2, 3) |
---|
234 | n/a | mock(a=3, b=6) |
---|
235 | n/a | self.assertEqual(mock.call_args_list, |
---|
236 | n/a | [call(1, 2, 3), call(a=3, b=6)]) |
---|
237 | n/a | |
---|
238 | n/a | def test_attribute_call(self): |
---|
239 | n/a | self.assertEqual(call.foo(1), ('foo', (1,), {})) |
---|
240 | n/a | self.assertEqual(call.bar.baz(fish='eggs'), |
---|
241 | n/a | ('bar.baz', (), {'fish': 'eggs'})) |
---|
242 | n/a | |
---|
243 | n/a | mock = Mock() |
---|
244 | n/a | mock.foo(1, 2 ,3) |
---|
245 | n/a | mock.bar.baz(a=3, b=6) |
---|
246 | n/a | self.assertEqual(mock.method_calls, |
---|
247 | n/a | [call.foo(1, 2, 3), call.bar.baz(a=3, b=6)]) |
---|
248 | n/a | |
---|
249 | n/a | |
---|
250 | n/a | def test_extended_call(self): |
---|
251 | n/a | result = call(1).foo(2).bar(3, a=4) |
---|
252 | n/a | self.assertEqual(result, ('().foo().bar', (3,), dict(a=4))) |
---|
253 | n/a | |
---|
254 | n/a | mock = MagicMock() |
---|
255 | n/a | mock(1, 2, a=3, b=4) |
---|
256 | n/a | self.assertEqual(mock.call_args, call(1, 2, a=3, b=4)) |
---|
257 | n/a | self.assertNotEqual(mock.call_args, call(1, 2, 3)) |
---|
258 | n/a | |
---|
259 | n/a | self.assertEqual(mock.call_args_list, [call(1, 2, a=3, b=4)]) |
---|
260 | n/a | self.assertEqual(mock.mock_calls, [call(1, 2, a=3, b=4)]) |
---|
261 | n/a | |
---|
262 | n/a | mock = MagicMock() |
---|
263 | n/a | mock.foo(1).bar()().baz.beep(a=6) |
---|
264 | n/a | |
---|
265 | n/a | last_call = call.foo(1).bar()().baz.beep(a=6) |
---|
266 | n/a | self.assertEqual(mock.mock_calls[-1], last_call) |
---|
267 | n/a | self.assertEqual(mock.mock_calls, last_call.call_list()) |
---|
268 | n/a | |
---|
269 | n/a | |
---|
270 | n/a | def test_call_list(self): |
---|
271 | n/a | mock = MagicMock() |
---|
272 | n/a | mock(1) |
---|
273 | n/a | self.assertEqual(call(1).call_list(), mock.mock_calls) |
---|
274 | n/a | |
---|
275 | n/a | mock = MagicMock() |
---|
276 | n/a | mock(1).method(2) |
---|
277 | n/a | self.assertEqual(call(1).method(2).call_list(), |
---|
278 | n/a | mock.mock_calls) |
---|
279 | n/a | |
---|
280 | n/a | mock = MagicMock() |
---|
281 | n/a | mock(1).method(2)(3) |
---|
282 | n/a | self.assertEqual(call(1).method(2)(3).call_list(), |
---|
283 | n/a | mock.mock_calls) |
---|
284 | n/a | |
---|
285 | n/a | mock = MagicMock() |
---|
286 | n/a | int(mock(1).method(2)(3).foo.bar.baz(4)(5)) |
---|
287 | n/a | kall = call(1).method(2)(3).foo.bar.baz(4)(5).__int__() |
---|
288 | n/a | self.assertEqual(kall.call_list(), mock.mock_calls) |
---|
289 | n/a | |
---|
290 | n/a | |
---|
291 | n/a | def test_call_any(self): |
---|
292 | n/a | self.assertEqual(call, ANY) |
---|
293 | n/a | |
---|
294 | n/a | m = MagicMock() |
---|
295 | n/a | int(m) |
---|
296 | n/a | self.assertEqual(m.mock_calls, [ANY]) |
---|
297 | n/a | self.assertEqual([ANY], m.mock_calls) |
---|
298 | n/a | |
---|
299 | n/a | |
---|
300 | n/a | def test_two_args_call(self): |
---|
301 | n/a | args = _Call(((1, 2), {'a': 3}), two=True) |
---|
302 | n/a | self.assertEqual(len(args), 2) |
---|
303 | n/a | self.assertEqual(args[0], (1, 2)) |
---|
304 | n/a | self.assertEqual(args[1], {'a': 3}) |
---|
305 | n/a | |
---|
306 | n/a | other_args = _Call(((1, 2), {'a': 3})) |
---|
307 | n/a | self.assertEqual(args, other_args) |
---|
308 | n/a | |
---|
309 | n/a | def test_call_with_name(self): |
---|
310 | n/a | self.assertEqual( |
---|
311 | n/a | 'foo', |
---|
312 | n/a | _Call((), 'foo')[0], |
---|
313 | n/a | ) |
---|
314 | n/a | self.assertEqual( |
---|
315 | n/a | '', |
---|
316 | n/a | _Call((('bar', 'barz'), ), )[0] |
---|
317 | n/a | ) |
---|
318 | n/a | self.assertEqual( |
---|
319 | n/a | '', |
---|
320 | n/a | _Call((('bar', 'barz'), {'hello': 'world'}), )[0] |
---|
321 | n/a | ) |
---|
322 | n/a | |
---|
323 | n/a | |
---|
324 | n/a | class SpecSignatureTest(unittest.TestCase): |
---|
325 | n/a | |
---|
326 | n/a | def _check_someclass_mock(self, mock): |
---|
327 | n/a | self.assertRaises(AttributeError, getattr, mock, 'foo') |
---|
328 | n/a | mock.one(1, 2) |
---|
329 | n/a | mock.one.assert_called_with(1, 2) |
---|
330 | n/a | self.assertRaises(AssertionError, |
---|
331 | n/a | mock.one.assert_called_with, 3, 4) |
---|
332 | n/a | self.assertRaises(TypeError, mock.one, 1) |
---|
333 | n/a | |
---|
334 | n/a | mock.two() |
---|
335 | n/a | mock.two.assert_called_with() |
---|
336 | n/a | self.assertRaises(AssertionError, |
---|
337 | n/a | mock.two.assert_called_with, 3) |
---|
338 | n/a | self.assertRaises(TypeError, mock.two, 1) |
---|
339 | n/a | |
---|
340 | n/a | mock.three() |
---|
341 | n/a | mock.three.assert_called_with() |
---|
342 | n/a | self.assertRaises(AssertionError, |
---|
343 | n/a | mock.three.assert_called_with, 3) |
---|
344 | n/a | self.assertRaises(TypeError, mock.three, 3, 2) |
---|
345 | n/a | |
---|
346 | n/a | mock.three(1) |
---|
347 | n/a | mock.three.assert_called_with(1) |
---|
348 | n/a | |
---|
349 | n/a | mock.three(a=1) |
---|
350 | n/a | mock.three.assert_called_with(a=1) |
---|
351 | n/a | |
---|
352 | n/a | |
---|
353 | n/a | def test_basic(self): |
---|
354 | n/a | mock = create_autospec(SomeClass) |
---|
355 | n/a | self._check_someclass_mock(mock) |
---|
356 | n/a | mock = create_autospec(SomeClass()) |
---|
357 | n/a | self._check_someclass_mock(mock) |
---|
358 | n/a | |
---|
359 | n/a | |
---|
360 | n/a | def test_create_autospec_return_value(self): |
---|
361 | n/a | def f(): |
---|
362 | n/a | pass |
---|
363 | n/a | mock = create_autospec(f, return_value='foo') |
---|
364 | n/a | self.assertEqual(mock(), 'foo') |
---|
365 | n/a | |
---|
366 | n/a | class Foo(object): |
---|
367 | n/a | pass |
---|
368 | n/a | |
---|
369 | n/a | mock = create_autospec(Foo, return_value='foo') |
---|
370 | n/a | self.assertEqual(mock(), 'foo') |
---|
371 | n/a | |
---|
372 | n/a | |
---|
373 | n/a | def test_autospec_reset_mock(self): |
---|
374 | n/a | m = create_autospec(int) |
---|
375 | n/a | int(m) |
---|
376 | n/a | m.reset_mock() |
---|
377 | n/a | self.assertEqual(m.__int__.call_count, 0) |
---|
378 | n/a | |
---|
379 | n/a | |
---|
380 | n/a | def test_mocking_unbound_methods(self): |
---|
381 | n/a | class Foo(object): |
---|
382 | n/a | def foo(self, foo): |
---|
383 | n/a | pass |
---|
384 | n/a | p = patch.object(Foo, 'foo') |
---|
385 | n/a | mock_foo = p.start() |
---|
386 | n/a | Foo().foo(1) |
---|
387 | n/a | |
---|
388 | n/a | mock_foo.assert_called_with(1) |
---|
389 | n/a | |
---|
390 | n/a | |
---|
391 | n/a | def test_create_autospec_unbound_methods(self): |
---|
392 | n/a | # see mock issue 128 |
---|
393 | n/a | # this is expected to fail until the issue is fixed |
---|
394 | n/a | return |
---|
395 | n/a | class Foo(object): |
---|
396 | n/a | def foo(self): |
---|
397 | n/a | pass |
---|
398 | n/a | |
---|
399 | n/a | klass = create_autospec(Foo) |
---|
400 | n/a | instance = klass() |
---|
401 | n/a | self.assertRaises(TypeError, instance.foo, 1) |
---|
402 | n/a | |
---|
403 | n/a | # Note: no type checking on the "self" parameter |
---|
404 | n/a | klass.foo(1) |
---|
405 | n/a | klass.foo.assert_called_with(1) |
---|
406 | n/a | self.assertRaises(TypeError, klass.foo) |
---|
407 | n/a | |
---|
408 | n/a | |
---|
409 | n/a | def test_create_autospec_keyword_arguments(self): |
---|
410 | n/a | class Foo(object): |
---|
411 | n/a | a = 3 |
---|
412 | n/a | m = create_autospec(Foo, a='3') |
---|
413 | n/a | self.assertEqual(m.a, '3') |
---|
414 | n/a | |
---|
415 | n/a | |
---|
416 | n/a | def test_create_autospec_keyword_only_arguments(self): |
---|
417 | n/a | def foo(a, *, b=None): |
---|
418 | n/a | pass |
---|
419 | n/a | |
---|
420 | n/a | m = create_autospec(foo) |
---|
421 | n/a | m(1) |
---|
422 | n/a | m.assert_called_with(1) |
---|
423 | n/a | self.assertRaises(TypeError, m, 1, 2) |
---|
424 | n/a | |
---|
425 | n/a | m(2, b=3) |
---|
426 | n/a | m.assert_called_with(2, b=3) |
---|
427 | n/a | |
---|
428 | n/a | |
---|
429 | n/a | def test_function_as_instance_attribute(self): |
---|
430 | n/a | obj = SomeClass() |
---|
431 | n/a | def f(a): |
---|
432 | n/a | pass |
---|
433 | n/a | obj.f = f |
---|
434 | n/a | |
---|
435 | n/a | mock = create_autospec(obj) |
---|
436 | n/a | mock.f('bing') |
---|
437 | n/a | mock.f.assert_called_with('bing') |
---|
438 | n/a | |
---|
439 | n/a | |
---|
440 | n/a | def test_spec_as_list(self): |
---|
441 | n/a | # because spec as a list of strings in the mock constructor means |
---|
442 | n/a | # something very different we treat a list instance as the type. |
---|
443 | n/a | mock = create_autospec([]) |
---|
444 | n/a | mock.append('foo') |
---|
445 | n/a | mock.append.assert_called_with('foo') |
---|
446 | n/a | |
---|
447 | n/a | self.assertRaises(AttributeError, getattr, mock, 'foo') |
---|
448 | n/a | |
---|
449 | n/a | class Foo(object): |
---|
450 | n/a | foo = [] |
---|
451 | n/a | |
---|
452 | n/a | mock = create_autospec(Foo) |
---|
453 | n/a | mock.foo.append(3) |
---|
454 | n/a | mock.foo.append.assert_called_with(3) |
---|
455 | n/a | self.assertRaises(AttributeError, getattr, mock.foo, 'foo') |
---|
456 | n/a | |
---|
457 | n/a | |
---|
458 | n/a | def test_attributes(self): |
---|
459 | n/a | class Sub(SomeClass): |
---|
460 | n/a | attr = SomeClass() |
---|
461 | n/a | |
---|
462 | n/a | sub_mock = create_autospec(Sub) |
---|
463 | n/a | |
---|
464 | n/a | for mock in (sub_mock, sub_mock.attr): |
---|
465 | n/a | self._check_someclass_mock(mock) |
---|
466 | n/a | |
---|
467 | n/a | |
---|
468 | n/a | def test_builtin_functions_types(self): |
---|
469 | n/a | # we could replace builtin functions / methods with a function |
---|
470 | n/a | # with *args / **kwargs signature. Using the builtin method type |
---|
471 | n/a | # as a spec seems to work fairly well though. |
---|
472 | n/a | class BuiltinSubclass(list): |
---|
473 | n/a | def bar(self, arg): |
---|
474 | n/a | pass |
---|
475 | n/a | sorted = sorted |
---|
476 | n/a | attr = {} |
---|
477 | n/a | |
---|
478 | n/a | mock = create_autospec(BuiltinSubclass) |
---|
479 | n/a | mock.append(3) |
---|
480 | n/a | mock.append.assert_called_with(3) |
---|
481 | n/a | self.assertRaises(AttributeError, getattr, mock.append, 'foo') |
---|
482 | n/a | |
---|
483 | n/a | mock.bar('foo') |
---|
484 | n/a | mock.bar.assert_called_with('foo') |
---|
485 | n/a | self.assertRaises(TypeError, mock.bar, 'foo', 'bar') |
---|
486 | n/a | self.assertRaises(AttributeError, getattr, mock.bar, 'foo') |
---|
487 | n/a | |
---|
488 | n/a | mock.sorted([1, 2]) |
---|
489 | n/a | mock.sorted.assert_called_with([1, 2]) |
---|
490 | n/a | self.assertRaises(AttributeError, getattr, mock.sorted, 'foo') |
---|
491 | n/a | |
---|
492 | n/a | mock.attr.pop(3) |
---|
493 | n/a | mock.attr.pop.assert_called_with(3) |
---|
494 | n/a | self.assertRaises(AttributeError, getattr, mock.attr, 'foo') |
---|
495 | n/a | |
---|
496 | n/a | |
---|
497 | n/a | def test_method_calls(self): |
---|
498 | n/a | class Sub(SomeClass): |
---|
499 | n/a | attr = SomeClass() |
---|
500 | n/a | |
---|
501 | n/a | mock = create_autospec(Sub) |
---|
502 | n/a | mock.one(1, 2) |
---|
503 | n/a | mock.two() |
---|
504 | n/a | mock.three(3) |
---|
505 | n/a | |
---|
506 | n/a | expected = [call.one(1, 2), call.two(), call.three(3)] |
---|
507 | n/a | self.assertEqual(mock.method_calls, expected) |
---|
508 | n/a | |
---|
509 | n/a | mock.attr.one(1, 2) |
---|
510 | n/a | mock.attr.two() |
---|
511 | n/a | mock.attr.three(3) |
---|
512 | n/a | |
---|
513 | n/a | expected.extend( |
---|
514 | n/a | [call.attr.one(1, 2), call.attr.two(), call.attr.three(3)] |
---|
515 | n/a | ) |
---|
516 | n/a | self.assertEqual(mock.method_calls, expected) |
---|
517 | n/a | |
---|
518 | n/a | |
---|
519 | n/a | def test_magic_methods(self): |
---|
520 | n/a | class BuiltinSubclass(list): |
---|
521 | n/a | attr = {} |
---|
522 | n/a | |
---|
523 | n/a | mock = create_autospec(BuiltinSubclass) |
---|
524 | n/a | self.assertEqual(list(mock), []) |
---|
525 | n/a | self.assertRaises(TypeError, int, mock) |
---|
526 | n/a | self.assertRaises(TypeError, int, mock.attr) |
---|
527 | n/a | self.assertEqual(list(mock), []) |
---|
528 | n/a | |
---|
529 | n/a | self.assertIsInstance(mock['foo'], MagicMock) |
---|
530 | n/a | self.assertIsInstance(mock.attr['foo'], MagicMock) |
---|
531 | n/a | |
---|
532 | n/a | |
---|
533 | n/a | def test_spec_set(self): |
---|
534 | n/a | class Sub(SomeClass): |
---|
535 | n/a | attr = SomeClass() |
---|
536 | n/a | |
---|
537 | n/a | for spec in (Sub, Sub()): |
---|
538 | n/a | mock = create_autospec(spec, spec_set=True) |
---|
539 | n/a | self._check_someclass_mock(mock) |
---|
540 | n/a | |
---|
541 | n/a | self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar') |
---|
542 | n/a | self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar') |
---|
543 | n/a | |
---|
544 | n/a | |
---|
545 | n/a | def test_descriptors(self): |
---|
546 | n/a | class Foo(object): |
---|
547 | n/a | @classmethod |
---|
548 | n/a | def f(cls, a, b): |
---|
549 | n/a | pass |
---|
550 | n/a | @staticmethod |
---|
551 | n/a | def g(a, b): |
---|
552 | n/a | pass |
---|
553 | n/a | |
---|
554 | n/a | class Bar(Foo): |
---|
555 | n/a | pass |
---|
556 | n/a | |
---|
557 | n/a | class Baz(SomeClass, Bar): |
---|
558 | n/a | pass |
---|
559 | n/a | |
---|
560 | n/a | for spec in (Foo, Foo(), Bar, Bar(), Baz, Baz()): |
---|
561 | n/a | mock = create_autospec(spec) |
---|
562 | n/a | mock.f(1, 2) |
---|
563 | n/a | mock.f.assert_called_once_with(1, 2) |
---|
564 | n/a | |
---|
565 | n/a | mock.g(3, 4) |
---|
566 | n/a | mock.g.assert_called_once_with(3, 4) |
---|
567 | n/a | |
---|
568 | n/a | |
---|
569 | n/a | def test_recursive(self): |
---|
570 | n/a | class A(object): |
---|
571 | n/a | def a(self): |
---|
572 | n/a | pass |
---|
573 | n/a | foo = 'foo bar baz' |
---|
574 | n/a | bar = foo |
---|
575 | n/a | |
---|
576 | n/a | A.B = A |
---|
577 | n/a | mock = create_autospec(A) |
---|
578 | n/a | |
---|
579 | n/a | mock() |
---|
580 | n/a | self.assertFalse(mock.B.called) |
---|
581 | n/a | |
---|
582 | n/a | mock.a() |
---|
583 | n/a | mock.B.a() |
---|
584 | n/a | self.assertEqual(mock.method_calls, [call.a(), call.B.a()]) |
---|
585 | n/a | |
---|
586 | n/a | self.assertIs(A.foo, A.bar) |
---|
587 | n/a | self.assertIsNot(mock.foo, mock.bar) |
---|
588 | n/a | mock.foo.lower() |
---|
589 | n/a | self.assertRaises(AssertionError, mock.bar.lower.assert_called_with) |
---|
590 | n/a | |
---|
591 | n/a | |
---|
592 | n/a | def test_spec_inheritance_for_classes(self): |
---|
593 | n/a | class Foo(object): |
---|
594 | n/a | def a(self, x): |
---|
595 | n/a | pass |
---|
596 | n/a | class Bar(object): |
---|
597 | n/a | def f(self, y): |
---|
598 | n/a | pass |
---|
599 | n/a | |
---|
600 | n/a | class_mock = create_autospec(Foo) |
---|
601 | n/a | |
---|
602 | n/a | self.assertIsNot(class_mock, class_mock()) |
---|
603 | n/a | |
---|
604 | n/a | for this_mock in class_mock, class_mock(): |
---|
605 | n/a | this_mock.a(x=5) |
---|
606 | n/a | this_mock.a.assert_called_with(x=5) |
---|
607 | n/a | this_mock.a.assert_called_with(5) |
---|
608 | n/a | self.assertRaises(TypeError, this_mock.a, 'foo', 'bar') |
---|
609 | n/a | self.assertRaises(AttributeError, getattr, this_mock, 'b') |
---|
610 | n/a | |
---|
611 | n/a | instance_mock = create_autospec(Foo()) |
---|
612 | n/a | instance_mock.a(5) |
---|
613 | n/a | instance_mock.a.assert_called_with(5) |
---|
614 | n/a | instance_mock.a.assert_called_with(x=5) |
---|
615 | n/a | self.assertRaises(TypeError, instance_mock.a, 'foo', 'bar') |
---|
616 | n/a | self.assertRaises(AttributeError, getattr, instance_mock, 'b') |
---|
617 | n/a | |
---|
618 | n/a | # The return value isn't isn't callable |
---|
619 | n/a | self.assertRaises(TypeError, instance_mock) |
---|
620 | n/a | |
---|
621 | n/a | instance_mock.Bar.f(6) |
---|
622 | n/a | instance_mock.Bar.f.assert_called_with(6) |
---|
623 | n/a | instance_mock.Bar.f.assert_called_with(y=6) |
---|
624 | n/a | self.assertRaises(AttributeError, getattr, instance_mock.Bar, 'g') |
---|
625 | n/a | |
---|
626 | n/a | instance_mock.Bar().f(6) |
---|
627 | n/a | instance_mock.Bar().f.assert_called_with(6) |
---|
628 | n/a | instance_mock.Bar().f.assert_called_with(y=6) |
---|
629 | n/a | self.assertRaises(AttributeError, getattr, instance_mock.Bar(), 'g') |
---|
630 | n/a | |
---|
631 | n/a | |
---|
632 | n/a | def test_inherit(self): |
---|
633 | n/a | class Foo(object): |
---|
634 | n/a | a = 3 |
---|
635 | n/a | |
---|
636 | n/a | Foo.Foo = Foo |
---|
637 | n/a | |
---|
638 | n/a | # class |
---|
639 | n/a | mock = create_autospec(Foo) |
---|
640 | n/a | instance = mock() |
---|
641 | n/a | self.assertRaises(AttributeError, getattr, instance, 'b') |
---|
642 | n/a | |
---|
643 | n/a | attr_instance = mock.Foo() |
---|
644 | n/a | self.assertRaises(AttributeError, getattr, attr_instance, 'b') |
---|
645 | n/a | |
---|
646 | n/a | # instance |
---|
647 | n/a | mock = create_autospec(Foo()) |
---|
648 | n/a | self.assertRaises(AttributeError, getattr, mock, 'b') |
---|
649 | n/a | self.assertRaises(TypeError, mock) |
---|
650 | n/a | |
---|
651 | n/a | # attribute instance |
---|
652 | n/a | call_result = mock.Foo() |
---|
653 | n/a | self.assertRaises(AttributeError, getattr, call_result, 'b') |
---|
654 | n/a | |
---|
655 | n/a | |
---|
656 | n/a | def test_builtins(self): |
---|
657 | n/a | # used to fail with infinite recursion |
---|
658 | n/a | create_autospec(1) |
---|
659 | n/a | |
---|
660 | n/a | create_autospec(int) |
---|
661 | n/a | create_autospec('foo') |
---|
662 | n/a | create_autospec(str) |
---|
663 | n/a | create_autospec({}) |
---|
664 | n/a | create_autospec(dict) |
---|
665 | n/a | create_autospec([]) |
---|
666 | n/a | create_autospec(list) |
---|
667 | n/a | create_autospec(set()) |
---|
668 | n/a | create_autospec(set) |
---|
669 | n/a | create_autospec(1.0) |
---|
670 | n/a | create_autospec(float) |
---|
671 | n/a | create_autospec(1j) |
---|
672 | n/a | create_autospec(complex) |
---|
673 | n/a | create_autospec(False) |
---|
674 | n/a | create_autospec(True) |
---|
675 | n/a | |
---|
676 | n/a | |
---|
677 | n/a | def test_function(self): |
---|
678 | n/a | def f(a, b): |
---|
679 | n/a | pass |
---|
680 | n/a | |
---|
681 | n/a | mock = create_autospec(f) |
---|
682 | n/a | self.assertRaises(TypeError, mock) |
---|
683 | n/a | mock(1, 2) |
---|
684 | n/a | mock.assert_called_with(1, 2) |
---|
685 | n/a | mock.assert_called_with(1, b=2) |
---|
686 | n/a | mock.assert_called_with(a=1, b=2) |
---|
687 | n/a | |
---|
688 | n/a | f.f = f |
---|
689 | n/a | mock = create_autospec(f) |
---|
690 | n/a | self.assertRaises(TypeError, mock.f) |
---|
691 | n/a | mock.f(3, 4) |
---|
692 | n/a | mock.f.assert_called_with(3, 4) |
---|
693 | n/a | mock.f.assert_called_with(a=3, b=4) |
---|
694 | n/a | |
---|
695 | n/a | |
---|
696 | n/a | def test_skip_attributeerrors(self): |
---|
697 | n/a | class Raiser(object): |
---|
698 | n/a | def __get__(self, obj, type=None): |
---|
699 | n/a | if obj is None: |
---|
700 | n/a | raise AttributeError('Can only be accessed via an instance') |
---|
701 | n/a | |
---|
702 | n/a | class RaiserClass(object): |
---|
703 | n/a | raiser = Raiser() |
---|
704 | n/a | |
---|
705 | n/a | @staticmethod |
---|
706 | n/a | def existing(a, b): |
---|
707 | n/a | return a + b |
---|
708 | n/a | |
---|
709 | n/a | s = create_autospec(RaiserClass) |
---|
710 | n/a | self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3)) |
---|
711 | n/a | s.existing(1, 2) |
---|
712 | n/a | self.assertRaises(AttributeError, lambda: s.nonexisting) |
---|
713 | n/a | |
---|
714 | n/a | # check we can fetch the raiser attribute and it has no spec |
---|
715 | n/a | obj = s.raiser |
---|
716 | n/a | obj.foo, obj.bar |
---|
717 | n/a | |
---|
718 | n/a | |
---|
719 | n/a | def test_signature_class(self): |
---|
720 | n/a | class Foo(object): |
---|
721 | n/a | def __init__(self, a, b=3): |
---|
722 | n/a | pass |
---|
723 | n/a | |
---|
724 | n/a | mock = create_autospec(Foo) |
---|
725 | n/a | |
---|
726 | n/a | self.assertRaises(TypeError, mock) |
---|
727 | n/a | mock(1) |
---|
728 | n/a | mock.assert_called_once_with(1) |
---|
729 | n/a | mock.assert_called_once_with(a=1) |
---|
730 | n/a | self.assertRaises(AssertionError, mock.assert_called_once_with, 2) |
---|
731 | n/a | |
---|
732 | n/a | mock(4, 5) |
---|
733 | n/a | mock.assert_called_with(4, 5) |
---|
734 | n/a | mock.assert_called_with(a=4, b=5) |
---|
735 | n/a | self.assertRaises(AssertionError, mock.assert_called_with, a=5, b=4) |
---|
736 | n/a | |
---|
737 | n/a | |
---|
738 | n/a | def test_class_with_no_init(self): |
---|
739 | n/a | # this used to raise an exception |
---|
740 | n/a | # due to trying to get a signature from object.__init__ |
---|
741 | n/a | class Foo(object): |
---|
742 | n/a | pass |
---|
743 | n/a | create_autospec(Foo) |
---|
744 | n/a | |
---|
745 | n/a | |
---|
746 | n/a | def test_signature_callable(self): |
---|
747 | n/a | class Callable(object): |
---|
748 | n/a | def __init__(self, x, y): |
---|
749 | n/a | pass |
---|
750 | n/a | def __call__(self, a): |
---|
751 | n/a | pass |
---|
752 | n/a | |
---|
753 | n/a | mock = create_autospec(Callable) |
---|
754 | n/a | mock(1, 2) |
---|
755 | n/a | mock.assert_called_once_with(1, 2) |
---|
756 | n/a | mock.assert_called_once_with(x=1, y=2) |
---|
757 | n/a | self.assertRaises(TypeError, mock, 'a') |
---|
758 | n/a | |
---|
759 | n/a | instance = mock(1, 2) |
---|
760 | n/a | self.assertRaises(TypeError, instance) |
---|
761 | n/a | instance(a='a') |
---|
762 | n/a | instance.assert_called_once_with('a') |
---|
763 | n/a | instance.assert_called_once_with(a='a') |
---|
764 | n/a | instance('a') |
---|
765 | n/a | instance.assert_called_with('a') |
---|
766 | n/a | instance.assert_called_with(a='a') |
---|
767 | n/a | |
---|
768 | n/a | mock = create_autospec(Callable(1, 2)) |
---|
769 | n/a | mock(a='a') |
---|
770 | n/a | mock.assert_called_once_with(a='a') |
---|
771 | n/a | self.assertRaises(TypeError, mock) |
---|
772 | n/a | mock('a') |
---|
773 | n/a | mock.assert_called_with('a') |
---|
774 | n/a | |
---|
775 | n/a | |
---|
776 | n/a | def test_signature_noncallable(self): |
---|
777 | n/a | class NonCallable(object): |
---|
778 | n/a | def __init__(self): |
---|
779 | n/a | pass |
---|
780 | n/a | |
---|
781 | n/a | mock = create_autospec(NonCallable) |
---|
782 | n/a | instance = mock() |
---|
783 | n/a | mock.assert_called_once_with() |
---|
784 | n/a | self.assertRaises(TypeError, mock, 'a') |
---|
785 | n/a | self.assertRaises(TypeError, instance) |
---|
786 | n/a | self.assertRaises(TypeError, instance, 'a') |
---|
787 | n/a | |
---|
788 | n/a | mock = create_autospec(NonCallable()) |
---|
789 | n/a | self.assertRaises(TypeError, mock) |
---|
790 | n/a | self.assertRaises(TypeError, mock, 'a') |
---|
791 | n/a | |
---|
792 | n/a | |
---|
793 | n/a | def test_create_autospec_none(self): |
---|
794 | n/a | class Foo(object): |
---|
795 | n/a | bar = None |
---|
796 | n/a | |
---|
797 | n/a | mock = create_autospec(Foo) |
---|
798 | n/a | none = mock.bar |
---|
799 | n/a | self.assertNotIsInstance(none, type(None)) |
---|
800 | n/a | |
---|
801 | n/a | none.foo() |
---|
802 | n/a | none.foo.assert_called_once_with() |
---|
803 | n/a | |
---|
804 | n/a | |
---|
805 | n/a | def test_autospec_functions_with_self_in_odd_place(self): |
---|
806 | n/a | class Foo(object): |
---|
807 | n/a | def f(a, self): |
---|
808 | n/a | pass |
---|
809 | n/a | |
---|
810 | n/a | a = create_autospec(Foo) |
---|
811 | n/a | a.f(10) |
---|
812 | n/a | a.f.assert_called_with(10) |
---|
813 | n/a | a.f.assert_called_with(self=10) |
---|
814 | n/a | a.f(self=10) |
---|
815 | n/a | a.f.assert_called_with(10) |
---|
816 | n/a | a.f.assert_called_with(self=10) |
---|
817 | n/a | |
---|
818 | n/a | |
---|
819 | n/a | def test_autospec_data_descriptor(self): |
---|
820 | n/a | class Descriptor(object): |
---|
821 | n/a | def __init__(self, value): |
---|
822 | n/a | self.value = value |
---|
823 | n/a | |
---|
824 | n/a | def __get__(self, obj, cls=None): |
---|
825 | n/a | if obj is None: |
---|
826 | n/a | return self |
---|
827 | n/a | return self.value |
---|
828 | n/a | |
---|
829 | n/a | def __set__(self, obj, value): |
---|
830 | n/a | pass |
---|
831 | n/a | |
---|
832 | n/a | class MyProperty(property): |
---|
833 | n/a | pass |
---|
834 | n/a | |
---|
835 | n/a | class Foo(object): |
---|
836 | n/a | __slots__ = ['slot'] |
---|
837 | n/a | |
---|
838 | n/a | @property |
---|
839 | n/a | def prop(self): |
---|
840 | n/a | return 3 |
---|
841 | n/a | |
---|
842 | n/a | @MyProperty |
---|
843 | n/a | def subprop(self): |
---|
844 | n/a | return 4 |
---|
845 | n/a | |
---|
846 | n/a | desc = Descriptor(42) |
---|
847 | n/a | |
---|
848 | n/a | foo = create_autospec(Foo) |
---|
849 | n/a | |
---|
850 | n/a | def check_data_descriptor(mock_attr): |
---|
851 | n/a | # Data descriptors don't have a spec. |
---|
852 | n/a | self.assertIsInstance(mock_attr, MagicMock) |
---|
853 | n/a | mock_attr(1, 2, 3) |
---|
854 | n/a | mock_attr.abc(4, 5, 6) |
---|
855 | n/a | mock_attr.assert_called_once_with(1, 2, 3) |
---|
856 | n/a | mock_attr.abc.assert_called_once_with(4, 5, 6) |
---|
857 | n/a | |
---|
858 | n/a | # property |
---|
859 | n/a | check_data_descriptor(foo.prop) |
---|
860 | n/a | # property subclass |
---|
861 | n/a | check_data_descriptor(foo.subprop) |
---|
862 | n/a | # class __slot__ |
---|
863 | n/a | check_data_descriptor(foo.slot) |
---|
864 | n/a | # plain data descriptor |
---|
865 | n/a | check_data_descriptor(foo.desc) |
---|
866 | n/a | |
---|
867 | n/a | |
---|
868 | n/a | class TestCallList(unittest.TestCase): |
---|
869 | n/a | |
---|
870 | n/a | def test_args_list_contains_call_list(self): |
---|
871 | n/a | mock = Mock() |
---|
872 | n/a | self.assertIsInstance(mock.call_args_list, _CallList) |
---|
873 | n/a | |
---|
874 | n/a | mock(1, 2) |
---|
875 | n/a | mock(a=3) |
---|
876 | n/a | mock(3, 4) |
---|
877 | n/a | mock(b=6) |
---|
878 | n/a | |
---|
879 | n/a | for kall in call(1, 2), call(a=3), call(3, 4), call(b=6): |
---|
880 | n/a | self.assertIn(kall, mock.call_args_list) |
---|
881 | n/a | |
---|
882 | n/a | calls = [call(a=3), call(3, 4)] |
---|
883 | n/a | self.assertIn(calls, mock.call_args_list) |
---|
884 | n/a | calls = [call(1, 2), call(a=3)] |
---|
885 | n/a | self.assertIn(calls, mock.call_args_list) |
---|
886 | n/a | calls = [call(3, 4), call(b=6)] |
---|
887 | n/a | self.assertIn(calls, mock.call_args_list) |
---|
888 | n/a | calls = [call(3, 4)] |
---|
889 | n/a | self.assertIn(calls, mock.call_args_list) |
---|
890 | n/a | |
---|
891 | n/a | self.assertNotIn(call('fish'), mock.call_args_list) |
---|
892 | n/a | self.assertNotIn([call('fish')], mock.call_args_list) |
---|
893 | n/a | |
---|
894 | n/a | |
---|
895 | n/a | def test_call_list_str(self): |
---|
896 | n/a | mock = Mock() |
---|
897 | n/a | mock(1, 2) |
---|
898 | n/a | mock.foo(a=3) |
---|
899 | n/a | mock.foo.bar().baz('fish', cat='dog') |
---|
900 | n/a | |
---|
901 | n/a | expected = ( |
---|
902 | n/a | "[call(1, 2),\n" |
---|
903 | n/a | " call.foo(a=3),\n" |
---|
904 | n/a | " call.foo.bar(),\n" |
---|
905 | n/a | " call.foo.bar().baz('fish', cat='dog')]" |
---|
906 | n/a | ) |
---|
907 | n/a | self.assertEqual(str(mock.mock_calls), expected) |
---|
908 | n/a | |
---|
909 | n/a | |
---|
910 | n/a | def test_propertymock(self): |
---|
911 | n/a | p = patch('%s.SomeClass.one' % __name__, new_callable=PropertyMock) |
---|
912 | n/a | mock = p.start() |
---|
913 | n/a | try: |
---|
914 | n/a | SomeClass.one |
---|
915 | n/a | mock.assert_called_once_with() |
---|
916 | n/a | |
---|
917 | n/a | s = SomeClass() |
---|
918 | n/a | s.one |
---|
919 | n/a | mock.assert_called_with() |
---|
920 | n/a | self.assertEqual(mock.mock_calls, [call(), call()]) |
---|
921 | n/a | |
---|
922 | n/a | s.one = 3 |
---|
923 | n/a | self.assertEqual(mock.mock_calls, [call(), call(), call(3)]) |
---|
924 | n/a | finally: |
---|
925 | n/a | p.stop() |
---|
926 | n/a | |
---|
927 | n/a | |
---|
928 | n/a | def test_propertymock_returnvalue(self): |
---|
929 | n/a | m = MagicMock() |
---|
930 | n/a | p = PropertyMock() |
---|
931 | n/a | type(m).foo = p |
---|
932 | n/a | |
---|
933 | n/a | returned = m.foo |
---|
934 | n/a | p.assert_called_once_with() |
---|
935 | n/a | self.assertIsInstance(returned, MagicMock) |
---|
936 | n/a | self.assertNotIsInstance(returned, PropertyMock) |
---|
937 | n/a | |
---|
938 | n/a | |
---|
939 | n/a | if __name__ == '__main__': |
---|
940 | n/a | unittest.main() |
---|