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