|
1 # Copyright (C) 2007-2012 Michael Foord & the mock team |
|
2 # E-mail: fuzzyman AT voidspace DOT org DOT uk |
|
3 # http://www.voidspace.org.uk/python/mock/ |
|
4 |
|
5 import os |
|
6 import sys |
|
7 |
|
8 from tests import support |
|
9 from tests.support import unittest2, inPy3k, SomeClass, is_instance, callable |
|
10 |
|
11 from mock import ( |
|
12 NonCallableMock, CallableMixin, patch, sentinel, |
|
13 MagicMock, Mock, NonCallableMagicMock, patch, _patch, |
|
14 DEFAULT, call, _get_target |
|
15 ) |
|
16 |
|
17 builtin_string = '__builtin__' |
|
18 if inPy3k: |
|
19 builtin_string = 'builtins' |
|
20 unicode = str |
|
21 |
|
22 PTModule = sys.modules[__name__] |
|
23 MODNAME = '%s.PTModule' % __name__ |
|
24 |
|
25 |
|
26 def _get_proxy(obj, get_only=True): |
|
27 class Proxy(object): |
|
28 def __getattr__(self, name): |
|
29 return getattr(obj, name) |
|
30 if not get_only: |
|
31 def __setattr__(self, name, value): |
|
32 setattr(obj, name, value) |
|
33 def __delattr__(self, name): |
|
34 delattr(obj, name) |
|
35 Proxy.__setattr__ = __setattr__ |
|
36 Proxy.__delattr__ = __delattr__ |
|
37 return Proxy() |
|
38 |
|
39 |
|
40 # for use in the test |
|
41 something = sentinel.Something |
|
42 something_else = sentinel.SomethingElse |
|
43 |
|
44 |
|
45 class Foo(object): |
|
46 def __init__(self, a): |
|
47 pass |
|
48 def f(self, a): |
|
49 pass |
|
50 def g(self): |
|
51 pass |
|
52 foo = 'bar' |
|
53 |
|
54 class Bar(object): |
|
55 def a(self): |
|
56 pass |
|
57 |
|
58 foo_name = '%s.Foo' % __name__ |
|
59 |
|
60 |
|
61 def function(a, b=Foo): |
|
62 pass |
|
63 |
|
64 |
|
65 class Container(object): |
|
66 def __init__(self): |
|
67 self.values = {} |
|
68 |
|
69 def __getitem__(self, name): |
|
70 return self.values[name] |
|
71 |
|
72 def __setitem__(self, name, value): |
|
73 self.values[name] = value |
|
74 |
|
75 def __delitem__(self, name): |
|
76 del self.values[name] |
|
77 |
|
78 def __iter__(self): |
|
79 return iter(self.values) |
|
80 |
|
81 |
|
82 |
|
83 class PatchTest(unittest2.TestCase): |
|
84 |
|
85 def assertNotCallable(self, obj, magic=True): |
|
86 MockClass = NonCallableMagicMock |
|
87 if not magic: |
|
88 MockClass = NonCallableMock |
|
89 |
|
90 self.assertRaises(TypeError, obj) |
|
91 self.assertTrue(is_instance(obj, MockClass)) |
|
92 self.assertFalse(is_instance(obj, CallableMixin)) |
|
93 |
|
94 |
|
95 def test_single_patchobject(self): |
|
96 class Something(object): |
|
97 attribute = sentinel.Original |
|
98 |
|
99 @patch.object(Something, 'attribute', sentinel.Patched) |
|
100 def test(): |
|
101 self.assertEqual(Something.attribute, sentinel.Patched, "unpatched") |
|
102 |
|
103 test() |
|
104 self.assertEqual(Something.attribute, sentinel.Original, |
|
105 "patch not restored") |
|
106 |
|
107 |
|
108 def test_patchobject_with_none(self): |
|
109 class Something(object): |
|
110 attribute = sentinel.Original |
|
111 |
|
112 @patch.object(Something, 'attribute', None) |
|
113 def test(): |
|
114 self.assertIsNone(Something.attribute, "unpatched") |
|
115 |
|
116 test() |
|
117 self.assertEqual(Something.attribute, sentinel.Original, |
|
118 "patch not restored") |
|
119 |
|
120 |
|
121 def test_multiple_patchobject(self): |
|
122 class Something(object): |
|
123 attribute = sentinel.Original |
|
124 next_attribute = sentinel.Original2 |
|
125 |
|
126 @patch.object(Something, 'attribute', sentinel.Patched) |
|
127 @patch.object(Something, 'next_attribute', sentinel.Patched2) |
|
128 def test(): |
|
129 self.assertEqual(Something.attribute, sentinel.Patched, |
|
130 "unpatched") |
|
131 self.assertEqual(Something.next_attribute, sentinel.Patched2, |
|
132 "unpatched") |
|
133 |
|
134 test() |
|
135 self.assertEqual(Something.attribute, sentinel.Original, |
|
136 "patch not restored") |
|
137 self.assertEqual(Something.next_attribute, sentinel.Original2, |
|
138 "patch not restored") |
|
139 |
|
140 |
|
141 def test_object_lookup_is_quite_lazy(self): |
|
142 global something |
|
143 original = something |
|
144 @patch('%s.something' % __name__, sentinel.Something2) |
|
145 def test(): |
|
146 pass |
|
147 |
|
148 try: |
|
149 something = sentinel.replacement_value |
|
150 test() |
|
151 self.assertEqual(something, sentinel.replacement_value) |
|
152 finally: |
|
153 something = original |
|
154 |
|
155 |
|
156 def test_patch(self): |
|
157 @patch('%s.something' % __name__, sentinel.Something2) |
|
158 def test(): |
|
159 self.assertEqual(PTModule.something, sentinel.Something2, |
|
160 "unpatched") |
|
161 |
|
162 test() |
|
163 self.assertEqual(PTModule.something, sentinel.Something, |
|
164 "patch not restored") |
|
165 |
|
166 @patch('%s.something' % __name__, sentinel.Something2) |
|
167 @patch('%s.something_else' % __name__, sentinel.SomethingElse) |
|
168 def test(): |
|
169 self.assertEqual(PTModule.something, sentinel.Something2, |
|
170 "unpatched") |
|
171 self.assertEqual(PTModule.something_else, sentinel.SomethingElse, |
|
172 "unpatched") |
|
173 |
|
174 self.assertEqual(PTModule.something, sentinel.Something, |
|
175 "patch not restored") |
|
176 self.assertEqual(PTModule.something_else, sentinel.SomethingElse, |
|
177 "patch not restored") |
|
178 |
|
179 # Test the patching and restoring works a second time |
|
180 test() |
|
181 |
|
182 self.assertEqual(PTModule.something, sentinel.Something, |
|
183 "patch not restored") |
|
184 self.assertEqual(PTModule.something_else, sentinel.SomethingElse, |
|
185 "patch not restored") |
|
186 |
|
187 mock = Mock() |
|
188 mock.return_value = sentinel.Handle |
|
189 @patch('%s.open' % builtin_string, mock) |
|
190 def test(): |
|
191 self.assertEqual(open('filename', 'r'), sentinel.Handle, |
|
192 "open not patched") |
|
193 test() |
|
194 test() |
|
195 |
|
196 self.assertNotEqual(open, mock, "patch not restored") |
|
197 |
|
198 |
|
199 def test_patch_class_attribute(self): |
|
200 @patch('%s.SomeClass.class_attribute' % __name__, |
|
201 sentinel.ClassAttribute) |
|
202 def test(): |
|
203 self.assertEqual(PTModule.SomeClass.class_attribute, |
|
204 sentinel.ClassAttribute, "unpatched") |
|
205 test() |
|
206 |
|
207 self.assertIsNone(PTModule.SomeClass.class_attribute, |
|
208 "patch not restored") |
|
209 |
|
210 |
|
211 def test_patchobject_with_default_mock(self): |
|
212 class Test(object): |
|
213 something = sentinel.Original |
|
214 something2 = sentinel.Original2 |
|
215 |
|
216 @patch.object(Test, 'something') |
|
217 def test(mock): |
|
218 self.assertEqual(mock, Test.something, |
|
219 "Mock not passed into test function") |
|
220 self.assertIsInstance(mock, MagicMock, |
|
221 "patch with two arguments did not create a mock") |
|
222 |
|
223 test() |
|
224 |
|
225 @patch.object(Test, 'something') |
|
226 @patch.object(Test, 'something2') |
|
227 def test(this1, this2, mock1, mock2): |
|
228 self.assertEqual(this1, sentinel.this1, |
|
229 "Patched function didn't receive initial argument") |
|
230 self.assertEqual(this2, sentinel.this2, |
|
231 "Patched function didn't receive second argument") |
|
232 self.assertEqual(mock1, Test.something2, |
|
233 "Mock not passed into test function") |
|
234 self.assertEqual(mock2, Test.something, |
|
235 "Second Mock not passed into test function") |
|
236 self.assertIsInstance(mock2, MagicMock, |
|
237 "patch with two arguments did not create a mock") |
|
238 self.assertIsInstance(mock2, MagicMock, |
|
239 "patch with two arguments did not create a mock") |
|
240 |
|
241 # A hack to test that new mocks are passed the second time |
|
242 self.assertNotEqual(outerMock1, mock1, "unexpected value for mock1") |
|
243 self.assertNotEqual(outerMock2, mock2, "unexpected value for mock1") |
|
244 return mock1, mock2 |
|
245 |
|
246 outerMock1 = outerMock2 = None |
|
247 outerMock1, outerMock2 = test(sentinel.this1, sentinel.this2) |
|
248 |
|
249 # Test that executing a second time creates new mocks |
|
250 test(sentinel.this1, sentinel.this2) |
|
251 |
|
252 |
|
253 def test_patch_with_spec(self): |
|
254 @patch('%s.SomeClass' % __name__, spec=SomeClass) |
|
255 def test(MockSomeClass): |
|
256 self.assertEqual(SomeClass, MockSomeClass) |
|
257 self.assertTrue(is_instance(SomeClass.wibble, MagicMock)) |
|
258 self.assertRaises(AttributeError, lambda: SomeClass.not_wibble) |
|
259 |
|
260 test() |
|
261 |
|
262 |
|
263 def test_patchobject_with_spec(self): |
|
264 @patch.object(SomeClass, 'class_attribute', spec=SomeClass) |
|
265 def test(MockAttribute): |
|
266 self.assertEqual(SomeClass.class_attribute, MockAttribute) |
|
267 self.assertTrue(is_instance(SomeClass.class_attribute.wibble, |
|
268 MagicMock)) |
|
269 self.assertRaises(AttributeError, |
|
270 lambda: SomeClass.class_attribute.not_wibble) |
|
271 |
|
272 test() |
|
273 |
|
274 |
|
275 def test_patch_with_spec_as_list(self): |
|
276 @patch('%s.SomeClass' % __name__, spec=['wibble']) |
|
277 def test(MockSomeClass): |
|
278 self.assertEqual(SomeClass, MockSomeClass) |
|
279 self.assertTrue(is_instance(SomeClass.wibble, MagicMock)) |
|
280 self.assertRaises(AttributeError, lambda: SomeClass.not_wibble) |
|
281 |
|
282 test() |
|
283 |
|
284 |
|
285 def test_patchobject_with_spec_as_list(self): |
|
286 @patch.object(SomeClass, 'class_attribute', spec=['wibble']) |
|
287 def test(MockAttribute): |
|
288 self.assertEqual(SomeClass.class_attribute, MockAttribute) |
|
289 self.assertTrue(is_instance(SomeClass.class_attribute.wibble, |
|
290 MagicMock)) |
|
291 self.assertRaises(AttributeError, |
|
292 lambda: SomeClass.class_attribute.not_wibble) |
|
293 |
|
294 test() |
|
295 |
|
296 |
|
297 def test_nested_patch_with_spec_as_list(self): |
|
298 # regression test for nested decorators |
|
299 @patch('%s.open' % builtin_string) |
|
300 @patch('%s.SomeClass' % __name__, spec=['wibble']) |
|
301 def test(MockSomeClass, MockOpen): |
|
302 self.assertEqual(SomeClass, MockSomeClass) |
|
303 self.assertTrue(is_instance(SomeClass.wibble, MagicMock)) |
|
304 self.assertRaises(AttributeError, lambda: SomeClass.not_wibble) |
|
305 test() |
|
306 |
|
307 |
|
308 def test_patch_with_spec_as_boolean(self): |
|
309 @patch('%s.SomeClass' % __name__, spec=True) |
|
310 def test(MockSomeClass): |
|
311 self.assertEqual(SomeClass, MockSomeClass) |
|
312 # Should not raise attribute error |
|
313 MockSomeClass.wibble |
|
314 |
|
315 self.assertRaises(AttributeError, lambda: MockSomeClass.not_wibble) |
|
316 |
|
317 test() |
|
318 |
|
319 |
|
320 def test_patch_object_with_spec_as_boolean(self): |
|
321 @patch.object(PTModule, 'SomeClass', spec=True) |
|
322 def test(MockSomeClass): |
|
323 self.assertEqual(SomeClass, MockSomeClass) |
|
324 # Should not raise attribute error |
|
325 MockSomeClass.wibble |
|
326 |
|
327 self.assertRaises(AttributeError, lambda: MockSomeClass.not_wibble) |
|
328 |
|
329 test() |
|
330 |
|
331 |
|
332 def test_patch_class_acts_with_spec_is_inherited(self): |
|
333 @patch('%s.SomeClass' % __name__, spec=True) |
|
334 def test(MockSomeClass): |
|
335 self.assertTrue(is_instance(MockSomeClass, MagicMock)) |
|
336 instance = MockSomeClass() |
|
337 self.assertNotCallable(instance) |
|
338 # Should not raise attribute error |
|
339 instance.wibble |
|
340 |
|
341 self.assertRaises(AttributeError, lambda: instance.not_wibble) |
|
342 |
|
343 test() |
|
344 |
|
345 |
|
346 def test_patch_with_create_mocks_non_existent_attributes(self): |
|
347 @patch('%s.frooble' % builtin_string, sentinel.Frooble, create=True) |
|
348 def test(): |
|
349 self.assertEqual(frooble, sentinel.Frooble) |
|
350 |
|
351 test() |
|
352 self.assertRaises(NameError, lambda: frooble) |
|
353 |
|
354 |
|
355 def test_patchobject_with_create_mocks_non_existent_attributes(self): |
|
356 @patch.object(SomeClass, 'frooble', sentinel.Frooble, create=True) |
|
357 def test(): |
|
358 self.assertEqual(SomeClass.frooble, sentinel.Frooble) |
|
359 |
|
360 test() |
|
361 self.assertFalse(hasattr(SomeClass, 'frooble')) |
|
362 |
|
363 |
|
364 def test_patch_wont_create_by_default(self): |
|
365 try: |
|
366 @patch('%s.frooble' % builtin_string, sentinel.Frooble) |
|
367 def test(): |
|
368 self.assertEqual(frooble, sentinel.Frooble) |
|
369 |
|
370 test() |
|
371 except AttributeError: |
|
372 pass |
|
373 else: |
|
374 self.fail('Patching non existent attributes should fail') |
|
375 |
|
376 self.assertRaises(NameError, lambda: frooble) |
|
377 |
|
378 |
|
379 def test_patchobject_wont_create_by_default(self): |
|
380 try: |
|
381 @patch.object(SomeClass, 'frooble', sentinel.Frooble) |
|
382 def test(): |
|
383 self.fail('Patching non existent attributes should fail') |
|
384 |
|
385 test() |
|
386 except AttributeError: |
|
387 pass |
|
388 else: |
|
389 self.fail('Patching non existent attributes should fail') |
|
390 self.assertFalse(hasattr(SomeClass, 'frooble')) |
|
391 |
|
392 |
|
393 def test_patch_with_static_methods(self): |
|
394 class Foo(object): |
|
395 @staticmethod |
|
396 def woot(): |
|
397 return sentinel.Static |
|
398 |
|
399 @patch.object(Foo, 'woot', staticmethod(lambda: sentinel.Patched)) |
|
400 def anonymous(): |
|
401 self.assertEqual(Foo.woot(), sentinel.Patched) |
|
402 anonymous() |
|
403 |
|
404 self.assertEqual(Foo.woot(), sentinel.Static) |
|
405 |
|
406 |
|
407 def test_patch_local(self): |
|
408 foo = sentinel.Foo |
|
409 @patch.object(sentinel, 'Foo', 'Foo') |
|
410 def anonymous(): |
|
411 self.assertEqual(sentinel.Foo, 'Foo') |
|
412 anonymous() |
|
413 |
|
414 self.assertEqual(sentinel.Foo, foo) |
|
415 |
|
416 |
|
417 def test_patch_slots(self): |
|
418 class Foo(object): |
|
419 __slots__ = ('Foo',) |
|
420 |
|
421 foo = Foo() |
|
422 foo.Foo = sentinel.Foo |
|
423 |
|
424 @patch.object(foo, 'Foo', 'Foo') |
|
425 def anonymous(): |
|
426 self.assertEqual(foo.Foo, 'Foo') |
|
427 anonymous() |
|
428 |
|
429 self.assertEqual(foo.Foo, sentinel.Foo) |
|
430 |
|
431 |
|
432 def test_patchobject_class_decorator(self): |
|
433 class Something(object): |
|
434 attribute = sentinel.Original |
|
435 |
|
436 class Foo(object): |
|
437 def test_method(other_self): |
|
438 self.assertEqual(Something.attribute, sentinel.Patched, |
|
439 "unpatched") |
|
440 def not_test_method(other_self): |
|
441 self.assertEqual(Something.attribute, sentinel.Original, |
|
442 "non-test method patched") |
|
443 |
|
444 Foo = patch.object(Something, 'attribute', sentinel.Patched)(Foo) |
|
445 |
|
446 f = Foo() |
|
447 f.test_method() |
|
448 f.not_test_method() |
|
449 |
|
450 self.assertEqual(Something.attribute, sentinel.Original, |
|
451 "patch not restored") |
|
452 |
|
453 |
|
454 def test_patch_class_decorator(self): |
|
455 class Something(object): |
|
456 attribute = sentinel.Original |
|
457 |
|
458 class Foo(object): |
|
459 def test_method(other_self, mock_something): |
|
460 self.assertEqual(PTModule.something, mock_something, |
|
461 "unpatched") |
|
462 def not_test_method(other_self): |
|
463 self.assertEqual(PTModule.something, sentinel.Something, |
|
464 "non-test method patched") |
|
465 Foo = patch('%s.something' % __name__)(Foo) |
|
466 |
|
467 f = Foo() |
|
468 f.test_method() |
|
469 f.not_test_method() |
|
470 |
|
471 self.assertEqual(Something.attribute, sentinel.Original, |
|
472 "patch not restored") |
|
473 self.assertEqual(PTModule.something, sentinel.Something, |
|
474 "patch not restored") |
|
475 |
|
476 |
|
477 def test_patchobject_twice(self): |
|
478 class Something(object): |
|
479 attribute = sentinel.Original |
|
480 next_attribute = sentinel.Original2 |
|
481 |
|
482 @patch.object(Something, 'attribute', sentinel.Patched) |
|
483 @patch.object(Something, 'attribute', sentinel.Patched) |
|
484 def test(): |
|
485 self.assertEqual(Something.attribute, sentinel.Patched, "unpatched") |
|
486 |
|
487 test() |
|
488 |
|
489 self.assertEqual(Something.attribute, sentinel.Original, |
|
490 "patch not restored") |
|
491 |
|
492 |
|
493 def test_patch_dict(self): |
|
494 foo = {'initial': object(), 'other': 'something'} |
|
495 original = foo.copy() |
|
496 |
|
497 @patch.dict(foo) |
|
498 def test(): |
|
499 foo['a'] = 3 |
|
500 del foo['initial'] |
|
501 foo['other'] = 'something else' |
|
502 |
|
503 test() |
|
504 |
|
505 self.assertEqual(foo, original) |
|
506 |
|
507 @patch.dict(foo, {'a': 'b'}) |
|
508 def test(): |
|
509 self.assertEqual(len(foo), 3) |
|
510 self.assertEqual(foo['a'], 'b') |
|
511 |
|
512 test() |
|
513 |
|
514 self.assertEqual(foo, original) |
|
515 |
|
516 @patch.dict(foo, [('a', 'b')]) |
|
517 def test(): |
|
518 self.assertEqual(len(foo), 3) |
|
519 self.assertEqual(foo['a'], 'b') |
|
520 |
|
521 test() |
|
522 |
|
523 self.assertEqual(foo, original) |
|
524 |
|
525 |
|
526 def test_patch_dict_with_container_object(self): |
|
527 foo = Container() |
|
528 foo['initial'] = object() |
|
529 foo['other'] = 'something' |
|
530 |
|
531 original = foo.values.copy() |
|
532 |
|
533 @patch.dict(foo) |
|
534 def test(): |
|
535 foo['a'] = 3 |
|
536 del foo['initial'] |
|
537 foo['other'] = 'something else' |
|
538 |
|
539 test() |
|
540 |
|
541 self.assertEqual(foo.values, original) |
|
542 |
|
543 @patch.dict(foo, {'a': 'b'}) |
|
544 def test(): |
|
545 self.assertEqual(len(foo.values), 3) |
|
546 self.assertEqual(foo['a'], 'b') |
|
547 |
|
548 test() |
|
549 |
|
550 self.assertEqual(foo.values, original) |
|
551 |
|
552 |
|
553 def test_patch_dict_with_clear(self): |
|
554 foo = {'initial': object(), 'other': 'something'} |
|
555 original = foo.copy() |
|
556 |
|
557 @patch.dict(foo, clear=True) |
|
558 def test(): |
|
559 self.assertEqual(foo, {}) |
|
560 foo['a'] = 3 |
|
561 foo['other'] = 'something else' |
|
562 |
|
563 test() |
|
564 |
|
565 self.assertEqual(foo, original) |
|
566 |
|
567 @patch.dict(foo, {'a': 'b'}, clear=True) |
|
568 def test(): |
|
569 self.assertEqual(foo, {'a': 'b'}) |
|
570 |
|
571 test() |
|
572 |
|
573 self.assertEqual(foo, original) |
|
574 |
|
575 @patch.dict(foo, [('a', 'b')], clear=True) |
|
576 def test(): |
|
577 self.assertEqual(foo, {'a': 'b'}) |
|
578 |
|
579 test() |
|
580 |
|
581 self.assertEqual(foo, original) |
|
582 |
|
583 |
|
584 def test_patch_dict_with_container_object_and_clear(self): |
|
585 foo = Container() |
|
586 foo['initial'] = object() |
|
587 foo['other'] = 'something' |
|
588 |
|
589 original = foo.values.copy() |
|
590 |
|
591 @patch.dict(foo, clear=True) |
|
592 def test(): |
|
593 self.assertEqual(foo.values, {}) |
|
594 foo['a'] = 3 |
|
595 foo['other'] = 'something else' |
|
596 |
|
597 test() |
|
598 |
|
599 self.assertEqual(foo.values, original) |
|
600 |
|
601 @patch.dict(foo, {'a': 'b'}, clear=True) |
|
602 def test(): |
|
603 self.assertEqual(foo.values, {'a': 'b'}) |
|
604 |
|
605 test() |
|
606 |
|
607 self.assertEqual(foo.values, original) |
|
608 |
|
609 |
|
610 def test_name_preserved(self): |
|
611 foo = {} |
|
612 |
|
613 @patch('%s.SomeClass' % __name__, object()) |
|
614 @patch('%s.SomeClass' % __name__, object(), autospec=True) |
|
615 @patch.object(SomeClass, object()) |
|
616 @patch.dict(foo) |
|
617 def some_name(): |
|
618 pass |
|
619 |
|
620 self.assertEqual(some_name.__name__, 'some_name') |
|
621 |
|
622 |
|
623 def test_patch_with_exception(self): |
|
624 foo = {} |
|
625 |
|
626 @patch.dict(foo, {'a': 'b'}) |
|
627 def test(): |
|
628 raise NameError('Konrad') |
|
629 try: |
|
630 test() |
|
631 except NameError: |
|
632 pass |
|
633 else: |
|
634 self.fail('NameError not raised by test') |
|
635 |
|
636 self.assertEqual(foo, {}) |
|
637 |
|
638 |
|
639 def test_patch_dict_with_string(self): |
|
640 @patch.dict('os.environ', {'konrad_delong': 'some value'}) |
|
641 def test(): |
|
642 self.assertIn('konrad_delong', os.environ) |
|
643 |
|
644 test() |
|
645 |
|
646 |
|
647 @unittest2.expectedFailure |
|
648 def test_patch_descriptor(self): |
|
649 # would be some effort to fix this - we could special case the |
|
650 # builtin descriptors: classmethod, property, staticmethod |
|
651 class Nothing(object): |
|
652 foo = None |
|
653 |
|
654 class Something(object): |
|
655 foo = {} |
|
656 |
|
657 @patch.object(Nothing, 'foo', 2) |
|
658 @classmethod |
|
659 def klass(cls): |
|
660 self.assertIs(cls, Something) |
|
661 |
|
662 @patch.object(Nothing, 'foo', 2) |
|
663 @staticmethod |
|
664 def static(arg): |
|
665 return arg |
|
666 |
|
667 @patch.dict(foo) |
|
668 @classmethod |
|
669 def klass_dict(cls): |
|
670 self.assertIs(cls, Something) |
|
671 |
|
672 @patch.dict(foo) |
|
673 @staticmethod |
|
674 def static_dict(arg): |
|
675 return arg |
|
676 |
|
677 # these will raise exceptions if patching descriptors is broken |
|
678 self.assertEqual(Something.static('f00'), 'f00') |
|
679 Something.klass() |
|
680 self.assertEqual(Something.static_dict('f00'), 'f00') |
|
681 Something.klass_dict() |
|
682 |
|
683 something = Something() |
|
684 self.assertEqual(something.static('f00'), 'f00') |
|
685 something.klass() |
|
686 self.assertEqual(something.static_dict('f00'), 'f00') |
|
687 something.klass_dict() |
|
688 |
|
689 |
|
690 def test_patch_spec_set(self): |
|
691 @patch('%s.SomeClass' % __name__, spec_set=SomeClass) |
|
692 def test(MockClass): |
|
693 MockClass.z = 'foo' |
|
694 |
|
695 self.assertRaises(AttributeError, test) |
|
696 |
|
697 @patch.object(support, 'SomeClass', spec_set=SomeClass) |
|
698 def test(MockClass): |
|
699 MockClass.z = 'foo' |
|
700 |
|
701 self.assertRaises(AttributeError, test) |
|
702 @patch('%s.SomeClass' % __name__, spec_set=True) |
|
703 def test(MockClass): |
|
704 MockClass.z = 'foo' |
|
705 |
|
706 self.assertRaises(AttributeError, test) |
|
707 |
|
708 @patch.object(support, 'SomeClass', spec_set=True) |
|
709 def test(MockClass): |
|
710 MockClass.z = 'foo' |
|
711 |
|
712 self.assertRaises(AttributeError, test) |
|
713 |
|
714 |
|
715 def test_spec_set_inherit(self): |
|
716 @patch('%s.SomeClass' % __name__, spec_set=True) |
|
717 def test(MockClass): |
|
718 instance = MockClass() |
|
719 instance.z = 'foo' |
|
720 |
|
721 self.assertRaises(AttributeError, test) |
|
722 |
|
723 |
|
724 def test_patch_start_stop(self): |
|
725 original = something |
|
726 patcher = patch('%s.something' % __name__) |
|
727 self.assertIs(something, original) |
|
728 mock = patcher.start() |
|
729 try: |
|
730 self.assertIsNot(mock, original) |
|
731 self.assertIs(something, mock) |
|
732 finally: |
|
733 patcher.stop() |
|
734 self.assertIs(something, original) |
|
735 |
|
736 |
|
737 def test_stop_without_start(self): |
|
738 patcher = patch(foo_name, 'bar', 3) |
|
739 |
|
740 # calling stop without start used to produce a very obscure error |
|
741 self.assertRaises(RuntimeError, patcher.stop) |
|
742 |
|
743 |
|
744 def test_patchobject_start_stop(self): |
|
745 original = something |
|
746 patcher = patch.object(PTModule, 'something', 'foo') |
|
747 self.assertIs(something, original) |
|
748 replaced = patcher.start() |
|
749 try: |
|
750 self.assertEqual(replaced, 'foo') |
|
751 self.assertIs(something, replaced) |
|
752 finally: |
|
753 patcher.stop() |
|
754 self.assertIs(something, original) |
|
755 |
|
756 |
|
757 def test_patch_dict_start_stop(self): |
|
758 d = {'foo': 'bar'} |
|
759 original = d.copy() |
|
760 patcher = patch.dict(d, [('spam', 'eggs')], clear=True) |
|
761 self.assertEqual(d, original) |
|
762 |
|
763 patcher.start() |
|
764 try: |
|
765 self.assertEqual(d, {'spam': 'eggs'}) |
|
766 finally: |
|
767 patcher.stop() |
|
768 self.assertEqual(d, original) |
|
769 |
|
770 |
|
771 def test_patch_dict_class_decorator(self): |
|
772 this = self |
|
773 d = {'spam': 'eggs'} |
|
774 original = d.copy() |
|
775 |
|
776 class Test(object): |
|
777 def test_first(self): |
|
778 this.assertEqual(d, {'foo': 'bar'}) |
|
779 def test_second(self): |
|
780 this.assertEqual(d, {'foo': 'bar'}) |
|
781 |
|
782 Test = patch.dict(d, {'foo': 'bar'}, clear=True)(Test) |
|
783 self.assertEqual(d, original) |
|
784 |
|
785 test = Test() |
|
786 |
|
787 test.test_first() |
|
788 self.assertEqual(d, original) |
|
789 |
|
790 test.test_second() |
|
791 self.assertEqual(d, original) |
|
792 |
|
793 test = Test() |
|
794 |
|
795 test.test_first() |
|
796 self.assertEqual(d, original) |
|
797 |
|
798 test.test_second() |
|
799 self.assertEqual(d, original) |
|
800 |
|
801 |
|
802 def test_get_only_proxy(self): |
|
803 class Something(object): |
|
804 foo = 'foo' |
|
805 class SomethingElse: |
|
806 foo = 'foo' |
|
807 |
|
808 for thing in Something, SomethingElse, Something(), SomethingElse: |
|
809 proxy = _get_proxy(thing) |
|
810 |
|
811 @patch.object(proxy, 'foo', 'bar') |
|
812 def test(): |
|
813 self.assertEqual(proxy.foo, 'bar') |
|
814 test() |
|
815 self.assertEqual(proxy.foo, 'foo') |
|
816 self.assertEqual(thing.foo, 'foo') |
|
817 self.assertNotIn('foo', proxy.__dict__) |
|
818 |
|
819 |
|
820 def test_get_set_delete_proxy(self): |
|
821 class Something(object): |
|
822 foo = 'foo' |
|
823 class SomethingElse: |
|
824 foo = 'foo' |
|
825 |
|
826 for thing in Something, SomethingElse, Something(), SomethingElse: |
|
827 proxy = _get_proxy(Something, get_only=False) |
|
828 |
|
829 @patch.object(proxy, 'foo', 'bar') |
|
830 def test(): |
|
831 self.assertEqual(proxy.foo, 'bar') |
|
832 test() |
|
833 self.assertEqual(proxy.foo, 'foo') |
|
834 self.assertEqual(thing.foo, 'foo') |
|
835 self.assertNotIn('foo', proxy.__dict__) |
|
836 |
|
837 |
|
838 def test_patch_keyword_args(self): |
|
839 kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33, |
|
840 'foo': MagicMock()} |
|
841 |
|
842 patcher = patch(foo_name, **kwargs) |
|
843 mock = patcher.start() |
|
844 patcher.stop() |
|
845 |
|
846 self.assertRaises(KeyError, mock) |
|
847 self.assertEqual(mock.foo.bar(), 33) |
|
848 self.assertIsInstance(mock.foo, MagicMock) |
|
849 |
|
850 |
|
851 def test_patch_object_keyword_args(self): |
|
852 kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33, |
|
853 'foo': MagicMock()} |
|
854 |
|
855 patcher = patch.object(Foo, 'f', **kwargs) |
|
856 mock = patcher.start() |
|
857 patcher.stop() |
|
858 |
|
859 self.assertRaises(KeyError, mock) |
|
860 self.assertEqual(mock.foo.bar(), 33) |
|
861 self.assertIsInstance(mock.foo, MagicMock) |
|
862 |
|
863 |
|
864 def test_patch_dict_keyword_args(self): |
|
865 original = {'foo': 'bar'} |
|
866 copy = original.copy() |
|
867 |
|
868 patcher = patch.dict(original, foo=3, bar=4, baz=5) |
|
869 patcher.start() |
|
870 |
|
871 try: |
|
872 self.assertEqual(original, dict(foo=3, bar=4, baz=5)) |
|
873 finally: |
|
874 patcher.stop() |
|
875 |
|
876 self.assertEqual(original, copy) |
|
877 |
|
878 |
|
879 def test_autospec(self): |
|
880 class Boo(object): |
|
881 def __init__(self, a): |
|
882 pass |
|
883 def f(self, a): |
|
884 pass |
|
885 def g(self): |
|
886 pass |
|
887 foo = 'bar' |
|
888 |
|
889 class Bar(object): |
|
890 def a(self): |
|
891 pass |
|
892 |
|
893 def _test(mock): |
|
894 mock(1) |
|
895 mock.assert_called_with(1) |
|
896 self.assertRaises(TypeError, mock) |
|
897 |
|
898 def _test2(mock): |
|
899 mock.f(1) |
|
900 mock.f.assert_called_with(1) |
|
901 self.assertRaises(TypeError, mock.f) |
|
902 |
|
903 mock.g() |
|
904 mock.g.assert_called_with() |
|
905 self.assertRaises(TypeError, mock.g, 1) |
|
906 |
|
907 self.assertRaises(AttributeError, getattr, mock, 'h') |
|
908 |
|
909 mock.foo.lower() |
|
910 mock.foo.lower.assert_called_with() |
|
911 self.assertRaises(AttributeError, getattr, mock.foo, 'bar') |
|
912 |
|
913 mock.Bar() |
|
914 mock.Bar.assert_called_with() |
|
915 |
|
916 mock.Bar.a() |
|
917 mock.Bar.a.assert_called_with() |
|
918 self.assertRaises(TypeError, mock.Bar.a, 1) |
|
919 |
|
920 mock.Bar().a() |
|
921 mock.Bar().a.assert_called_with() |
|
922 self.assertRaises(TypeError, mock.Bar().a, 1) |
|
923 |
|
924 self.assertRaises(AttributeError, getattr, mock.Bar, 'b') |
|
925 self.assertRaises(AttributeError, getattr, mock.Bar(), 'b') |
|
926 |
|
927 def function(mock): |
|
928 _test(mock) |
|
929 _test2(mock) |
|
930 _test2(mock(1)) |
|
931 self.assertIs(mock, Foo) |
|
932 return mock |
|
933 |
|
934 test = patch(foo_name, autospec=True)(function) |
|
935 |
|
936 mock = test() |
|
937 self.assertIsNot(Foo, mock) |
|
938 # test patching a second time works |
|
939 test() |
|
940 |
|
941 module = sys.modules[__name__] |
|
942 test = patch.object(module, 'Foo', autospec=True)(function) |
|
943 |
|
944 mock = test() |
|
945 self.assertIsNot(Foo, mock) |
|
946 # test patching a second time works |
|
947 test() |
|
948 |
|
949 |
|
950 def test_autospec_function(self): |
|
951 @patch('%s.function' % __name__, autospec=True) |
|
952 def test(mock): |
|
953 function(1) |
|
954 function.assert_called_with(1) |
|
955 function(2, 3) |
|
956 function.assert_called_with(2, 3) |
|
957 |
|
958 self.assertRaises(TypeError, function) |
|
959 self.assertRaises(AttributeError, getattr, function, 'foo') |
|
960 |
|
961 test() |
|
962 |
|
963 |
|
964 def test_autospec_keywords(self): |
|
965 @patch('%s.function' % __name__, autospec=True, |
|
966 return_value=3) |
|
967 def test(mock_function): |
|
968 #self.assertEqual(function.abc, 'foo') |
|
969 return function(1, 2) |
|
970 |
|
971 result = test() |
|
972 self.assertEqual(result, 3) |
|
973 |
|
974 |
|
975 def test_autospec_with_new(self): |
|
976 patcher = patch('%s.function' % __name__, new=3, autospec=True) |
|
977 self.assertRaises(TypeError, patcher.start) |
|
978 |
|
979 module = sys.modules[__name__] |
|
980 patcher = patch.object(module, 'function', new=3, autospec=True) |
|
981 self.assertRaises(TypeError, patcher.start) |
|
982 |
|
983 |
|
984 def test_autospec_with_object(self): |
|
985 class Bar(Foo): |
|
986 extra = [] |
|
987 |
|
988 patcher = patch(foo_name, autospec=Bar) |
|
989 mock = patcher.start() |
|
990 try: |
|
991 self.assertIsInstance(mock, Bar) |
|
992 self.assertIsInstance(mock.extra, list) |
|
993 finally: |
|
994 patcher.stop() |
|
995 |
|
996 |
|
997 def test_autospec_inherits(self): |
|
998 FooClass = Foo |
|
999 patcher = patch(foo_name, autospec=True) |
|
1000 mock = patcher.start() |
|
1001 try: |
|
1002 self.assertIsInstance(mock, FooClass) |
|
1003 self.assertIsInstance(mock(3), FooClass) |
|
1004 finally: |
|
1005 patcher.stop() |
|
1006 |
|
1007 |
|
1008 def test_autospec_name(self): |
|
1009 patcher = patch(foo_name, autospec=True) |
|
1010 mock = patcher.start() |
|
1011 |
|
1012 try: |
|
1013 self.assertIn(" name='Foo'", repr(mock)) |
|
1014 self.assertIn(" name='Foo.f'", repr(mock.f)) |
|
1015 self.assertIn(" name='Foo()'", repr(mock(None))) |
|
1016 self.assertIn(" name='Foo().f'", repr(mock(None).f)) |
|
1017 finally: |
|
1018 patcher.stop() |
|
1019 |
|
1020 |
|
1021 def test_tracebacks(self): |
|
1022 @patch.object(Foo, 'f', object()) |
|
1023 def test(): |
|
1024 raise AssertionError |
|
1025 try: |
|
1026 test() |
|
1027 except: |
|
1028 err = sys.exc_info() |
|
1029 |
|
1030 result = unittest2.TextTestResult(None, None, 0) |
|
1031 traceback = result._exc_info_to_string(err, self) |
|
1032 self.assertIn('raise AssertionError', traceback) |
|
1033 |
|
1034 |
|
1035 def test_new_callable_patch(self): |
|
1036 patcher = patch(foo_name, new_callable=NonCallableMagicMock) |
|
1037 |
|
1038 m1 = patcher.start() |
|
1039 patcher.stop() |
|
1040 m2 = patcher.start() |
|
1041 patcher.stop() |
|
1042 |
|
1043 self.assertIsNot(m1, m2) |
|
1044 for mock in m1, m2: |
|
1045 self.assertNotCallable(m1) |
|
1046 |
|
1047 |
|
1048 def test_new_callable_patch_object(self): |
|
1049 patcher = patch.object(Foo, 'f', new_callable=NonCallableMagicMock) |
|
1050 |
|
1051 m1 = patcher.start() |
|
1052 patcher.stop() |
|
1053 m2 = patcher.start() |
|
1054 patcher.stop() |
|
1055 |
|
1056 self.assertIsNot(m1, m2) |
|
1057 for mock in m1, m2: |
|
1058 self.assertNotCallable(m1) |
|
1059 |
|
1060 |
|
1061 def test_new_callable_keyword_arguments(self): |
|
1062 class Bar(object): |
|
1063 kwargs = None |
|
1064 def __init__(self, **kwargs): |
|
1065 Bar.kwargs = kwargs |
|
1066 |
|
1067 patcher = patch(foo_name, new_callable=Bar, arg1=1, arg2=2) |
|
1068 m = patcher.start() |
|
1069 try: |
|
1070 self.assertIs(type(m), Bar) |
|
1071 self.assertEqual(Bar.kwargs, dict(arg1=1, arg2=2)) |
|
1072 finally: |
|
1073 patcher.stop() |
|
1074 |
|
1075 |
|
1076 def test_new_callable_spec(self): |
|
1077 class Bar(object): |
|
1078 kwargs = None |
|
1079 def __init__(self, **kwargs): |
|
1080 Bar.kwargs = kwargs |
|
1081 |
|
1082 patcher = patch(foo_name, new_callable=Bar, spec=Bar) |
|
1083 patcher.start() |
|
1084 try: |
|
1085 self.assertEqual(Bar.kwargs, dict(spec=Bar)) |
|
1086 finally: |
|
1087 patcher.stop() |
|
1088 |
|
1089 patcher = patch(foo_name, new_callable=Bar, spec_set=Bar) |
|
1090 patcher.start() |
|
1091 try: |
|
1092 self.assertEqual(Bar.kwargs, dict(spec_set=Bar)) |
|
1093 finally: |
|
1094 patcher.stop() |
|
1095 |
|
1096 |
|
1097 def test_new_callable_create(self): |
|
1098 non_existent_attr = '%s.weeeee' % foo_name |
|
1099 p = patch(non_existent_attr, new_callable=NonCallableMock) |
|
1100 self.assertRaises(AttributeError, p.start) |
|
1101 |
|
1102 p = patch(non_existent_attr, new_callable=NonCallableMock, |
|
1103 create=True) |
|
1104 m = p.start() |
|
1105 try: |
|
1106 self.assertNotCallable(m, magic=False) |
|
1107 finally: |
|
1108 p.stop() |
|
1109 |
|
1110 |
|
1111 def test_new_callable_incompatible_with_new(self): |
|
1112 self.assertRaises( |
|
1113 ValueError, patch, foo_name, new=object(), new_callable=MagicMock |
|
1114 ) |
|
1115 self.assertRaises( |
|
1116 ValueError, patch.object, Foo, 'f', new=object(), |
|
1117 new_callable=MagicMock |
|
1118 ) |
|
1119 |
|
1120 |
|
1121 def test_new_callable_incompatible_with_autospec(self): |
|
1122 self.assertRaises( |
|
1123 ValueError, patch, foo_name, new_callable=MagicMock, |
|
1124 autospec=True |
|
1125 ) |
|
1126 self.assertRaises( |
|
1127 ValueError, patch.object, Foo, 'f', new_callable=MagicMock, |
|
1128 autospec=True |
|
1129 ) |
|
1130 |
|
1131 |
|
1132 def test_new_callable_inherit_for_mocks(self): |
|
1133 class MockSub(Mock): |
|
1134 pass |
|
1135 |
|
1136 MockClasses = ( |
|
1137 NonCallableMock, NonCallableMagicMock, MagicMock, Mock, MockSub |
|
1138 ) |
|
1139 for Klass in MockClasses: |
|
1140 for arg in 'spec', 'spec_set': |
|
1141 kwargs = {arg: True} |
|
1142 p = patch(foo_name, new_callable=Klass, **kwargs) |
|
1143 m = p.start() |
|
1144 try: |
|
1145 instance = m.return_value |
|
1146 self.assertRaises(AttributeError, getattr, instance, 'x') |
|
1147 finally: |
|
1148 p.stop() |
|
1149 |
|
1150 |
|
1151 def test_new_callable_inherit_non_mock(self): |
|
1152 class NotAMock(object): |
|
1153 def __init__(self, spec): |
|
1154 self.spec = spec |
|
1155 |
|
1156 p = patch(foo_name, new_callable=NotAMock, spec=True) |
|
1157 m = p.start() |
|
1158 try: |
|
1159 self.assertTrue(is_instance(m, NotAMock)) |
|
1160 self.assertRaises(AttributeError, getattr, m, 'return_value') |
|
1161 finally: |
|
1162 p.stop() |
|
1163 |
|
1164 self.assertEqual(m.spec, Foo) |
|
1165 |
|
1166 |
|
1167 def test_new_callable_class_decorating(self): |
|
1168 test = self |
|
1169 original = Foo |
|
1170 class SomeTest(object): |
|
1171 |
|
1172 def _test(self, mock_foo): |
|
1173 test.assertIsNot(Foo, original) |
|
1174 test.assertIs(Foo, mock_foo) |
|
1175 test.assertIsInstance(Foo, SomeClass) |
|
1176 |
|
1177 def test_two(self, mock_foo): |
|
1178 self._test(mock_foo) |
|
1179 def test_one(self, mock_foo): |
|
1180 self._test(mock_foo) |
|
1181 |
|
1182 SomeTest = patch(foo_name, new_callable=SomeClass)(SomeTest) |
|
1183 SomeTest().test_one() |
|
1184 SomeTest().test_two() |
|
1185 self.assertIs(Foo, original) |
|
1186 |
|
1187 |
|
1188 def test_patch_multiple(self): |
|
1189 original_foo = Foo |
|
1190 original_f = Foo.f |
|
1191 original_g = Foo.g |
|
1192 |
|
1193 patcher1 = patch.multiple(foo_name, f=1, g=2) |
|
1194 patcher2 = patch.multiple(Foo, f=1, g=2) |
|
1195 |
|
1196 for patcher in patcher1, patcher2: |
|
1197 patcher.start() |
|
1198 try: |
|
1199 self.assertIs(Foo, original_foo) |
|
1200 self.assertEqual(Foo.f, 1) |
|
1201 self.assertEqual(Foo.g, 2) |
|
1202 finally: |
|
1203 patcher.stop() |
|
1204 |
|
1205 self.assertIs(Foo, original_foo) |
|
1206 self.assertEqual(Foo.f, original_f) |
|
1207 self.assertEqual(Foo.g, original_g) |
|
1208 |
|
1209 |
|
1210 @patch.multiple(foo_name, f=3, g=4) |
|
1211 def test(): |
|
1212 self.assertIs(Foo, original_foo) |
|
1213 self.assertEqual(Foo.f, 3) |
|
1214 self.assertEqual(Foo.g, 4) |
|
1215 |
|
1216 test() |
|
1217 |
|
1218 |
|
1219 def test_patch_multiple_no_kwargs(self): |
|
1220 self.assertRaises(ValueError, patch.multiple, foo_name) |
|
1221 self.assertRaises(ValueError, patch.multiple, Foo) |
|
1222 |
|
1223 |
|
1224 def test_patch_multiple_create_mocks(self): |
|
1225 original_foo = Foo |
|
1226 original_f = Foo.f |
|
1227 original_g = Foo.g |
|
1228 |
|
1229 @patch.multiple(foo_name, f=DEFAULT, g=3, foo=DEFAULT) |
|
1230 def test(f, foo): |
|
1231 self.assertIs(Foo, original_foo) |
|
1232 self.assertIs(Foo.f, f) |
|
1233 self.assertEqual(Foo.g, 3) |
|
1234 self.assertIs(Foo.foo, foo) |
|
1235 self.assertTrue(is_instance(f, MagicMock)) |
|
1236 self.assertTrue(is_instance(foo, MagicMock)) |
|
1237 |
|
1238 test() |
|
1239 self.assertEqual(Foo.f, original_f) |
|
1240 self.assertEqual(Foo.g, original_g) |
|
1241 |
|
1242 |
|
1243 def test_patch_multiple_create_mocks_different_order(self): |
|
1244 # bug revealed by Jython! |
|
1245 original_f = Foo.f |
|
1246 original_g = Foo.g |
|
1247 |
|
1248 patcher = patch.object(Foo, 'f', 3) |
|
1249 patcher.attribute_name = 'f' |
|
1250 |
|
1251 other = patch.object(Foo, 'g', DEFAULT) |
|
1252 other.attribute_name = 'g' |
|
1253 patcher.additional_patchers = [other] |
|
1254 |
|
1255 @patcher |
|
1256 def test(g): |
|
1257 self.assertIs(Foo.g, g) |
|
1258 self.assertEqual(Foo.f, 3) |
|
1259 |
|
1260 test() |
|
1261 self.assertEqual(Foo.f, original_f) |
|
1262 self.assertEqual(Foo.g, original_g) |
|
1263 |
|
1264 |
|
1265 def test_patch_multiple_stacked_decorators(self): |
|
1266 original_foo = Foo |
|
1267 original_f = Foo.f |
|
1268 original_g = Foo.g |
|
1269 |
|
1270 @patch.multiple(foo_name, f=DEFAULT) |
|
1271 @patch.multiple(foo_name, foo=DEFAULT) |
|
1272 @patch(foo_name + '.g') |
|
1273 def test1(g, **kwargs): |
|
1274 _test(g, **kwargs) |
|
1275 |
|
1276 @patch.multiple(foo_name, f=DEFAULT) |
|
1277 @patch(foo_name + '.g') |
|
1278 @patch.multiple(foo_name, foo=DEFAULT) |
|
1279 def test2(g, **kwargs): |
|
1280 _test(g, **kwargs) |
|
1281 |
|
1282 @patch(foo_name + '.g') |
|
1283 @patch.multiple(foo_name, f=DEFAULT) |
|
1284 @patch.multiple(foo_name, foo=DEFAULT) |
|
1285 def test3(g, **kwargs): |
|
1286 _test(g, **kwargs) |
|
1287 |
|
1288 def _test(g, **kwargs): |
|
1289 f = kwargs.pop('f') |
|
1290 foo = kwargs.pop('foo') |
|
1291 self.assertFalse(kwargs) |
|
1292 |
|
1293 self.assertIs(Foo, original_foo) |
|
1294 self.assertIs(Foo.f, f) |
|
1295 self.assertIs(Foo.g, g) |
|
1296 self.assertIs(Foo.foo, foo) |
|
1297 self.assertTrue(is_instance(f, MagicMock)) |
|
1298 self.assertTrue(is_instance(g, MagicMock)) |
|
1299 self.assertTrue(is_instance(foo, MagicMock)) |
|
1300 |
|
1301 test1() |
|
1302 test2() |
|
1303 test3() |
|
1304 self.assertEqual(Foo.f, original_f) |
|
1305 self.assertEqual(Foo.g, original_g) |
|
1306 |
|
1307 |
|
1308 def test_patch_multiple_create_mocks_patcher(self): |
|
1309 original_foo = Foo |
|
1310 original_f = Foo.f |
|
1311 original_g = Foo.g |
|
1312 |
|
1313 patcher = patch.multiple(foo_name, f=DEFAULT, g=3, foo=DEFAULT) |
|
1314 |
|
1315 result = patcher.start() |
|
1316 try: |
|
1317 f = result['f'] |
|
1318 foo = result['foo'] |
|
1319 self.assertEqual(set(result), set(['f', 'foo'])) |
|
1320 |
|
1321 self.assertIs(Foo, original_foo) |
|
1322 self.assertIs(Foo.f, f) |
|
1323 self.assertIs(Foo.foo, foo) |
|
1324 self.assertTrue(is_instance(f, MagicMock)) |
|
1325 self.assertTrue(is_instance(foo, MagicMock)) |
|
1326 finally: |
|
1327 patcher.stop() |
|
1328 |
|
1329 self.assertEqual(Foo.f, original_f) |
|
1330 self.assertEqual(Foo.g, original_g) |
|
1331 |
|
1332 |
|
1333 def test_patch_multiple_decorating_class(self): |
|
1334 test = self |
|
1335 original_foo = Foo |
|
1336 original_f = Foo.f |
|
1337 original_g = Foo.g |
|
1338 |
|
1339 class SomeTest(object): |
|
1340 |
|
1341 def _test(self, f, foo): |
|
1342 test.assertIs(Foo, original_foo) |
|
1343 test.assertIs(Foo.f, f) |
|
1344 test.assertEqual(Foo.g, 3) |
|
1345 test.assertIs(Foo.foo, foo) |
|
1346 test.assertTrue(is_instance(f, MagicMock)) |
|
1347 test.assertTrue(is_instance(foo, MagicMock)) |
|
1348 |
|
1349 def test_two(self, f, foo): |
|
1350 self._test(f, foo) |
|
1351 def test_one(self, f, foo): |
|
1352 self._test(f, foo) |
|
1353 |
|
1354 SomeTest = patch.multiple( |
|
1355 foo_name, f=DEFAULT, g=3, foo=DEFAULT |
|
1356 )(SomeTest) |
|
1357 |
|
1358 thing = SomeTest() |
|
1359 thing.test_one() |
|
1360 thing.test_two() |
|
1361 |
|
1362 self.assertEqual(Foo.f, original_f) |
|
1363 self.assertEqual(Foo.g, original_g) |
|
1364 |
|
1365 |
|
1366 def test_patch_multiple_create(self): |
|
1367 patcher = patch.multiple(Foo, blam='blam') |
|
1368 self.assertRaises(AttributeError, patcher.start) |
|
1369 |
|
1370 patcher = patch.multiple(Foo, blam='blam', create=True) |
|
1371 patcher.start() |
|
1372 try: |
|
1373 self.assertEqual(Foo.blam, 'blam') |
|
1374 finally: |
|
1375 patcher.stop() |
|
1376 |
|
1377 self.assertFalse(hasattr(Foo, 'blam')) |
|
1378 |
|
1379 |
|
1380 def test_patch_multiple_spec_set(self): |
|
1381 # if spec_set works then we can assume that spec and autospec also |
|
1382 # work as the underlying machinery is the same |
|
1383 patcher = patch.multiple(Foo, foo=DEFAULT, spec_set=['a', 'b']) |
|
1384 result = patcher.start() |
|
1385 try: |
|
1386 self.assertEqual(Foo.foo, result['foo']) |
|
1387 Foo.foo.a(1) |
|
1388 Foo.foo.b(2) |
|
1389 Foo.foo.a.assert_called_with(1) |
|
1390 Foo.foo.b.assert_called_with(2) |
|
1391 self.assertRaises(AttributeError, setattr, Foo.foo, 'c', None) |
|
1392 finally: |
|
1393 patcher.stop() |
|
1394 |
|
1395 |
|
1396 def test_patch_multiple_new_callable(self): |
|
1397 class Thing(object): |
|
1398 pass |
|
1399 |
|
1400 patcher = patch.multiple( |
|
1401 Foo, f=DEFAULT, g=DEFAULT, new_callable=Thing |
|
1402 ) |
|
1403 result = patcher.start() |
|
1404 try: |
|
1405 self.assertIs(Foo.f, result['f']) |
|
1406 self.assertIs(Foo.g, result['g']) |
|
1407 self.assertIsInstance(Foo.f, Thing) |
|
1408 self.assertIsInstance(Foo.g, Thing) |
|
1409 self.assertIsNot(Foo.f, Foo.g) |
|
1410 finally: |
|
1411 patcher.stop() |
|
1412 |
|
1413 |
|
1414 def test_nested_patch_failure(self): |
|
1415 original_f = Foo.f |
|
1416 original_g = Foo.g |
|
1417 |
|
1418 @patch.object(Foo, 'g', 1) |
|
1419 @patch.object(Foo, 'missing', 1) |
|
1420 @patch.object(Foo, 'f', 1) |
|
1421 def thing1(): |
|
1422 pass |
|
1423 |
|
1424 @patch.object(Foo, 'missing', 1) |
|
1425 @patch.object(Foo, 'g', 1) |
|
1426 @patch.object(Foo, 'f', 1) |
|
1427 def thing2(): |
|
1428 pass |
|
1429 |
|
1430 @patch.object(Foo, 'g', 1) |
|
1431 @patch.object(Foo, 'f', 1) |
|
1432 @patch.object(Foo, 'missing', 1) |
|
1433 def thing3(): |
|
1434 pass |
|
1435 |
|
1436 for func in thing1, thing2, thing3: |
|
1437 self.assertRaises(AttributeError, func) |
|
1438 self.assertEqual(Foo.f, original_f) |
|
1439 self.assertEqual(Foo.g, original_g) |
|
1440 |
|
1441 |
|
1442 def test_new_callable_failure(self): |
|
1443 original_f = Foo.f |
|
1444 original_g = Foo.g |
|
1445 original_foo = Foo.foo |
|
1446 |
|
1447 def crasher(): |
|
1448 raise NameError('crasher') |
|
1449 |
|
1450 @patch.object(Foo, 'g', 1) |
|
1451 @patch.object(Foo, 'foo', new_callable=crasher) |
|
1452 @patch.object(Foo, 'f', 1) |
|
1453 def thing1(): |
|
1454 pass |
|
1455 |
|
1456 @patch.object(Foo, 'foo', new_callable=crasher) |
|
1457 @patch.object(Foo, 'g', 1) |
|
1458 @patch.object(Foo, 'f', 1) |
|
1459 def thing2(): |
|
1460 pass |
|
1461 |
|
1462 @patch.object(Foo, 'g', 1) |
|
1463 @patch.object(Foo, 'f', 1) |
|
1464 @patch.object(Foo, 'foo', new_callable=crasher) |
|
1465 def thing3(): |
|
1466 pass |
|
1467 |
|
1468 for func in thing1, thing2, thing3: |
|
1469 self.assertRaises(NameError, func) |
|
1470 self.assertEqual(Foo.f, original_f) |
|
1471 self.assertEqual(Foo.g, original_g) |
|
1472 self.assertEqual(Foo.foo, original_foo) |
|
1473 |
|
1474 |
|
1475 def test_patch_multiple_failure(self): |
|
1476 original_f = Foo.f |
|
1477 original_g = Foo.g |
|
1478 |
|
1479 patcher = patch.object(Foo, 'f', 1) |
|
1480 patcher.attribute_name = 'f' |
|
1481 |
|
1482 good = patch.object(Foo, 'g', 1) |
|
1483 good.attribute_name = 'g' |
|
1484 |
|
1485 bad = patch.object(Foo, 'missing', 1) |
|
1486 bad.attribute_name = 'missing' |
|
1487 |
|
1488 for additionals in [good, bad], [bad, good]: |
|
1489 patcher.additional_patchers = additionals |
|
1490 |
|
1491 @patcher |
|
1492 def func(): |
|
1493 pass |
|
1494 |
|
1495 self.assertRaises(AttributeError, func) |
|
1496 self.assertEqual(Foo.f, original_f) |
|
1497 self.assertEqual(Foo.g, original_g) |
|
1498 |
|
1499 |
|
1500 def test_patch_multiple_new_callable_failure(self): |
|
1501 original_f = Foo.f |
|
1502 original_g = Foo.g |
|
1503 original_foo = Foo.foo |
|
1504 |
|
1505 def crasher(): |
|
1506 raise NameError('crasher') |
|
1507 |
|
1508 patcher = patch.object(Foo, 'f', 1) |
|
1509 patcher.attribute_name = 'f' |
|
1510 |
|
1511 good = patch.object(Foo, 'g', 1) |
|
1512 good.attribute_name = 'g' |
|
1513 |
|
1514 bad = patch.object(Foo, 'foo', new_callable=crasher) |
|
1515 bad.attribute_name = 'foo' |
|
1516 |
|
1517 for additionals in [good, bad], [bad, good]: |
|
1518 patcher.additional_patchers = additionals |
|
1519 |
|
1520 @patcher |
|
1521 def func(): |
|
1522 pass |
|
1523 |
|
1524 self.assertRaises(NameError, func) |
|
1525 self.assertEqual(Foo.f, original_f) |
|
1526 self.assertEqual(Foo.g, original_g) |
|
1527 self.assertEqual(Foo.foo, original_foo) |
|
1528 |
|
1529 |
|
1530 def test_patch_multiple_string_subclasses(self): |
|
1531 for base in (str, unicode): |
|
1532 Foo = type('Foo', (base,), {'fish': 'tasty'}) |
|
1533 foo = Foo() |
|
1534 @patch.multiple(foo, fish='nearly gone') |
|
1535 def test(): |
|
1536 self.assertEqual(foo.fish, 'nearly gone') |
|
1537 |
|
1538 test() |
|
1539 self.assertEqual(foo.fish, 'tasty') |
|
1540 |
|
1541 |
|
1542 @patch('mock.patch.TEST_PREFIX', 'foo') |
|
1543 def test_patch_test_prefix(self): |
|
1544 class Foo(object): |
|
1545 thing = 'original' |
|
1546 |
|
1547 def foo_one(self): |
|
1548 return self.thing |
|
1549 def foo_two(self): |
|
1550 return self.thing |
|
1551 def test_one(self): |
|
1552 return self.thing |
|
1553 def test_two(self): |
|
1554 return self.thing |
|
1555 |
|
1556 Foo = patch.object(Foo, 'thing', 'changed')(Foo) |
|
1557 |
|
1558 foo = Foo() |
|
1559 self.assertEqual(foo.foo_one(), 'changed') |
|
1560 self.assertEqual(foo.foo_two(), 'changed') |
|
1561 self.assertEqual(foo.test_one(), 'original') |
|
1562 self.assertEqual(foo.test_two(), 'original') |
|
1563 |
|
1564 |
|
1565 @patch('mock.patch.TEST_PREFIX', 'bar') |
|
1566 def test_patch_dict_test_prefix(self): |
|
1567 class Foo(object): |
|
1568 def bar_one(self): |
|
1569 return dict(the_dict) |
|
1570 def bar_two(self): |
|
1571 return dict(the_dict) |
|
1572 def test_one(self): |
|
1573 return dict(the_dict) |
|
1574 def test_two(self): |
|
1575 return dict(the_dict) |
|
1576 |
|
1577 the_dict = {'key': 'original'} |
|
1578 Foo = patch.dict(the_dict, key='changed')(Foo) |
|
1579 |
|
1580 foo =Foo() |
|
1581 self.assertEqual(foo.bar_one(), {'key': 'changed'}) |
|
1582 self.assertEqual(foo.bar_two(), {'key': 'changed'}) |
|
1583 self.assertEqual(foo.test_one(), {'key': 'original'}) |
|
1584 self.assertEqual(foo.test_two(), {'key': 'original'}) |
|
1585 |
|
1586 |
|
1587 def test_patch_with_spec_mock_repr(self): |
|
1588 for arg in ('spec', 'autospec', 'spec_set'): |
|
1589 p = patch('%s.SomeClass' % __name__, **{arg: True}) |
|
1590 m = p.start() |
|
1591 try: |
|
1592 self.assertIn(" name='SomeClass'", repr(m)) |
|
1593 self.assertIn(" name='SomeClass.class_attribute'", |
|
1594 repr(m.class_attribute)) |
|
1595 self.assertIn(" name='SomeClass()'", repr(m())) |
|
1596 self.assertIn(" name='SomeClass().class_attribute'", |
|
1597 repr(m().class_attribute)) |
|
1598 finally: |
|
1599 p.stop() |
|
1600 |
|
1601 |
|
1602 def test_patch_nested_autospec_repr(self): |
|
1603 p = patch('tests.support', autospec=True) |
|
1604 m = p.start() |
|
1605 try: |
|
1606 self.assertIn(" name='support.SomeClass.wibble()'", |
|
1607 repr(m.SomeClass.wibble())) |
|
1608 self.assertIn(" name='support.SomeClass().wibble()'", |
|
1609 repr(m.SomeClass().wibble())) |
|
1610 finally: |
|
1611 p.stop() |
|
1612 |
|
1613 |
|
1614 def test_mock_calls_with_patch(self): |
|
1615 for arg in ('spec', 'autospec', 'spec_set'): |
|
1616 p = patch('%s.SomeClass' % __name__, **{arg: True}) |
|
1617 m = p.start() |
|
1618 try: |
|
1619 m.wibble() |
|
1620 |
|
1621 kalls = [call.wibble()] |
|
1622 self.assertEqual(m.mock_calls, kalls) |
|
1623 self.assertEqual(m.method_calls, kalls) |
|
1624 self.assertEqual(m.wibble.mock_calls, [call()]) |
|
1625 |
|
1626 result = m() |
|
1627 kalls.append(call()) |
|
1628 self.assertEqual(m.mock_calls, kalls) |
|
1629 |
|
1630 result.wibble() |
|
1631 kalls.append(call().wibble()) |
|
1632 self.assertEqual(m.mock_calls, kalls) |
|
1633 |
|
1634 self.assertEqual(result.mock_calls, [call.wibble()]) |
|
1635 self.assertEqual(result.wibble.mock_calls, [call()]) |
|
1636 self.assertEqual(result.method_calls, [call.wibble()]) |
|
1637 finally: |
|
1638 p.stop() |
|
1639 |
|
1640 |
|
1641 def test_patch_imports_lazily(self): |
|
1642 sys.modules.pop('squizz', None) |
|
1643 |
|
1644 p1 = patch('squizz.squozz') |
|
1645 self.assertRaises(ImportError, p1.start) |
|
1646 |
|
1647 squizz = Mock() |
|
1648 squizz.squozz = 6 |
|
1649 sys.modules['squizz'] = squizz |
|
1650 p1 = patch('squizz.squozz') |
|
1651 squizz.squozz = 3 |
|
1652 p1.start() |
|
1653 p1.stop() |
|
1654 self.assertEqual(squizz.squozz, 3) |
|
1655 |
|
1656 |
|
1657 def test_patch_propogrates_exc_on_exit(self): |
|
1658 class holder: |
|
1659 exc_info = None, None, None |
|
1660 |
|
1661 class custom_patch(_patch): |
|
1662 def __exit__(self, etype=None, val=None, tb=None): |
|
1663 _patch.__exit__(self, etype, val, tb) |
|
1664 holder.exc_info = etype, val, tb |
|
1665 stop = __exit__ |
|
1666 |
|
1667 def with_custom_patch(target): |
|
1668 getter, attribute = _get_target(target) |
|
1669 return custom_patch( |
|
1670 getter, attribute, DEFAULT, None, False, None, |
|
1671 None, None, {} |
|
1672 ) |
|
1673 |
|
1674 @with_custom_patch('squizz.squozz') |
|
1675 def test(mock): |
|
1676 raise RuntimeError |
|
1677 |
|
1678 self.assertRaises(RuntimeError, test) |
|
1679 self.assertIs(holder.exc_info[0], RuntimeError) |
|
1680 self.assertIsNotNone(holder.exc_info[1], |
|
1681 'exception value not propgated') |
|
1682 self.assertIsNotNone(holder.exc_info[2], |
|
1683 'exception traceback not propgated') |
|
1684 |
|
1685 |
|
1686 def test_create_and_specs(self): |
|
1687 for kwarg in ('spec', 'spec_set', 'autospec'): |
|
1688 p = patch('%s.doesnotexist' % __name__, create=True, |
|
1689 **{kwarg: True}) |
|
1690 self.assertRaises(TypeError, p.start) |
|
1691 self.assertRaises(NameError, lambda: doesnotexist) |
|
1692 |
|
1693 # check that spec with create is innocuous if the original exists |
|
1694 p = patch(MODNAME, create=True, **{kwarg: True}) |
|
1695 p.start() |
|
1696 p.stop() |
|
1697 |
|
1698 |
|
1699 def test_multiple_specs(self): |
|
1700 original = PTModule |
|
1701 for kwarg in ('spec', 'spec_set'): |
|
1702 p = patch(MODNAME, autospec=0, **{kwarg: 0}) |
|
1703 self.assertRaises(TypeError, p.start) |
|
1704 self.assertIs(PTModule, original) |
|
1705 |
|
1706 for kwarg in ('spec', 'autospec'): |
|
1707 p = patch(MODNAME, spec_set=0, **{kwarg: 0}) |
|
1708 self.assertRaises(TypeError, p.start) |
|
1709 self.assertIs(PTModule, original) |
|
1710 |
|
1711 for kwarg in ('spec_set', 'autospec'): |
|
1712 p = patch(MODNAME, spec=0, **{kwarg: 0}) |
|
1713 self.assertRaises(TypeError, p.start) |
|
1714 self.assertIs(PTModule, original) |
|
1715 |
|
1716 |
|
1717 def test_specs_false_instead_of_none(self): |
|
1718 p = patch(MODNAME, spec=False, spec_set=False, autospec=False) |
|
1719 mock = p.start() |
|
1720 try: |
|
1721 # no spec should have been set, so attribute access should not fail |
|
1722 mock.does_not_exist |
|
1723 mock.does_not_exist = 3 |
|
1724 finally: |
|
1725 p.stop() |
|
1726 |
|
1727 |
|
1728 def test_falsey_spec(self): |
|
1729 for kwarg in ('spec', 'autospec', 'spec_set'): |
|
1730 p = patch(MODNAME, **{kwarg: 0}) |
|
1731 m = p.start() |
|
1732 try: |
|
1733 self.assertRaises(AttributeError, getattr, m, 'doesnotexit') |
|
1734 finally: |
|
1735 p.stop() |
|
1736 |
|
1737 |
|
1738 def test_spec_set_true(self): |
|
1739 for kwarg in ('spec', 'autospec'): |
|
1740 p = patch(MODNAME, spec_set=True, **{kwarg: True}) |
|
1741 m = p.start() |
|
1742 try: |
|
1743 self.assertRaises(AttributeError, setattr, m, |
|
1744 'doesnotexist', 'something') |
|
1745 self.assertRaises(AttributeError, getattr, m, 'doesnotexist') |
|
1746 finally: |
|
1747 p.stop() |
|
1748 |
|
1749 |
|
1750 def test_callable_spec_as_list(self): |
|
1751 spec = ('__call__',) |
|
1752 p = patch(MODNAME, spec=spec) |
|
1753 m = p.start() |
|
1754 try: |
|
1755 self.assertTrue(callable(m)) |
|
1756 finally: |
|
1757 p.stop() |
|
1758 |
|
1759 |
|
1760 def test_not_callable_spec_as_list(self): |
|
1761 spec = ('foo', 'bar') |
|
1762 p = patch(MODNAME, spec=spec) |
|
1763 m = p.start() |
|
1764 try: |
|
1765 self.assertFalse(callable(m)) |
|
1766 finally: |
|
1767 p.stop() |
|
1768 |
|
1769 |
|
1770 def test_patch_stopall(self): |
|
1771 unlink = os.unlink |
|
1772 chdir = os.chdir |
|
1773 path = os.path |
|
1774 patch('os.unlink', something).start() |
|
1775 patch('os.chdir', something_else).start() |
|
1776 |
|
1777 @patch('os.path') |
|
1778 def patched(mock_path): |
|
1779 patch.stopall() |
|
1780 self.assertIs(os.path, mock_path) |
|
1781 self.assertIs(os.unlink, unlink) |
|
1782 self.assertIs(os.chdir, chdir) |
|
1783 |
|
1784 patched() |
|
1785 self.assertIs(os.path, path) |
|
1786 |
|
1787 |
|
1788 |
|
1789 if __name__ == '__main__': |
|
1790 unittest2.main() |