1 | n/a | import copy |
---|
2 | n/a | import sys |
---|
3 | n/a | import tempfile |
---|
4 | n/a | |
---|
5 | n/a | import unittest |
---|
6 | n/a | from unittest.test.testmock.support import is_instance |
---|
7 | n/a | from unittest import mock |
---|
8 | n/a | from unittest.mock import ( |
---|
9 | n/a | call, DEFAULT, patch, sentinel, |
---|
10 | n/a | MagicMock, Mock, NonCallableMock, |
---|
11 | n/a | NonCallableMagicMock, _CallList, |
---|
12 | n/a | create_autospec |
---|
13 | n/a | ) |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | class Iter(object): |
---|
17 | n/a | def __init__(self): |
---|
18 | n/a | self.thing = iter(['this', 'is', 'an', 'iter']) |
---|
19 | n/a | |
---|
20 | n/a | def __iter__(self): |
---|
21 | n/a | return self |
---|
22 | n/a | |
---|
23 | n/a | def next(self): |
---|
24 | n/a | return next(self.thing) |
---|
25 | n/a | |
---|
26 | n/a | __next__ = next |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | class Something(object): |
---|
30 | n/a | def meth(self, a, b, c, d=None): |
---|
31 | n/a | pass |
---|
32 | n/a | |
---|
33 | n/a | @classmethod |
---|
34 | n/a | def cmeth(cls, a, b, c, d=None): |
---|
35 | n/a | pass |
---|
36 | n/a | |
---|
37 | n/a | @staticmethod |
---|
38 | n/a | def smeth(a, b, c, d=None): |
---|
39 | n/a | pass |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | class MockTest(unittest.TestCase): |
---|
43 | n/a | |
---|
44 | n/a | def test_all(self): |
---|
45 | n/a | # if __all__ is badly defined then import * will raise an error |
---|
46 | n/a | # We have to exec it because you can't import * inside a method |
---|
47 | n/a | # in Python 3 |
---|
48 | n/a | exec("from unittest.mock import *") |
---|
49 | n/a | |
---|
50 | n/a | |
---|
51 | n/a | def test_constructor(self): |
---|
52 | n/a | mock = Mock() |
---|
53 | n/a | |
---|
54 | n/a | self.assertFalse(mock.called, "called not initialised correctly") |
---|
55 | n/a | self.assertEqual(mock.call_count, 0, |
---|
56 | n/a | "call_count not initialised correctly") |
---|
57 | n/a | self.assertTrue(is_instance(mock.return_value, Mock), |
---|
58 | n/a | "return_value not initialised correctly") |
---|
59 | n/a | |
---|
60 | n/a | self.assertEqual(mock.call_args, None, |
---|
61 | n/a | "call_args not initialised correctly") |
---|
62 | n/a | self.assertEqual(mock.call_args_list, [], |
---|
63 | n/a | "call_args_list not initialised correctly") |
---|
64 | n/a | self.assertEqual(mock.method_calls, [], |
---|
65 | n/a | "method_calls not initialised correctly") |
---|
66 | n/a | |
---|
67 | n/a | # Can't use hasattr for this test as it always returns True on a mock |
---|
68 | n/a | self.assertNotIn('_items', mock.__dict__, |
---|
69 | n/a | "default mock should not have '_items' attribute") |
---|
70 | n/a | |
---|
71 | n/a | self.assertIsNone(mock._mock_parent, |
---|
72 | n/a | "parent not initialised correctly") |
---|
73 | n/a | self.assertIsNone(mock._mock_methods, |
---|
74 | n/a | "methods not initialised correctly") |
---|
75 | n/a | self.assertEqual(mock._mock_children, {}, |
---|
76 | n/a | "children not initialised incorrectly") |
---|
77 | n/a | |
---|
78 | n/a | |
---|
79 | n/a | def test_return_value_in_constructor(self): |
---|
80 | n/a | mock = Mock(return_value=None) |
---|
81 | n/a | self.assertIsNone(mock.return_value, |
---|
82 | n/a | "return value in constructor not honoured") |
---|
83 | n/a | |
---|
84 | n/a | |
---|
85 | n/a | def test_repr(self): |
---|
86 | n/a | mock = Mock(name='foo') |
---|
87 | n/a | self.assertIn('foo', repr(mock)) |
---|
88 | n/a | self.assertIn("'%s'" % id(mock), repr(mock)) |
---|
89 | n/a | |
---|
90 | n/a | mocks = [(Mock(), 'mock'), (Mock(name='bar'), 'bar')] |
---|
91 | n/a | for mock, name in mocks: |
---|
92 | n/a | self.assertIn('%s.bar' % name, repr(mock.bar)) |
---|
93 | n/a | self.assertIn('%s.foo()' % name, repr(mock.foo())) |
---|
94 | n/a | self.assertIn('%s.foo().bing' % name, repr(mock.foo().bing)) |
---|
95 | n/a | self.assertIn('%s()' % name, repr(mock())) |
---|
96 | n/a | self.assertIn('%s()()' % name, repr(mock()())) |
---|
97 | n/a | self.assertIn('%s()().foo.bar.baz().bing' % name, |
---|
98 | n/a | repr(mock()().foo.bar.baz().bing)) |
---|
99 | n/a | |
---|
100 | n/a | |
---|
101 | n/a | def test_repr_with_spec(self): |
---|
102 | n/a | class X(object): |
---|
103 | n/a | pass |
---|
104 | n/a | |
---|
105 | n/a | mock = Mock(spec=X) |
---|
106 | n/a | self.assertIn(" spec='X' ", repr(mock)) |
---|
107 | n/a | |
---|
108 | n/a | mock = Mock(spec=X()) |
---|
109 | n/a | self.assertIn(" spec='X' ", repr(mock)) |
---|
110 | n/a | |
---|
111 | n/a | mock = Mock(spec_set=X) |
---|
112 | n/a | self.assertIn(" spec_set='X' ", repr(mock)) |
---|
113 | n/a | |
---|
114 | n/a | mock = Mock(spec_set=X()) |
---|
115 | n/a | self.assertIn(" spec_set='X' ", repr(mock)) |
---|
116 | n/a | |
---|
117 | n/a | mock = Mock(spec=X, name='foo') |
---|
118 | n/a | self.assertIn(" spec='X' ", repr(mock)) |
---|
119 | n/a | self.assertIn(" name='foo' ", repr(mock)) |
---|
120 | n/a | |
---|
121 | n/a | mock = Mock(name='foo') |
---|
122 | n/a | self.assertNotIn("spec", repr(mock)) |
---|
123 | n/a | |
---|
124 | n/a | mock = Mock() |
---|
125 | n/a | self.assertNotIn("spec", repr(mock)) |
---|
126 | n/a | |
---|
127 | n/a | mock = Mock(spec=['foo']) |
---|
128 | n/a | self.assertNotIn("spec", repr(mock)) |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | def test_side_effect(self): |
---|
132 | n/a | mock = Mock() |
---|
133 | n/a | |
---|
134 | n/a | def effect(*args, **kwargs): |
---|
135 | n/a | raise SystemError('kablooie') |
---|
136 | n/a | |
---|
137 | n/a | mock.side_effect = effect |
---|
138 | n/a | self.assertRaises(SystemError, mock, 1, 2, fish=3) |
---|
139 | n/a | mock.assert_called_with(1, 2, fish=3) |
---|
140 | n/a | |
---|
141 | n/a | results = [1, 2, 3] |
---|
142 | n/a | def effect(): |
---|
143 | n/a | return results.pop() |
---|
144 | n/a | mock.side_effect = effect |
---|
145 | n/a | |
---|
146 | n/a | self.assertEqual([mock(), mock(), mock()], [3, 2, 1], |
---|
147 | n/a | "side effect not used correctly") |
---|
148 | n/a | |
---|
149 | n/a | mock = Mock(side_effect=sentinel.SideEffect) |
---|
150 | n/a | self.assertEqual(mock.side_effect, sentinel.SideEffect, |
---|
151 | n/a | "side effect in constructor not used") |
---|
152 | n/a | |
---|
153 | n/a | def side_effect(): |
---|
154 | n/a | return DEFAULT |
---|
155 | n/a | mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN) |
---|
156 | n/a | self.assertEqual(mock(), sentinel.RETURN) |
---|
157 | n/a | |
---|
158 | n/a | def test_autospec_side_effect(self): |
---|
159 | n/a | # Test for issue17826 |
---|
160 | n/a | results = [1, 2, 3] |
---|
161 | n/a | def effect(): |
---|
162 | n/a | return results.pop() |
---|
163 | n/a | def f(): |
---|
164 | n/a | pass |
---|
165 | n/a | |
---|
166 | n/a | mock = create_autospec(f) |
---|
167 | n/a | mock.side_effect = [1, 2, 3] |
---|
168 | n/a | self.assertEqual([mock(), mock(), mock()], [1, 2, 3], |
---|
169 | n/a | "side effect not used correctly in create_autospec") |
---|
170 | n/a | # Test where side effect is a callable |
---|
171 | n/a | results = [1, 2, 3] |
---|
172 | n/a | mock = create_autospec(f) |
---|
173 | n/a | mock.side_effect = effect |
---|
174 | n/a | self.assertEqual([mock(), mock(), mock()], [3, 2, 1], |
---|
175 | n/a | "callable side effect not used correctly") |
---|
176 | n/a | |
---|
177 | n/a | def test_autospec_side_effect_exception(self): |
---|
178 | n/a | # Test for issue 23661 |
---|
179 | n/a | def f(): |
---|
180 | n/a | pass |
---|
181 | n/a | |
---|
182 | n/a | mock = create_autospec(f) |
---|
183 | n/a | mock.side_effect = ValueError('Bazinga!') |
---|
184 | n/a | self.assertRaisesRegex(ValueError, 'Bazinga!', mock) |
---|
185 | n/a | |
---|
186 | n/a | @unittest.skipUnless('java' in sys.platform, |
---|
187 | n/a | 'This test only applies to Jython') |
---|
188 | n/a | def test_java_exception_side_effect(self): |
---|
189 | n/a | import java |
---|
190 | n/a | mock = Mock(side_effect=java.lang.RuntimeException("Boom!")) |
---|
191 | n/a | |
---|
192 | n/a | # can't use assertRaises with java exceptions |
---|
193 | n/a | try: |
---|
194 | n/a | mock(1, 2, fish=3) |
---|
195 | n/a | except java.lang.RuntimeException: |
---|
196 | n/a | pass |
---|
197 | n/a | else: |
---|
198 | n/a | self.fail('java exception not raised') |
---|
199 | n/a | mock.assert_called_with(1,2, fish=3) |
---|
200 | n/a | |
---|
201 | n/a | |
---|
202 | n/a | def test_reset_mock(self): |
---|
203 | n/a | parent = Mock() |
---|
204 | n/a | spec = ["something"] |
---|
205 | n/a | mock = Mock(name="child", parent=parent, spec=spec) |
---|
206 | n/a | mock(sentinel.Something, something=sentinel.SomethingElse) |
---|
207 | n/a | something = mock.something |
---|
208 | n/a | mock.something() |
---|
209 | n/a | mock.side_effect = sentinel.SideEffect |
---|
210 | n/a | return_value = mock.return_value |
---|
211 | n/a | return_value() |
---|
212 | n/a | |
---|
213 | n/a | mock.reset_mock() |
---|
214 | n/a | |
---|
215 | n/a | self.assertEqual(mock._mock_name, "child", |
---|
216 | n/a | "name incorrectly reset") |
---|
217 | n/a | self.assertEqual(mock._mock_parent, parent, |
---|
218 | n/a | "parent incorrectly reset") |
---|
219 | n/a | self.assertEqual(mock._mock_methods, spec, |
---|
220 | n/a | "methods incorrectly reset") |
---|
221 | n/a | |
---|
222 | n/a | self.assertFalse(mock.called, "called not reset") |
---|
223 | n/a | self.assertEqual(mock.call_count, 0, "call_count not reset") |
---|
224 | n/a | self.assertEqual(mock.call_args, None, "call_args not reset") |
---|
225 | n/a | self.assertEqual(mock.call_args_list, [], "call_args_list not reset") |
---|
226 | n/a | self.assertEqual(mock.method_calls, [], |
---|
227 | n/a | "method_calls not initialised correctly: %r != %r" % |
---|
228 | n/a | (mock.method_calls, [])) |
---|
229 | n/a | self.assertEqual(mock.mock_calls, []) |
---|
230 | n/a | |
---|
231 | n/a | self.assertEqual(mock.side_effect, sentinel.SideEffect, |
---|
232 | n/a | "side_effect incorrectly reset") |
---|
233 | n/a | self.assertEqual(mock.return_value, return_value, |
---|
234 | n/a | "return_value incorrectly reset") |
---|
235 | n/a | self.assertFalse(return_value.called, "return value mock not reset") |
---|
236 | n/a | self.assertEqual(mock._mock_children, {'something': something}, |
---|
237 | n/a | "children reset incorrectly") |
---|
238 | n/a | self.assertEqual(mock.something, something, |
---|
239 | n/a | "children incorrectly cleared") |
---|
240 | n/a | self.assertFalse(mock.something.called, "child not reset") |
---|
241 | n/a | |
---|
242 | n/a | |
---|
243 | n/a | def test_reset_mock_recursion(self): |
---|
244 | n/a | mock = Mock() |
---|
245 | n/a | mock.return_value = mock |
---|
246 | n/a | |
---|
247 | n/a | # used to cause recursion |
---|
248 | n/a | mock.reset_mock() |
---|
249 | n/a | |
---|
250 | n/a | def test_reset_mock_on_mock_open_issue_18622(self): |
---|
251 | n/a | a = mock.mock_open() |
---|
252 | n/a | a.reset_mock() |
---|
253 | n/a | |
---|
254 | n/a | def test_call(self): |
---|
255 | n/a | mock = Mock() |
---|
256 | n/a | self.assertTrue(is_instance(mock.return_value, Mock), |
---|
257 | n/a | "Default return_value should be a Mock") |
---|
258 | n/a | |
---|
259 | n/a | result = mock() |
---|
260 | n/a | self.assertEqual(mock(), result, |
---|
261 | n/a | "different result from consecutive calls") |
---|
262 | n/a | mock.reset_mock() |
---|
263 | n/a | |
---|
264 | n/a | ret_val = mock(sentinel.Arg) |
---|
265 | n/a | self.assertTrue(mock.called, "called not set") |
---|
266 | n/a | self.assertEqual(mock.call_count, 1, "call_count incoreect") |
---|
267 | n/a | self.assertEqual(mock.call_args, ((sentinel.Arg,), {}), |
---|
268 | n/a | "call_args not set") |
---|
269 | n/a | self.assertEqual(mock.call_args_list, [((sentinel.Arg,), {})], |
---|
270 | n/a | "call_args_list not initialised correctly") |
---|
271 | n/a | |
---|
272 | n/a | mock.return_value = sentinel.ReturnValue |
---|
273 | n/a | ret_val = mock(sentinel.Arg, key=sentinel.KeyArg) |
---|
274 | n/a | self.assertEqual(ret_val, sentinel.ReturnValue, |
---|
275 | n/a | "incorrect return value") |
---|
276 | n/a | |
---|
277 | n/a | self.assertEqual(mock.call_count, 2, "call_count incorrect") |
---|
278 | n/a | self.assertEqual(mock.call_args, |
---|
279 | n/a | ((sentinel.Arg,), {'key': sentinel.KeyArg}), |
---|
280 | n/a | "call_args not set") |
---|
281 | n/a | self.assertEqual(mock.call_args_list, [ |
---|
282 | n/a | ((sentinel.Arg,), {}), |
---|
283 | n/a | ((sentinel.Arg,), {'key': sentinel.KeyArg}) |
---|
284 | n/a | ], |
---|
285 | n/a | "call_args_list not set") |
---|
286 | n/a | |
---|
287 | n/a | |
---|
288 | n/a | def test_call_args_comparison(self): |
---|
289 | n/a | mock = Mock() |
---|
290 | n/a | mock() |
---|
291 | n/a | mock(sentinel.Arg) |
---|
292 | n/a | mock(kw=sentinel.Kwarg) |
---|
293 | n/a | mock(sentinel.Arg, kw=sentinel.Kwarg) |
---|
294 | n/a | self.assertEqual(mock.call_args_list, [ |
---|
295 | n/a | (), |
---|
296 | n/a | ((sentinel.Arg,),), |
---|
297 | n/a | ({"kw": sentinel.Kwarg},), |
---|
298 | n/a | ((sentinel.Arg,), {"kw": sentinel.Kwarg}) |
---|
299 | n/a | ]) |
---|
300 | n/a | self.assertEqual(mock.call_args, |
---|
301 | n/a | ((sentinel.Arg,), {"kw": sentinel.Kwarg})) |
---|
302 | n/a | |
---|
303 | n/a | # Comparing call_args to a long sequence should not raise |
---|
304 | n/a | # an exception. See issue 24857. |
---|
305 | n/a | self.assertFalse(mock.call_args == "a long sequence") |
---|
306 | n/a | |
---|
307 | n/a | |
---|
308 | n/a | def test_calls_equal_with_any(self): |
---|
309 | n/a | # Check that equality and non-equality is consistent even when |
---|
310 | n/a | # comparing with mock.ANY |
---|
311 | n/a | mm = mock.MagicMock() |
---|
312 | n/a | self.assertTrue(mm == mm) |
---|
313 | n/a | self.assertFalse(mm != mm) |
---|
314 | n/a | self.assertFalse(mm == mock.MagicMock()) |
---|
315 | n/a | self.assertTrue(mm != mock.MagicMock()) |
---|
316 | n/a | self.assertTrue(mm == mock.ANY) |
---|
317 | n/a | self.assertFalse(mm != mock.ANY) |
---|
318 | n/a | self.assertTrue(mock.ANY == mm) |
---|
319 | n/a | self.assertFalse(mock.ANY != mm) |
---|
320 | n/a | |
---|
321 | n/a | call1 = mock.call(mock.MagicMock()) |
---|
322 | n/a | call2 = mock.call(mock.ANY) |
---|
323 | n/a | self.assertTrue(call1 == call2) |
---|
324 | n/a | self.assertFalse(call1 != call2) |
---|
325 | n/a | self.assertTrue(call2 == call1) |
---|
326 | n/a | self.assertFalse(call2 != call1) |
---|
327 | n/a | |
---|
328 | n/a | |
---|
329 | n/a | def test_assert_called_with(self): |
---|
330 | n/a | mock = Mock() |
---|
331 | n/a | mock() |
---|
332 | n/a | |
---|
333 | n/a | # Will raise an exception if it fails |
---|
334 | n/a | mock.assert_called_with() |
---|
335 | n/a | self.assertRaises(AssertionError, mock.assert_called_with, 1) |
---|
336 | n/a | |
---|
337 | n/a | mock.reset_mock() |
---|
338 | n/a | self.assertRaises(AssertionError, mock.assert_called_with) |
---|
339 | n/a | |
---|
340 | n/a | mock(1, 2, 3, a='fish', b='nothing') |
---|
341 | n/a | mock.assert_called_with(1, 2, 3, a='fish', b='nothing') |
---|
342 | n/a | |
---|
343 | n/a | |
---|
344 | n/a | def test_assert_called_with_any(self): |
---|
345 | n/a | m = MagicMock() |
---|
346 | n/a | m(MagicMock()) |
---|
347 | n/a | m.assert_called_with(mock.ANY) |
---|
348 | n/a | |
---|
349 | n/a | |
---|
350 | n/a | def test_assert_called_with_function_spec(self): |
---|
351 | n/a | def f(a, b, c, d=None): |
---|
352 | n/a | pass |
---|
353 | n/a | |
---|
354 | n/a | mock = Mock(spec=f) |
---|
355 | n/a | |
---|
356 | n/a | mock(1, b=2, c=3) |
---|
357 | n/a | mock.assert_called_with(1, 2, 3) |
---|
358 | n/a | mock.assert_called_with(a=1, b=2, c=3) |
---|
359 | n/a | self.assertRaises(AssertionError, mock.assert_called_with, |
---|
360 | n/a | 1, b=3, c=2) |
---|
361 | n/a | # Expected call doesn't match the spec's signature |
---|
362 | n/a | with self.assertRaises(AssertionError) as cm: |
---|
363 | n/a | mock.assert_called_with(e=8) |
---|
364 | n/a | self.assertIsInstance(cm.exception.__cause__, TypeError) |
---|
365 | n/a | |
---|
366 | n/a | |
---|
367 | n/a | def test_assert_called_with_method_spec(self): |
---|
368 | n/a | def _check(mock): |
---|
369 | n/a | mock(1, b=2, c=3) |
---|
370 | n/a | mock.assert_called_with(1, 2, 3) |
---|
371 | n/a | mock.assert_called_with(a=1, b=2, c=3) |
---|
372 | n/a | self.assertRaises(AssertionError, mock.assert_called_with, |
---|
373 | n/a | 1, b=3, c=2) |
---|
374 | n/a | |
---|
375 | n/a | mock = Mock(spec=Something().meth) |
---|
376 | n/a | _check(mock) |
---|
377 | n/a | mock = Mock(spec=Something.cmeth) |
---|
378 | n/a | _check(mock) |
---|
379 | n/a | mock = Mock(spec=Something().cmeth) |
---|
380 | n/a | _check(mock) |
---|
381 | n/a | mock = Mock(spec=Something.smeth) |
---|
382 | n/a | _check(mock) |
---|
383 | n/a | mock = Mock(spec=Something().smeth) |
---|
384 | n/a | _check(mock) |
---|
385 | n/a | |
---|
386 | n/a | |
---|
387 | n/a | def test_assert_called_once_with(self): |
---|
388 | n/a | mock = Mock() |
---|
389 | n/a | mock() |
---|
390 | n/a | |
---|
391 | n/a | # Will raise an exception if it fails |
---|
392 | n/a | mock.assert_called_once_with() |
---|
393 | n/a | |
---|
394 | n/a | mock() |
---|
395 | n/a | self.assertRaises(AssertionError, mock.assert_called_once_with) |
---|
396 | n/a | |
---|
397 | n/a | mock.reset_mock() |
---|
398 | n/a | self.assertRaises(AssertionError, mock.assert_called_once_with) |
---|
399 | n/a | |
---|
400 | n/a | mock('foo', 'bar', baz=2) |
---|
401 | n/a | mock.assert_called_once_with('foo', 'bar', baz=2) |
---|
402 | n/a | |
---|
403 | n/a | mock.reset_mock() |
---|
404 | n/a | mock('foo', 'bar', baz=2) |
---|
405 | n/a | self.assertRaises( |
---|
406 | n/a | AssertionError, |
---|
407 | n/a | lambda: mock.assert_called_once_with('bob', 'bar', baz=2) |
---|
408 | n/a | ) |
---|
409 | n/a | |
---|
410 | n/a | |
---|
411 | n/a | def test_assert_called_once_with_function_spec(self): |
---|
412 | n/a | def f(a, b, c, d=None): |
---|
413 | n/a | pass |
---|
414 | n/a | |
---|
415 | n/a | mock = Mock(spec=f) |
---|
416 | n/a | |
---|
417 | n/a | mock(1, b=2, c=3) |
---|
418 | n/a | mock.assert_called_once_with(1, 2, 3) |
---|
419 | n/a | mock.assert_called_once_with(a=1, b=2, c=3) |
---|
420 | n/a | self.assertRaises(AssertionError, mock.assert_called_once_with, |
---|
421 | n/a | 1, b=3, c=2) |
---|
422 | n/a | # Expected call doesn't match the spec's signature |
---|
423 | n/a | with self.assertRaises(AssertionError) as cm: |
---|
424 | n/a | mock.assert_called_once_with(e=8) |
---|
425 | n/a | self.assertIsInstance(cm.exception.__cause__, TypeError) |
---|
426 | n/a | # Mock called more than once => always fails |
---|
427 | n/a | mock(4, 5, 6) |
---|
428 | n/a | self.assertRaises(AssertionError, mock.assert_called_once_with, |
---|
429 | n/a | 1, 2, 3) |
---|
430 | n/a | self.assertRaises(AssertionError, mock.assert_called_once_with, |
---|
431 | n/a | 4, 5, 6) |
---|
432 | n/a | |
---|
433 | n/a | |
---|
434 | n/a | def test_attribute_access_returns_mocks(self): |
---|
435 | n/a | mock = Mock() |
---|
436 | n/a | something = mock.something |
---|
437 | n/a | self.assertTrue(is_instance(something, Mock), "attribute isn't a mock") |
---|
438 | n/a | self.assertEqual(mock.something, something, |
---|
439 | n/a | "different attributes returned for same name") |
---|
440 | n/a | |
---|
441 | n/a | # Usage example |
---|
442 | n/a | mock = Mock() |
---|
443 | n/a | mock.something.return_value = 3 |
---|
444 | n/a | |
---|
445 | n/a | self.assertEqual(mock.something(), 3, "method returned wrong value") |
---|
446 | n/a | self.assertTrue(mock.something.called, |
---|
447 | n/a | "method didn't record being called") |
---|
448 | n/a | |
---|
449 | n/a | |
---|
450 | n/a | def test_attributes_have_name_and_parent_set(self): |
---|
451 | n/a | mock = Mock() |
---|
452 | n/a | something = mock.something |
---|
453 | n/a | |
---|
454 | n/a | self.assertEqual(something._mock_name, "something", |
---|
455 | n/a | "attribute name not set correctly") |
---|
456 | n/a | self.assertEqual(something._mock_parent, mock, |
---|
457 | n/a | "attribute parent not set correctly") |
---|
458 | n/a | |
---|
459 | n/a | |
---|
460 | n/a | def test_method_calls_recorded(self): |
---|
461 | n/a | mock = Mock() |
---|
462 | n/a | mock.something(3, fish=None) |
---|
463 | n/a | mock.something_else.something(6, cake=sentinel.Cake) |
---|
464 | n/a | |
---|
465 | n/a | self.assertEqual(mock.something_else.method_calls, |
---|
466 | n/a | [("something", (6,), {'cake': sentinel.Cake})], |
---|
467 | n/a | "method calls not recorded correctly") |
---|
468 | n/a | self.assertEqual(mock.method_calls, [ |
---|
469 | n/a | ("something", (3,), {'fish': None}), |
---|
470 | n/a | ("something_else.something", (6,), {'cake': sentinel.Cake}) |
---|
471 | n/a | ], |
---|
472 | n/a | "method calls not recorded correctly") |
---|
473 | n/a | |
---|
474 | n/a | |
---|
475 | n/a | def test_method_calls_compare_easily(self): |
---|
476 | n/a | mock = Mock() |
---|
477 | n/a | mock.something() |
---|
478 | n/a | self.assertEqual(mock.method_calls, [('something',)]) |
---|
479 | n/a | self.assertEqual(mock.method_calls, [('something', (), {})]) |
---|
480 | n/a | |
---|
481 | n/a | mock = Mock() |
---|
482 | n/a | mock.something('different') |
---|
483 | n/a | self.assertEqual(mock.method_calls, [('something', ('different',))]) |
---|
484 | n/a | self.assertEqual(mock.method_calls, |
---|
485 | n/a | [('something', ('different',), {})]) |
---|
486 | n/a | |
---|
487 | n/a | mock = Mock() |
---|
488 | n/a | mock.something(x=1) |
---|
489 | n/a | self.assertEqual(mock.method_calls, [('something', {'x': 1})]) |
---|
490 | n/a | self.assertEqual(mock.method_calls, [('something', (), {'x': 1})]) |
---|
491 | n/a | |
---|
492 | n/a | mock = Mock() |
---|
493 | n/a | mock.something('different', some='more') |
---|
494 | n/a | self.assertEqual(mock.method_calls, [ |
---|
495 | n/a | ('something', ('different',), {'some': 'more'}) |
---|
496 | n/a | ]) |
---|
497 | n/a | |
---|
498 | n/a | |
---|
499 | n/a | def test_only_allowed_methods_exist(self): |
---|
500 | n/a | for spec in ['something'], ('something',): |
---|
501 | n/a | for arg in 'spec', 'spec_set': |
---|
502 | n/a | mock = Mock(**{arg: spec}) |
---|
503 | n/a | |
---|
504 | n/a | # this should be allowed |
---|
505 | n/a | mock.something |
---|
506 | n/a | self.assertRaisesRegex( |
---|
507 | n/a | AttributeError, |
---|
508 | n/a | "Mock object has no attribute 'something_else'", |
---|
509 | n/a | getattr, mock, 'something_else' |
---|
510 | n/a | ) |
---|
511 | n/a | |
---|
512 | n/a | |
---|
513 | n/a | def test_from_spec(self): |
---|
514 | n/a | class Something(object): |
---|
515 | n/a | x = 3 |
---|
516 | n/a | __something__ = None |
---|
517 | n/a | def y(self): |
---|
518 | n/a | pass |
---|
519 | n/a | |
---|
520 | n/a | def test_attributes(mock): |
---|
521 | n/a | # should work |
---|
522 | n/a | mock.x |
---|
523 | n/a | mock.y |
---|
524 | n/a | mock.__something__ |
---|
525 | n/a | self.assertRaisesRegex( |
---|
526 | n/a | AttributeError, |
---|
527 | n/a | "Mock object has no attribute 'z'", |
---|
528 | n/a | getattr, mock, 'z' |
---|
529 | n/a | ) |
---|
530 | n/a | self.assertRaisesRegex( |
---|
531 | n/a | AttributeError, |
---|
532 | n/a | "Mock object has no attribute '__foobar__'", |
---|
533 | n/a | getattr, mock, '__foobar__' |
---|
534 | n/a | ) |
---|
535 | n/a | |
---|
536 | n/a | test_attributes(Mock(spec=Something)) |
---|
537 | n/a | test_attributes(Mock(spec=Something())) |
---|
538 | n/a | |
---|
539 | n/a | |
---|
540 | n/a | def test_wraps_calls(self): |
---|
541 | n/a | real = Mock() |
---|
542 | n/a | |
---|
543 | n/a | mock = Mock(wraps=real) |
---|
544 | n/a | self.assertEqual(mock(), real()) |
---|
545 | n/a | |
---|
546 | n/a | real.reset_mock() |
---|
547 | n/a | |
---|
548 | n/a | mock(1, 2, fish=3) |
---|
549 | n/a | real.assert_called_with(1, 2, fish=3) |
---|
550 | n/a | |
---|
551 | n/a | |
---|
552 | n/a | def test_wraps_call_with_nondefault_return_value(self): |
---|
553 | n/a | real = Mock() |
---|
554 | n/a | |
---|
555 | n/a | mock = Mock(wraps=real) |
---|
556 | n/a | mock.return_value = 3 |
---|
557 | n/a | |
---|
558 | n/a | self.assertEqual(mock(), 3) |
---|
559 | n/a | self.assertFalse(real.called) |
---|
560 | n/a | |
---|
561 | n/a | |
---|
562 | n/a | def test_wraps_attributes(self): |
---|
563 | n/a | class Real(object): |
---|
564 | n/a | attribute = Mock() |
---|
565 | n/a | |
---|
566 | n/a | real = Real() |
---|
567 | n/a | |
---|
568 | n/a | mock = Mock(wraps=real) |
---|
569 | n/a | self.assertEqual(mock.attribute(), real.attribute()) |
---|
570 | n/a | self.assertRaises(AttributeError, lambda: mock.fish) |
---|
571 | n/a | |
---|
572 | n/a | self.assertNotEqual(mock.attribute, real.attribute) |
---|
573 | n/a | result = mock.attribute.frog(1, 2, fish=3) |
---|
574 | n/a | Real.attribute.frog.assert_called_with(1, 2, fish=3) |
---|
575 | n/a | self.assertEqual(result, Real.attribute.frog()) |
---|
576 | n/a | |
---|
577 | n/a | |
---|
578 | n/a | def test_exceptional_side_effect(self): |
---|
579 | n/a | mock = Mock(side_effect=AttributeError) |
---|
580 | n/a | self.assertRaises(AttributeError, mock) |
---|
581 | n/a | |
---|
582 | n/a | mock = Mock(side_effect=AttributeError('foo')) |
---|
583 | n/a | self.assertRaises(AttributeError, mock) |
---|
584 | n/a | |
---|
585 | n/a | |
---|
586 | n/a | def test_baseexceptional_side_effect(self): |
---|
587 | n/a | mock = Mock(side_effect=KeyboardInterrupt) |
---|
588 | n/a | self.assertRaises(KeyboardInterrupt, mock) |
---|
589 | n/a | |
---|
590 | n/a | mock = Mock(side_effect=KeyboardInterrupt('foo')) |
---|
591 | n/a | self.assertRaises(KeyboardInterrupt, mock) |
---|
592 | n/a | |
---|
593 | n/a | |
---|
594 | n/a | def test_assert_called_with_message(self): |
---|
595 | n/a | mock = Mock() |
---|
596 | n/a | self.assertRaisesRegex(AssertionError, 'Not called', |
---|
597 | n/a | mock.assert_called_with) |
---|
598 | n/a | |
---|
599 | n/a | |
---|
600 | n/a | def test_assert_called_once_with_message(self): |
---|
601 | n/a | mock = Mock(name='geoffrey') |
---|
602 | n/a | self.assertRaisesRegex(AssertionError, |
---|
603 | n/a | r"Expected 'geoffrey' to be called once\.", |
---|
604 | n/a | mock.assert_called_once_with) |
---|
605 | n/a | |
---|
606 | n/a | |
---|
607 | n/a | def test__name__(self): |
---|
608 | n/a | mock = Mock() |
---|
609 | n/a | self.assertRaises(AttributeError, lambda: mock.__name__) |
---|
610 | n/a | |
---|
611 | n/a | mock.__name__ = 'foo' |
---|
612 | n/a | self.assertEqual(mock.__name__, 'foo') |
---|
613 | n/a | |
---|
614 | n/a | |
---|
615 | n/a | def test_spec_list_subclass(self): |
---|
616 | n/a | class Sub(list): |
---|
617 | n/a | pass |
---|
618 | n/a | mock = Mock(spec=Sub(['foo'])) |
---|
619 | n/a | |
---|
620 | n/a | mock.append(3) |
---|
621 | n/a | mock.append.assert_called_with(3) |
---|
622 | n/a | self.assertRaises(AttributeError, getattr, mock, 'foo') |
---|
623 | n/a | |
---|
624 | n/a | |
---|
625 | n/a | def test_spec_class(self): |
---|
626 | n/a | class X(object): |
---|
627 | n/a | pass |
---|
628 | n/a | |
---|
629 | n/a | mock = Mock(spec=X) |
---|
630 | n/a | self.assertIsInstance(mock, X) |
---|
631 | n/a | |
---|
632 | n/a | mock = Mock(spec=X()) |
---|
633 | n/a | self.assertIsInstance(mock, X) |
---|
634 | n/a | |
---|
635 | n/a | self.assertIs(mock.__class__, X) |
---|
636 | n/a | self.assertEqual(Mock().__class__.__name__, 'Mock') |
---|
637 | n/a | |
---|
638 | n/a | mock = Mock(spec_set=X) |
---|
639 | n/a | self.assertIsInstance(mock, X) |
---|
640 | n/a | |
---|
641 | n/a | mock = Mock(spec_set=X()) |
---|
642 | n/a | self.assertIsInstance(mock, X) |
---|
643 | n/a | |
---|
644 | n/a | |
---|
645 | n/a | def test_setting_attribute_with_spec_set(self): |
---|
646 | n/a | class X(object): |
---|
647 | n/a | y = 3 |
---|
648 | n/a | |
---|
649 | n/a | mock = Mock(spec=X) |
---|
650 | n/a | mock.x = 'foo' |
---|
651 | n/a | |
---|
652 | n/a | mock = Mock(spec_set=X) |
---|
653 | n/a | def set_attr(): |
---|
654 | n/a | mock.x = 'foo' |
---|
655 | n/a | |
---|
656 | n/a | mock.y = 'foo' |
---|
657 | n/a | self.assertRaises(AttributeError, set_attr) |
---|
658 | n/a | |
---|
659 | n/a | |
---|
660 | n/a | def test_copy(self): |
---|
661 | n/a | current = sys.getrecursionlimit() |
---|
662 | n/a | self.addCleanup(sys.setrecursionlimit, current) |
---|
663 | n/a | |
---|
664 | n/a | # can't use sys.maxint as this doesn't exist in Python 3 |
---|
665 | n/a | sys.setrecursionlimit(int(10e8)) |
---|
666 | n/a | # this segfaults without the fix in place |
---|
667 | n/a | copy.copy(Mock()) |
---|
668 | n/a | |
---|
669 | n/a | |
---|
670 | n/a | def test_subclass_with_properties(self): |
---|
671 | n/a | class SubClass(Mock): |
---|
672 | n/a | def _get(self): |
---|
673 | n/a | return 3 |
---|
674 | n/a | def _set(self, value): |
---|
675 | n/a | raise NameError('strange error') |
---|
676 | n/a | some_attribute = property(_get, _set) |
---|
677 | n/a | |
---|
678 | n/a | s = SubClass(spec_set=SubClass) |
---|
679 | n/a | self.assertEqual(s.some_attribute, 3) |
---|
680 | n/a | |
---|
681 | n/a | def test(): |
---|
682 | n/a | s.some_attribute = 3 |
---|
683 | n/a | self.assertRaises(NameError, test) |
---|
684 | n/a | |
---|
685 | n/a | def test(): |
---|
686 | n/a | s.foo = 'bar' |
---|
687 | n/a | self.assertRaises(AttributeError, test) |
---|
688 | n/a | |
---|
689 | n/a | |
---|
690 | n/a | def test_setting_call(self): |
---|
691 | n/a | mock = Mock() |
---|
692 | n/a | def __call__(self, a): |
---|
693 | n/a | return self._mock_call(a) |
---|
694 | n/a | |
---|
695 | n/a | type(mock).__call__ = __call__ |
---|
696 | n/a | mock('one') |
---|
697 | n/a | mock.assert_called_with('one') |
---|
698 | n/a | |
---|
699 | n/a | self.assertRaises(TypeError, mock, 'one', 'two') |
---|
700 | n/a | |
---|
701 | n/a | |
---|
702 | n/a | def test_dir(self): |
---|
703 | n/a | mock = Mock() |
---|
704 | n/a | attrs = set(dir(mock)) |
---|
705 | n/a | type_attrs = set([m for m in dir(Mock) if not m.startswith('_')]) |
---|
706 | n/a | |
---|
707 | n/a | # all public attributes from the type are included |
---|
708 | n/a | self.assertEqual(set(), type_attrs - attrs) |
---|
709 | n/a | |
---|
710 | n/a | # creates these attributes |
---|
711 | n/a | mock.a, mock.b |
---|
712 | n/a | self.assertIn('a', dir(mock)) |
---|
713 | n/a | self.assertIn('b', dir(mock)) |
---|
714 | n/a | |
---|
715 | n/a | # instance attributes |
---|
716 | n/a | mock.c = mock.d = None |
---|
717 | n/a | self.assertIn('c', dir(mock)) |
---|
718 | n/a | self.assertIn('d', dir(mock)) |
---|
719 | n/a | |
---|
720 | n/a | # magic methods |
---|
721 | n/a | mock.__iter__ = lambda s: iter([]) |
---|
722 | n/a | self.assertIn('__iter__', dir(mock)) |
---|
723 | n/a | |
---|
724 | n/a | |
---|
725 | n/a | def test_dir_from_spec(self): |
---|
726 | n/a | mock = Mock(spec=unittest.TestCase) |
---|
727 | n/a | testcase_attrs = set(dir(unittest.TestCase)) |
---|
728 | n/a | attrs = set(dir(mock)) |
---|
729 | n/a | |
---|
730 | n/a | # all attributes from the spec are included |
---|
731 | n/a | self.assertEqual(set(), testcase_attrs - attrs) |
---|
732 | n/a | |
---|
733 | n/a | # shadow a sys attribute |
---|
734 | n/a | mock.version = 3 |
---|
735 | n/a | self.assertEqual(dir(mock).count('version'), 1) |
---|
736 | n/a | |
---|
737 | n/a | |
---|
738 | n/a | def test_filter_dir(self): |
---|
739 | n/a | patcher = patch.object(mock, 'FILTER_DIR', False) |
---|
740 | n/a | patcher.start() |
---|
741 | n/a | try: |
---|
742 | n/a | attrs = set(dir(Mock())) |
---|
743 | n/a | type_attrs = set(dir(Mock)) |
---|
744 | n/a | |
---|
745 | n/a | # ALL attributes from the type are included |
---|
746 | n/a | self.assertEqual(set(), type_attrs - attrs) |
---|
747 | n/a | finally: |
---|
748 | n/a | patcher.stop() |
---|
749 | n/a | |
---|
750 | n/a | |
---|
751 | n/a | def test_configure_mock(self): |
---|
752 | n/a | mock = Mock(foo='bar') |
---|
753 | n/a | self.assertEqual(mock.foo, 'bar') |
---|
754 | n/a | |
---|
755 | n/a | mock = MagicMock(foo='bar') |
---|
756 | n/a | self.assertEqual(mock.foo, 'bar') |
---|
757 | n/a | |
---|
758 | n/a | kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33, |
---|
759 | n/a | 'foo': MagicMock()} |
---|
760 | n/a | mock = Mock(**kwargs) |
---|
761 | n/a | self.assertRaises(KeyError, mock) |
---|
762 | n/a | self.assertEqual(mock.foo.bar(), 33) |
---|
763 | n/a | self.assertIsInstance(mock.foo, MagicMock) |
---|
764 | n/a | |
---|
765 | n/a | mock = Mock() |
---|
766 | n/a | mock.configure_mock(**kwargs) |
---|
767 | n/a | self.assertRaises(KeyError, mock) |
---|
768 | n/a | self.assertEqual(mock.foo.bar(), 33) |
---|
769 | n/a | self.assertIsInstance(mock.foo, MagicMock) |
---|
770 | n/a | |
---|
771 | n/a | |
---|
772 | n/a | def assertRaisesWithMsg(self, exception, message, func, *args, **kwargs): |
---|
773 | n/a | # needed because assertRaisesRegex doesn't work easily with newlines |
---|
774 | n/a | try: |
---|
775 | n/a | func(*args, **kwargs) |
---|
776 | n/a | except: |
---|
777 | n/a | instance = sys.exc_info()[1] |
---|
778 | n/a | self.assertIsInstance(instance, exception) |
---|
779 | n/a | else: |
---|
780 | n/a | self.fail('Exception %r not raised' % (exception,)) |
---|
781 | n/a | |
---|
782 | n/a | msg = str(instance) |
---|
783 | n/a | self.assertEqual(msg, message) |
---|
784 | n/a | |
---|
785 | n/a | |
---|
786 | n/a | def test_assert_called_with_failure_message(self): |
---|
787 | n/a | mock = NonCallableMock() |
---|
788 | n/a | |
---|
789 | n/a | expected = "mock(1, '2', 3, bar='foo')" |
---|
790 | n/a | message = 'Expected call: %s\nNot called' |
---|
791 | n/a | self.assertRaisesWithMsg( |
---|
792 | n/a | AssertionError, message % (expected,), |
---|
793 | n/a | mock.assert_called_with, 1, '2', 3, bar='foo' |
---|
794 | n/a | ) |
---|
795 | n/a | |
---|
796 | n/a | mock.foo(1, '2', 3, foo='foo') |
---|
797 | n/a | |
---|
798 | n/a | |
---|
799 | n/a | asserters = [ |
---|
800 | n/a | mock.foo.assert_called_with, mock.foo.assert_called_once_with |
---|
801 | n/a | ] |
---|
802 | n/a | for meth in asserters: |
---|
803 | n/a | actual = "foo(1, '2', 3, foo='foo')" |
---|
804 | n/a | expected = "foo(1, '2', 3, bar='foo')" |
---|
805 | n/a | message = 'Expected call: %s\nActual call: %s' |
---|
806 | n/a | self.assertRaisesWithMsg( |
---|
807 | n/a | AssertionError, message % (expected, actual), |
---|
808 | n/a | meth, 1, '2', 3, bar='foo' |
---|
809 | n/a | ) |
---|
810 | n/a | |
---|
811 | n/a | # just kwargs |
---|
812 | n/a | for meth in asserters: |
---|
813 | n/a | actual = "foo(1, '2', 3, foo='foo')" |
---|
814 | n/a | expected = "foo(bar='foo')" |
---|
815 | n/a | message = 'Expected call: %s\nActual call: %s' |
---|
816 | n/a | self.assertRaisesWithMsg( |
---|
817 | n/a | AssertionError, message % (expected, actual), |
---|
818 | n/a | meth, bar='foo' |
---|
819 | n/a | ) |
---|
820 | n/a | |
---|
821 | n/a | # just args |
---|
822 | n/a | for meth in asserters: |
---|
823 | n/a | actual = "foo(1, '2', 3, foo='foo')" |
---|
824 | n/a | expected = "foo(1, 2, 3)" |
---|
825 | n/a | message = 'Expected call: %s\nActual call: %s' |
---|
826 | n/a | self.assertRaisesWithMsg( |
---|
827 | n/a | AssertionError, message % (expected, actual), |
---|
828 | n/a | meth, 1, 2, 3 |
---|
829 | n/a | ) |
---|
830 | n/a | |
---|
831 | n/a | # empty |
---|
832 | n/a | for meth in asserters: |
---|
833 | n/a | actual = "foo(1, '2', 3, foo='foo')" |
---|
834 | n/a | expected = "foo()" |
---|
835 | n/a | message = 'Expected call: %s\nActual call: %s' |
---|
836 | n/a | self.assertRaisesWithMsg( |
---|
837 | n/a | AssertionError, message % (expected, actual), meth |
---|
838 | n/a | ) |
---|
839 | n/a | |
---|
840 | n/a | |
---|
841 | n/a | def test_mock_calls(self): |
---|
842 | n/a | mock = MagicMock() |
---|
843 | n/a | |
---|
844 | n/a | # need to do this because MagicMock.mock_calls used to just return |
---|
845 | n/a | # a MagicMock which also returned a MagicMock when __eq__ was called |
---|
846 | n/a | self.assertIs(mock.mock_calls == [], True) |
---|
847 | n/a | |
---|
848 | n/a | mock = MagicMock() |
---|
849 | n/a | mock() |
---|
850 | n/a | expected = [('', (), {})] |
---|
851 | n/a | self.assertEqual(mock.mock_calls, expected) |
---|
852 | n/a | |
---|
853 | n/a | mock.foo() |
---|
854 | n/a | expected.append(call.foo()) |
---|
855 | n/a | self.assertEqual(mock.mock_calls, expected) |
---|
856 | n/a | # intermediate mock_calls work too |
---|
857 | n/a | self.assertEqual(mock.foo.mock_calls, [('', (), {})]) |
---|
858 | n/a | |
---|
859 | n/a | mock = MagicMock() |
---|
860 | n/a | mock().foo(1, 2, 3, a=4, b=5) |
---|
861 | n/a | expected = [ |
---|
862 | n/a | ('', (), {}), ('().foo', (1, 2, 3), dict(a=4, b=5)) |
---|
863 | n/a | ] |
---|
864 | n/a | self.assertEqual(mock.mock_calls, expected) |
---|
865 | n/a | self.assertEqual(mock.return_value.foo.mock_calls, |
---|
866 | n/a | [('', (1, 2, 3), dict(a=4, b=5))]) |
---|
867 | n/a | self.assertEqual(mock.return_value.mock_calls, |
---|
868 | n/a | [('foo', (1, 2, 3), dict(a=4, b=5))]) |
---|
869 | n/a | |
---|
870 | n/a | mock = MagicMock() |
---|
871 | n/a | mock().foo.bar().baz() |
---|
872 | n/a | expected = [ |
---|
873 | n/a | ('', (), {}), ('().foo.bar', (), {}), |
---|
874 | n/a | ('().foo.bar().baz', (), {}) |
---|
875 | n/a | ] |
---|
876 | n/a | self.assertEqual(mock.mock_calls, expected) |
---|
877 | n/a | self.assertEqual(mock().mock_calls, |
---|
878 | n/a | call.foo.bar().baz().call_list()) |
---|
879 | n/a | |
---|
880 | n/a | for kwargs in dict(), dict(name='bar'): |
---|
881 | n/a | mock = MagicMock(**kwargs) |
---|
882 | n/a | int(mock.foo) |
---|
883 | n/a | expected = [('foo.__int__', (), {})] |
---|
884 | n/a | self.assertEqual(mock.mock_calls, expected) |
---|
885 | n/a | |
---|
886 | n/a | mock = MagicMock(**kwargs) |
---|
887 | n/a | mock.a()() |
---|
888 | n/a | expected = [('a', (), {}), ('a()', (), {})] |
---|
889 | n/a | self.assertEqual(mock.mock_calls, expected) |
---|
890 | n/a | self.assertEqual(mock.a().mock_calls, [call()]) |
---|
891 | n/a | |
---|
892 | n/a | mock = MagicMock(**kwargs) |
---|
893 | n/a | mock(1)(2)(3) |
---|
894 | n/a | self.assertEqual(mock.mock_calls, call(1)(2)(3).call_list()) |
---|
895 | n/a | self.assertEqual(mock().mock_calls, call(2)(3).call_list()) |
---|
896 | n/a | self.assertEqual(mock()().mock_calls, call(3).call_list()) |
---|
897 | n/a | |
---|
898 | n/a | mock = MagicMock(**kwargs) |
---|
899 | n/a | mock(1)(2)(3).a.b.c(4) |
---|
900 | n/a | self.assertEqual(mock.mock_calls, |
---|
901 | n/a | call(1)(2)(3).a.b.c(4).call_list()) |
---|
902 | n/a | self.assertEqual(mock().mock_calls, |
---|
903 | n/a | call(2)(3).a.b.c(4).call_list()) |
---|
904 | n/a | self.assertEqual(mock()().mock_calls, |
---|
905 | n/a | call(3).a.b.c(4).call_list()) |
---|
906 | n/a | |
---|
907 | n/a | mock = MagicMock(**kwargs) |
---|
908 | n/a | int(mock().foo.bar().baz()) |
---|
909 | n/a | last_call = ('().foo.bar().baz().__int__', (), {}) |
---|
910 | n/a | self.assertEqual(mock.mock_calls[-1], last_call) |
---|
911 | n/a | self.assertEqual(mock().mock_calls, |
---|
912 | n/a | call.foo.bar().baz().__int__().call_list()) |
---|
913 | n/a | self.assertEqual(mock().foo.bar().mock_calls, |
---|
914 | n/a | call.baz().__int__().call_list()) |
---|
915 | n/a | self.assertEqual(mock().foo.bar().baz.mock_calls, |
---|
916 | n/a | call().__int__().call_list()) |
---|
917 | n/a | |
---|
918 | n/a | |
---|
919 | n/a | def test_subclassing(self): |
---|
920 | n/a | class Subclass(Mock): |
---|
921 | n/a | pass |
---|
922 | n/a | |
---|
923 | n/a | mock = Subclass() |
---|
924 | n/a | self.assertIsInstance(mock.foo, Subclass) |
---|
925 | n/a | self.assertIsInstance(mock(), Subclass) |
---|
926 | n/a | |
---|
927 | n/a | class Subclass(Mock): |
---|
928 | n/a | def _get_child_mock(self, **kwargs): |
---|
929 | n/a | return Mock(**kwargs) |
---|
930 | n/a | |
---|
931 | n/a | mock = Subclass() |
---|
932 | n/a | self.assertNotIsInstance(mock.foo, Subclass) |
---|
933 | n/a | self.assertNotIsInstance(mock(), Subclass) |
---|
934 | n/a | |
---|
935 | n/a | |
---|
936 | n/a | def test_arg_lists(self): |
---|
937 | n/a | mocks = [ |
---|
938 | n/a | Mock(), |
---|
939 | n/a | MagicMock(), |
---|
940 | n/a | NonCallableMock(), |
---|
941 | n/a | NonCallableMagicMock() |
---|
942 | n/a | ] |
---|
943 | n/a | |
---|
944 | n/a | def assert_attrs(mock): |
---|
945 | n/a | names = 'call_args_list', 'method_calls', 'mock_calls' |
---|
946 | n/a | for name in names: |
---|
947 | n/a | attr = getattr(mock, name) |
---|
948 | n/a | self.assertIsInstance(attr, _CallList) |
---|
949 | n/a | self.assertIsInstance(attr, list) |
---|
950 | n/a | self.assertEqual(attr, []) |
---|
951 | n/a | |
---|
952 | n/a | for mock in mocks: |
---|
953 | n/a | assert_attrs(mock) |
---|
954 | n/a | |
---|
955 | n/a | if callable(mock): |
---|
956 | n/a | mock() |
---|
957 | n/a | mock(1, 2) |
---|
958 | n/a | mock(a=3) |
---|
959 | n/a | |
---|
960 | n/a | mock.reset_mock() |
---|
961 | n/a | assert_attrs(mock) |
---|
962 | n/a | |
---|
963 | n/a | mock.foo() |
---|
964 | n/a | mock.foo.bar(1, a=3) |
---|
965 | n/a | mock.foo(1).bar().baz(3) |
---|
966 | n/a | |
---|
967 | n/a | mock.reset_mock() |
---|
968 | n/a | assert_attrs(mock) |
---|
969 | n/a | |
---|
970 | n/a | |
---|
971 | n/a | def test_call_args_two_tuple(self): |
---|
972 | n/a | mock = Mock() |
---|
973 | n/a | mock(1, a=3) |
---|
974 | n/a | mock(2, b=4) |
---|
975 | n/a | |
---|
976 | n/a | self.assertEqual(len(mock.call_args), 2) |
---|
977 | n/a | args, kwargs = mock.call_args |
---|
978 | n/a | self.assertEqual(args, (2,)) |
---|
979 | n/a | self.assertEqual(kwargs, dict(b=4)) |
---|
980 | n/a | |
---|
981 | n/a | expected_list = [((1,), dict(a=3)), ((2,), dict(b=4))] |
---|
982 | n/a | for expected, call_args in zip(expected_list, mock.call_args_list): |
---|
983 | n/a | self.assertEqual(len(call_args), 2) |
---|
984 | n/a | self.assertEqual(expected[0], call_args[0]) |
---|
985 | n/a | self.assertEqual(expected[1], call_args[1]) |
---|
986 | n/a | |
---|
987 | n/a | |
---|
988 | n/a | def test_side_effect_iterator(self): |
---|
989 | n/a | mock = Mock(side_effect=iter([1, 2, 3])) |
---|
990 | n/a | self.assertEqual([mock(), mock(), mock()], [1, 2, 3]) |
---|
991 | n/a | self.assertRaises(StopIteration, mock) |
---|
992 | n/a | |
---|
993 | n/a | mock = MagicMock(side_effect=['a', 'b', 'c']) |
---|
994 | n/a | self.assertEqual([mock(), mock(), mock()], ['a', 'b', 'c']) |
---|
995 | n/a | self.assertRaises(StopIteration, mock) |
---|
996 | n/a | |
---|
997 | n/a | mock = Mock(side_effect='ghi') |
---|
998 | n/a | self.assertEqual([mock(), mock(), mock()], ['g', 'h', 'i']) |
---|
999 | n/a | self.assertRaises(StopIteration, mock) |
---|
1000 | n/a | |
---|
1001 | n/a | class Foo(object): |
---|
1002 | n/a | pass |
---|
1003 | n/a | mock = MagicMock(side_effect=Foo) |
---|
1004 | n/a | self.assertIsInstance(mock(), Foo) |
---|
1005 | n/a | |
---|
1006 | n/a | mock = Mock(side_effect=Iter()) |
---|
1007 | n/a | self.assertEqual([mock(), mock(), mock(), mock()], |
---|
1008 | n/a | ['this', 'is', 'an', 'iter']) |
---|
1009 | n/a | self.assertRaises(StopIteration, mock) |
---|
1010 | n/a | |
---|
1011 | n/a | |
---|
1012 | n/a | def test_side_effect_iterator_exceptions(self): |
---|
1013 | n/a | for Klass in Mock, MagicMock: |
---|
1014 | n/a | iterable = (ValueError, 3, KeyError, 6) |
---|
1015 | n/a | m = Klass(side_effect=iterable) |
---|
1016 | n/a | self.assertRaises(ValueError, m) |
---|
1017 | n/a | self.assertEqual(m(), 3) |
---|
1018 | n/a | self.assertRaises(KeyError, m) |
---|
1019 | n/a | self.assertEqual(m(), 6) |
---|
1020 | n/a | |
---|
1021 | n/a | |
---|
1022 | n/a | def test_side_effect_setting_iterator(self): |
---|
1023 | n/a | mock = Mock() |
---|
1024 | n/a | mock.side_effect = iter([1, 2, 3]) |
---|
1025 | n/a | self.assertEqual([mock(), mock(), mock()], [1, 2, 3]) |
---|
1026 | n/a | self.assertRaises(StopIteration, mock) |
---|
1027 | n/a | side_effect = mock.side_effect |
---|
1028 | n/a | self.assertIsInstance(side_effect, type(iter([]))) |
---|
1029 | n/a | |
---|
1030 | n/a | mock.side_effect = ['a', 'b', 'c'] |
---|
1031 | n/a | self.assertEqual([mock(), mock(), mock()], ['a', 'b', 'c']) |
---|
1032 | n/a | self.assertRaises(StopIteration, mock) |
---|
1033 | n/a | side_effect = mock.side_effect |
---|
1034 | n/a | self.assertIsInstance(side_effect, type(iter([]))) |
---|
1035 | n/a | |
---|
1036 | n/a | this_iter = Iter() |
---|
1037 | n/a | mock.side_effect = this_iter |
---|
1038 | n/a | self.assertEqual([mock(), mock(), mock(), mock()], |
---|
1039 | n/a | ['this', 'is', 'an', 'iter']) |
---|
1040 | n/a | self.assertRaises(StopIteration, mock) |
---|
1041 | n/a | self.assertIs(mock.side_effect, this_iter) |
---|
1042 | n/a | |
---|
1043 | n/a | def test_side_effect_iterator_default(self): |
---|
1044 | n/a | mock = Mock(return_value=2) |
---|
1045 | n/a | mock.side_effect = iter([1, DEFAULT]) |
---|
1046 | n/a | self.assertEqual([mock(), mock()], [1, 2]) |
---|
1047 | n/a | |
---|
1048 | n/a | def test_assert_has_calls_any_order(self): |
---|
1049 | n/a | mock = Mock() |
---|
1050 | n/a | mock(1, 2) |
---|
1051 | n/a | mock(a=3) |
---|
1052 | n/a | mock(3, 4) |
---|
1053 | n/a | mock(b=6) |
---|
1054 | n/a | mock(b=6) |
---|
1055 | n/a | |
---|
1056 | n/a | kalls = [ |
---|
1057 | n/a | call(1, 2), ({'a': 3},), |
---|
1058 | n/a | ((3, 4),), ((), {'a': 3}), |
---|
1059 | n/a | ('', (1, 2)), ('', {'a': 3}), |
---|
1060 | n/a | ('', (1, 2), {}), ('', (), {'a': 3}) |
---|
1061 | n/a | ] |
---|
1062 | n/a | for kall in kalls: |
---|
1063 | n/a | mock.assert_has_calls([kall], any_order=True) |
---|
1064 | n/a | |
---|
1065 | n/a | for kall in call(1, '2'), call(b=3), call(), 3, None, 'foo': |
---|
1066 | n/a | self.assertRaises( |
---|
1067 | n/a | AssertionError, mock.assert_has_calls, |
---|
1068 | n/a | [kall], any_order=True |
---|
1069 | n/a | ) |
---|
1070 | n/a | |
---|
1071 | n/a | kall_lists = [ |
---|
1072 | n/a | [call(1, 2), call(b=6)], |
---|
1073 | n/a | [call(3, 4), call(1, 2)], |
---|
1074 | n/a | [call(b=6), call(b=6)], |
---|
1075 | n/a | ] |
---|
1076 | n/a | |
---|
1077 | n/a | for kall_list in kall_lists: |
---|
1078 | n/a | mock.assert_has_calls(kall_list, any_order=True) |
---|
1079 | n/a | |
---|
1080 | n/a | kall_lists = [ |
---|
1081 | n/a | [call(b=6), call(b=6), call(b=6)], |
---|
1082 | n/a | [call(1, 2), call(1, 2)], |
---|
1083 | n/a | [call(3, 4), call(1, 2), call(5, 7)], |
---|
1084 | n/a | [call(b=6), call(3, 4), call(b=6), call(1, 2), call(b=6)], |
---|
1085 | n/a | ] |
---|
1086 | n/a | for kall_list in kall_lists: |
---|
1087 | n/a | self.assertRaises( |
---|
1088 | n/a | AssertionError, mock.assert_has_calls, |
---|
1089 | n/a | kall_list, any_order=True |
---|
1090 | n/a | ) |
---|
1091 | n/a | |
---|
1092 | n/a | def test_assert_has_calls(self): |
---|
1093 | n/a | kalls1 = [ |
---|
1094 | n/a | call(1, 2), ({'a': 3},), |
---|
1095 | n/a | ((3, 4),), call(b=6), |
---|
1096 | n/a | ('', (1,), {'b': 6}), |
---|
1097 | n/a | ] |
---|
1098 | n/a | kalls2 = [call.foo(), call.bar(1)] |
---|
1099 | n/a | kalls2.extend(call.spam().baz(a=3).call_list()) |
---|
1100 | n/a | kalls2.extend(call.bam(set(), foo={}).fish([1]).call_list()) |
---|
1101 | n/a | |
---|
1102 | n/a | mocks = [] |
---|
1103 | n/a | for mock in Mock(), MagicMock(): |
---|
1104 | n/a | mock(1, 2) |
---|
1105 | n/a | mock(a=3) |
---|
1106 | n/a | mock(3, 4) |
---|
1107 | n/a | mock(b=6) |
---|
1108 | n/a | mock(1, b=6) |
---|
1109 | n/a | mocks.append((mock, kalls1)) |
---|
1110 | n/a | |
---|
1111 | n/a | mock = Mock() |
---|
1112 | n/a | mock.foo() |
---|
1113 | n/a | mock.bar(1) |
---|
1114 | n/a | mock.spam().baz(a=3) |
---|
1115 | n/a | mock.bam(set(), foo={}).fish([1]) |
---|
1116 | n/a | mocks.append((mock, kalls2)) |
---|
1117 | n/a | |
---|
1118 | n/a | for mock, kalls in mocks: |
---|
1119 | n/a | for i in range(len(kalls)): |
---|
1120 | n/a | for step in 1, 2, 3: |
---|
1121 | n/a | these = kalls[i:i+step] |
---|
1122 | n/a | mock.assert_has_calls(these) |
---|
1123 | n/a | |
---|
1124 | n/a | if len(these) > 1: |
---|
1125 | n/a | self.assertRaises( |
---|
1126 | n/a | AssertionError, |
---|
1127 | n/a | mock.assert_has_calls, |
---|
1128 | n/a | list(reversed(these)) |
---|
1129 | n/a | ) |
---|
1130 | n/a | |
---|
1131 | n/a | |
---|
1132 | n/a | def test_assert_has_calls_with_function_spec(self): |
---|
1133 | n/a | def f(a, b, c, d=None): |
---|
1134 | n/a | pass |
---|
1135 | n/a | |
---|
1136 | n/a | mock = Mock(spec=f) |
---|
1137 | n/a | |
---|
1138 | n/a | mock(1, b=2, c=3) |
---|
1139 | n/a | mock(4, 5, c=6, d=7) |
---|
1140 | n/a | mock(10, 11, c=12) |
---|
1141 | n/a | calls = [ |
---|
1142 | n/a | ('', (1, 2, 3), {}), |
---|
1143 | n/a | ('', (4, 5, 6), {'d': 7}), |
---|
1144 | n/a | ((10, 11, 12), {}), |
---|
1145 | n/a | ] |
---|
1146 | n/a | mock.assert_has_calls(calls) |
---|
1147 | n/a | mock.assert_has_calls(calls, any_order=True) |
---|
1148 | n/a | mock.assert_has_calls(calls[1:]) |
---|
1149 | n/a | mock.assert_has_calls(calls[1:], any_order=True) |
---|
1150 | n/a | mock.assert_has_calls(calls[:-1]) |
---|
1151 | n/a | mock.assert_has_calls(calls[:-1], any_order=True) |
---|
1152 | n/a | # Reversed order |
---|
1153 | n/a | calls = list(reversed(calls)) |
---|
1154 | n/a | with self.assertRaises(AssertionError): |
---|
1155 | n/a | mock.assert_has_calls(calls) |
---|
1156 | n/a | mock.assert_has_calls(calls, any_order=True) |
---|
1157 | n/a | with self.assertRaises(AssertionError): |
---|
1158 | n/a | mock.assert_has_calls(calls[1:]) |
---|
1159 | n/a | mock.assert_has_calls(calls[1:], any_order=True) |
---|
1160 | n/a | with self.assertRaises(AssertionError): |
---|
1161 | n/a | mock.assert_has_calls(calls[:-1]) |
---|
1162 | n/a | mock.assert_has_calls(calls[:-1], any_order=True) |
---|
1163 | n/a | |
---|
1164 | n/a | |
---|
1165 | n/a | def test_assert_any_call(self): |
---|
1166 | n/a | mock = Mock() |
---|
1167 | n/a | mock(1, 2) |
---|
1168 | n/a | mock(a=3) |
---|
1169 | n/a | mock(1, b=6) |
---|
1170 | n/a | |
---|
1171 | n/a | mock.assert_any_call(1, 2) |
---|
1172 | n/a | mock.assert_any_call(a=3) |
---|
1173 | n/a | mock.assert_any_call(1, b=6) |
---|
1174 | n/a | |
---|
1175 | n/a | self.assertRaises( |
---|
1176 | n/a | AssertionError, |
---|
1177 | n/a | mock.assert_any_call |
---|
1178 | n/a | ) |
---|
1179 | n/a | self.assertRaises( |
---|
1180 | n/a | AssertionError, |
---|
1181 | n/a | mock.assert_any_call, |
---|
1182 | n/a | 1, 3 |
---|
1183 | n/a | ) |
---|
1184 | n/a | self.assertRaises( |
---|
1185 | n/a | AssertionError, |
---|
1186 | n/a | mock.assert_any_call, |
---|
1187 | n/a | a=4 |
---|
1188 | n/a | ) |
---|
1189 | n/a | |
---|
1190 | n/a | |
---|
1191 | n/a | def test_assert_any_call_with_function_spec(self): |
---|
1192 | n/a | def f(a, b, c, d=None): |
---|
1193 | n/a | pass |
---|
1194 | n/a | |
---|
1195 | n/a | mock = Mock(spec=f) |
---|
1196 | n/a | |
---|
1197 | n/a | mock(1, b=2, c=3) |
---|
1198 | n/a | mock(4, 5, c=6, d=7) |
---|
1199 | n/a | mock.assert_any_call(1, 2, 3) |
---|
1200 | n/a | mock.assert_any_call(a=1, b=2, c=3) |
---|
1201 | n/a | mock.assert_any_call(4, 5, 6, 7) |
---|
1202 | n/a | mock.assert_any_call(a=4, b=5, c=6, d=7) |
---|
1203 | n/a | self.assertRaises(AssertionError, mock.assert_any_call, |
---|
1204 | n/a | 1, b=3, c=2) |
---|
1205 | n/a | # Expected call doesn't match the spec's signature |
---|
1206 | n/a | with self.assertRaises(AssertionError) as cm: |
---|
1207 | n/a | mock.assert_any_call(e=8) |
---|
1208 | n/a | self.assertIsInstance(cm.exception.__cause__, TypeError) |
---|
1209 | n/a | |
---|
1210 | n/a | |
---|
1211 | n/a | def test_mock_calls_create_autospec(self): |
---|
1212 | n/a | def f(a, b): |
---|
1213 | n/a | pass |
---|
1214 | n/a | obj = Iter() |
---|
1215 | n/a | obj.f = f |
---|
1216 | n/a | |
---|
1217 | n/a | funcs = [ |
---|
1218 | n/a | create_autospec(f), |
---|
1219 | n/a | create_autospec(obj).f |
---|
1220 | n/a | ] |
---|
1221 | n/a | for func in funcs: |
---|
1222 | n/a | func(1, 2) |
---|
1223 | n/a | func(3, 4) |
---|
1224 | n/a | |
---|
1225 | n/a | self.assertEqual( |
---|
1226 | n/a | func.mock_calls, [call(1, 2), call(3, 4)] |
---|
1227 | n/a | ) |
---|
1228 | n/a | |
---|
1229 | n/a | #Issue21222 |
---|
1230 | n/a | def test_create_autospec_with_name(self): |
---|
1231 | n/a | m = mock.create_autospec(object(), name='sweet_func') |
---|
1232 | n/a | self.assertIn('sweet_func', repr(m)) |
---|
1233 | n/a | |
---|
1234 | n/a | #Issue21238 |
---|
1235 | n/a | def test_mock_unsafe(self): |
---|
1236 | n/a | m = Mock() |
---|
1237 | n/a | with self.assertRaises(AttributeError): |
---|
1238 | n/a | m.assert_foo_call() |
---|
1239 | n/a | with self.assertRaises(AttributeError): |
---|
1240 | n/a | m.assret_foo_call() |
---|
1241 | n/a | m = Mock(unsafe=True) |
---|
1242 | n/a | m.assert_foo_call() |
---|
1243 | n/a | m.assret_foo_call() |
---|
1244 | n/a | |
---|
1245 | n/a | #Issue21262 |
---|
1246 | n/a | def test_assert_not_called(self): |
---|
1247 | n/a | m = Mock() |
---|
1248 | n/a | m.hello.assert_not_called() |
---|
1249 | n/a | m.hello() |
---|
1250 | n/a | with self.assertRaises(AssertionError): |
---|
1251 | n/a | m.hello.assert_not_called() |
---|
1252 | n/a | |
---|
1253 | n/a | def test_assert_called(self): |
---|
1254 | n/a | m = Mock() |
---|
1255 | n/a | with self.assertRaises(AssertionError): |
---|
1256 | n/a | m.hello.assert_called() |
---|
1257 | n/a | m.hello() |
---|
1258 | n/a | m.hello.assert_called() |
---|
1259 | n/a | |
---|
1260 | n/a | m.hello() |
---|
1261 | n/a | m.hello.assert_called() |
---|
1262 | n/a | |
---|
1263 | n/a | def test_assert_called_once(self): |
---|
1264 | n/a | m = Mock() |
---|
1265 | n/a | with self.assertRaises(AssertionError): |
---|
1266 | n/a | m.hello.assert_called_once() |
---|
1267 | n/a | m.hello() |
---|
1268 | n/a | m.hello.assert_called_once() |
---|
1269 | n/a | |
---|
1270 | n/a | m.hello() |
---|
1271 | n/a | with self.assertRaises(AssertionError): |
---|
1272 | n/a | m.hello.assert_called_once() |
---|
1273 | n/a | |
---|
1274 | n/a | #Issue21256 printout of keyword args should be in deterministic order |
---|
1275 | n/a | def test_sorted_call_signature(self): |
---|
1276 | n/a | m = Mock() |
---|
1277 | n/a | m.hello(name='hello', daddy='hero') |
---|
1278 | n/a | text = "call(daddy='hero', name='hello')" |
---|
1279 | n/a | self.assertEqual(repr(m.hello.call_args), text) |
---|
1280 | n/a | |
---|
1281 | n/a | #Issue21270 overrides tuple methods for mock.call objects |
---|
1282 | n/a | def test_override_tuple_methods(self): |
---|
1283 | n/a | c = call.count() |
---|
1284 | n/a | i = call.index(132,'hello') |
---|
1285 | n/a | m = Mock() |
---|
1286 | n/a | m.count() |
---|
1287 | n/a | m.index(132,"hello") |
---|
1288 | n/a | self.assertEqual(m.method_calls[0], c) |
---|
1289 | n/a | self.assertEqual(m.method_calls[1], i) |
---|
1290 | n/a | |
---|
1291 | n/a | def test_reset_return_sideeffect(self): |
---|
1292 | n/a | m = Mock(return_value=10, side_effect=[2,3]) |
---|
1293 | n/a | m.reset_mock(return_value=True, side_effect=True) |
---|
1294 | n/a | self.assertIsInstance(m.return_value, Mock) |
---|
1295 | n/a | self.assertEqual(m.side_effect, None) |
---|
1296 | n/a | |
---|
1297 | n/a | def test_reset_return(self): |
---|
1298 | n/a | m = Mock(return_value=10, side_effect=[2,3]) |
---|
1299 | n/a | m.reset_mock(return_value=True) |
---|
1300 | n/a | self.assertIsInstance(m.return_value, Mock) |
---|
1301 | n/a | self.assertNotEqual(m.side_effect, None) |
---|
1302 | n/a | |
---|
1303 | n/a | def test_reset_sideeffect(self): |
---|
1304 | n/a | m = Mock(return_value=10, side_effect=[2,3]) |
---|
1305 | n/a | m.reset_mock(side_effect=True) |
---|
1306 | n/a | self.assertEqual(m.return_value, 10) |
---|
1307 | n/a | self.assertEqual(m.side_effect, None) |
---|
1308 | n/a | |
---|
1309 | n/a | def test_mock_add_spec(self): |
---|
1310 | n/a | class _One(object): |
---|
1311 | n/a | one = 1 |
---|
1312 | n/a | class _Two(object): |
---|
1313 | n/a | two = 2 |
---|
1314 | n/a | class Anything(object): |
---|
1315 | n/a | one = two = three = 'four' |
---|
1316 | n/a | |
---|
1317 | n/a | klasses = [ |
---|
1318 | n/a | Mock, MagicMock, NonCallableMock, NonCallableMagicMock |
---|
1319 | n/a | ] |
---|
1320 | n/a | for Klass in list(klasses): |
---|
1321 | n/a | klasses.append(lambda K=Klass: K(spec=Anything)) |
---|
1322 | n/a | klasses.append(lambda K=Klass: K(spec_set=Anything)) |
---|
1323 | n/a | |
---|
1324 | n/a | for Klass in klasses: |
---|
1325 | n/a | for kwargs in dict(), dict(spec_set=True): |
---|
1326 | n/a | mock = Klass() |
---|
1327 | n/a | #no error |
---|
1328 | n/a | mock.one, mock.two, mock.three |
---|
1329 | n/a | |
---|
1330 | n/a | for One, Two in [(_One, _Two), (['one'], ['two'])]: |
---|
1331 | n/a | for kwargs in dict(), dict(spec_set=True): |
---|
1332 | n/a | mock.mock_add_spec(One, **kwargs) |
---|
1333 | n/a | |
---|
1334 | n/a | mock.one |
---|
1335 | n/a | self.assertRaises( |
---|
1336 | n/a | AttributeError, getattr, mock, 'two' |
---|
1337 | n/a | ) |
---|
1338 | n/a | self.assertRaises( |
---|
1339 | n/a | AttributeError, getattr, mock, 'three' |
---|
1340 | n/a | ) |
---|
1341 | n/a | if 'spec_set' in kwargs: |
---|
1342 | n/a | self.assertRaises( |
---|
1343 | n/a | AttributeError, setattr, mock, 'three', None |
---|
1344 | n/a | ) |
---|
1345 | n/a | |
---|
1346 | n/a | mock.mock_add_spec(Two, **kwargs) |
---|
1347 | n/a | self.assertRaises( |
---|
1348 | n/a | AttributeError, getattr, mock, 'one' |
---|
1349 | n/a | ) |
---|
1350 | n/a | mock.two |
---|
1351 | n/a | self.assertRaises( |
---|
1352 | n/a | AttributeError, getattr, mock, 'three' |
---|
1353 | n/a | ) |
---|
1354 | n/a | if 'spec_set' in kwargs: |
---|
1355 | n/a | self.assertRaises( |
---|
1356 | n/a | AttributeError, setattr, mock, 'three', None |
---|
1357 | n/a | ) |
---|
1358 | n/a | # note that creating a mock, setting an instance attribute, and |
---|
1359 | n/a | # *then* setting a spec doesn't work. Not the intended use case |
---|
1360 | n/a | |
---|
1361 | n/a | |
---|
1362 | n/a | def test_mock_add_spec_magic_methods(self): |
---|
1363 | n/a | for Klass in MagicMock, NonCallableMagicMock: |
---|
1364 | n/a | mock = Klass() |
---|
1365 | n/a | int(mock) |
---|
1366 | n/a | |
---|
1367 | n/a | mock.mock_add_spec(object) |
---|
1368 | n/a | self.assertRaises(TypeError, int, mock) |
---|
1369 | n/a | |
---|
1370 | n/a | mock = Klass() |
---|
1371 | n/a | mock['foo'] |
---|
1372 | n/a | mock.__int__.return_value =4 |
---|
1373 | n/a | |
---|
1374 | n/a | mock.mock_add_spec(int) |
---|
1375 | n/a | self.assertEqual(int(mock), 4) |
---|
1376 | n/a | self.assertRaises(TypeError, lambda: mock['foo']) |
---|
1377 | n/a | |
---|
1378 | n/a | |
---|
1379 | n/a | def test_adding_child_mock(self): |
---|
1380 | n/a | for Klass in NonCallableMock, Mock, MagicMock, NonCallableMagicMock: |
---|
1381 | n/a | mock = Klass() |
---|
1382 | n/a | |
---|
1383 | n/a | mock.foo = Mock() |
---|
1384 | n/a | mock.foo() |
---|
1385 | n/a | |
---|
1386 | n/a | self.assertEqual(mock.method_calls, [call.foo()]) |
---|
1387 | n/a | self.assertEqual(mock.mock_calls, [call.foo()]) |
---|
1388 | n/a | |
---|
1389 | n/a | mock = Klass() |
---|
1390 | n/a | mock.bar = Mock(name='name') |
---|
1391 | n/a | mock.bar() |
---|
1392 | n/a | self.assertEqual(mock.method_calls, []) |
---|
1393 | n/a | self.assertEqual(mock.mock_calls, []) |
---|
1394 | n/a | |
---|
1395 | n/a | # mock with an existing _new_parent but no name |
---|
1396 | n/a | mock = Klass() |
---|
1397 | n/a | mock.baz = MagicMock()() |
---|
1398 | n/a | mock.baz() |
---|
1399 | n/a | self.assertEqual(mock.method_calls, []) |
---|
1400 | n/a | self.assertEqual(mock.mock_calls, []) |
---|
1401 | n/a | |
---|
1402 | n/a | |
---|
1403 | n/a | def test_adding_return_value_mock(self): |
---|
1404 | n/a | for Klass in Mock, MagicMock: |
---|
1405 | n/a | mock = Klass() |
---|
1406 | n/a | mock.return_value = MagicMock() |
---|
1407 | n/a | |
---|
1408 | n/a | mock()() |
---|
1409 | n/a | self.assertEqual(mock.mock_calls, [call(), call()()]) |
---|
1410 | n/a | |
---|
1411 | n/a | |
---|
1412 | n/a | def test_manager_mock(self): |
---|
1413 | n/a | class Foo(object): |
---|
1414 | n/a | one = 'one' |
---|
1415 | n/a | two = 'two' |
---|
1416 | n/a | manager = Mock() |
---|
1417 | n/a | p1 = patch.object(Foo, 'one') |
---|
1418 | n/a | p2 = patch.object(Foo, 'two') |
---|
1419 | n/a | |
---|
1420 | n/a | mock_one = p1.start() |
---|
1421 | n/a | self.addCleanup(p1.stop) |
---|
1422 | n/a | mock_two = p2.start() |
---|
1423 | n/a | self.addCleanup(p2.stop) |
---|
1424 | n/a | |
---|
1425 | n/a | manager.attach_mock(mock_one, 'one') |
---|
1426 | n/a | manager.attach_mock(mock_two, 'two') |
---|
1427 | n/a | |
---|
1428 | n/a | Foo.two() |
---|
1429 | n/a | Foo.one() |
---|
1430 | n/a | |
---|
1431 | n/a | self.assertEqual(manager.mock_calls, [call.two(), call.one()]) |
---|
1432 | n/a | |
---|
1433 | n/a | |
---|
1434 | n/a | def test_magic_methods_mock_calls(self): |
---|
1435 | n/a | for Klass in Mock, MagicMock: |
---|
1436 | n/a | m = Klass() |
---|
1437 | n/a | m.__int__ = Mock(return_value=3) |
---|
1438 | n/a | m.__float__ = MagicMock(return_value=3.0) |
---|
1439 | n/a | int(m) |
---|
1440 | n/a | float(m) |
---|
1441 | n/a | |
---|
1442 | n/a | self.assertEqual(m.mock_calls, [call.__int__(), call.__float__()]) |
---|
1443 | n/a | self.assertEqual(m.method_calls, []) |
---|
1444 | n/a | |
---|
1445 | n/a | def test_mock_open_reuse_issue_21750(self): |
---|
1446 | n/a | mocked_open = mock.mock_open(read_data='data') |
---|
1447 | n/a | f1 = mocked_open('a-name') |
---|
1448 | n/a | f1_data = f1.read() |
---|
1449 | n/a | f2 = mocked_open('another-name') |
---|
1450 | n/a | f2_data = f2.read() |
---|
1451 | n/a | self.assertEqual(f1_data, f2_data) |
---|
1452 | n/a | |
---|
1453 | n/a | def test_mock_open_write(self): |
---|
1454 | n/a | # Test exception in file writing write() |
---|
1455 | n/a | mock_namedtemp = mock.mock_open(mock.MagicMock(name='JLV')) |
---|
1456 | n/a | with mock.patch('tempfile.NamedTemporaryFile', mock_namedtemp): |
---|
1457 | n/a | mock_filehandle = mock_namedtemp.return_value |
---|
1458 | n/a | mock_write = mock_filehandle.write |
---|
1459 | n/a | mock_write.side_effect = OSError('Test 2 Error') |
---|
1460 | n/a | def attempt(): |
---|
1461 | n/a | tempfile.NamedTemporaryFile().write('asd') |
---|
1462 | n/a | self.assertRaises(OSError, attempt) |
---|
1463 | n/a | |
---|
1464 | n/a | def test_mock_open_alter_readline(self): |
---|
1465 | n/a | mopen = mock.mock_open(read_data='foo\nbarn') |
---|
1466 | n/a | mopen.return_value.readline.side_effect = lambda *args:'abc' |
---|
1467 | n/a | first = mopen().readline() |
---|
1468 | n/a | second = mopen().readline() |
---|
1469 | n/a | self.assertEqual('abc', first) |
---|
1470 | n/a | self.assertEqual('abc', second) |
---|
1471 | n/a | |
---|
1472 | n/a | def test_mock_open_after_eof(self): |
---|
1473 | n/a | # read, readline and readlines should work after end of file. |
---|
1474 | n/a | _open = mock.mock_open(read_data='foo') |
---|
1475 | n/a | h = _open('bar') |
---|
1476 | n/a | h.read() |
---|
1477 | n/a | self.assertEqual('', h.read()) |
---|
1478 | n/a | self.assertEqual('', h.read()) |
---|
1479 | n/a | self.assertEqual('', h.readline()) |
---|
1480 | n/a | self.assertEqual('', h.readline()) |
---|
1481 | n/a | self.assertEqual([], h.readlines()) |
---|
1482 | n/a | self.assertEqual([], h.readlines()) |
---|
1483 | n/a | |
---|
1484 | n/a | def test_mock_parents(self): |
---|
1485 | n/a | for Klass in Mock, MagicMock: |
---|
1486 | n/a | m = Klass() |
---|
1487 | n/a | original_repr = repr(m) |
---|
1488 | n/a | m.return_value = m |
---|
1489 | n/a | self.assertIs(m(), m) |
---|
1490 | n/a | self.assertEqual(repr(m), original_repr) |
---|
1491 | n/a | |
---|
1492 | n/a | m.reset_mock() |
---|
1493 | n/a | self.assertIs(m(), m) |
---|
1494 | n/a | self.assertEqual(repr(m), original_repr) |
---|
1495 | n/a | |
---|
1496 | n/a | m = Klass() |
---|
1497 | n/a | m.b = m.a |
---|
1498 | n/a | self.assertIn("name='mock.a'", repr(m.b)) |
---|
1499 | n/a | self.assertIn("name='mock.a'", repr(m.a)) |
---|
1500 | n/a | m.reset_mock() |
---|
1501 | n/a | self.assertIn("name='mock.a'", repr(m.b)) |
---|
1502 | n/a | self.assertIn("name='mock.a'", repr(m.a)) |
---|
1503 | n/a | |
---|
1504 | n/a | m = Klass() |
---|
1505 | n/a | original_repr = repr(m) |
---|
1506 | n/a | m.a = m() |
---|
1507 | n/a | m.a.return_value = m |
---|
1508 | n/a | |
---|
1509 | n/a | self.assertEqual(repr(m), original_repr) |
---|
1510 | n/a | self.assertEqual(repr(m.a()), original_repr) |
---|
1511 | n/a | |
---|
1512 | n/a | |
---|
1513 | n/a | def test_attach_mock(self): |
---|
1514 | n/a | classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock |
---|
1515 | n/a | for Klass in classes: |
---|
1516 | n/a | for Klass2 in classes: |
---|
1517 | n/a | m = Klass() |
---|
1518 | n/a | |
---|
1519 | n/a | m2 = Klass2(name='foo') |
---|
1520 | n/a | m.attach_mock(m2, 'bar') |
---|
1521 | n/a | |
---|
1522 | n/a | self.assertIs(m.bar, m2) |
---|
1523 | n/a | self.assertIn("name='mock.bar'", repr(m2)) |
---|
1524 | n/a | |
---|
1525 | n/a | m.bar.baz(1) |
---|
1526 | n/a | self.assertEqual(m.mock_calls, [call.bar.baz(1)]) |
---|
1527 | n/a | self.assertEqual(m.method_calls, [call.bar.baz(1)]) |
---|
1528 | n/a | |
---|
1529 | n/a | |
---|
1530 | n/a | def test_attach_mock_return_value(self): |
---|
1531 | n/a | classes = Mock, MagicMock, NonCallableMagicMock, NonCallableMock |
---|
1532 | n/a | for Klass in Mock, MagicMock: |
---|
1533 | n/a | for Klass2 in classes: |
---|
1534 | n/a | m = Klass() |
---|
1535 | n/a | |
---|
1536 | n/a | m2 = Klass2(name='foo') |
---|
1537 | n/a | m.attach_mock(m2, 'return_value') |
---|
1538 | n/a | |
---|
1539 | n/a | self.assertIs(m(), m2) |
---|
1540 | n/a | self.assertIn("name='mock()'", repr(m2)) |
---|
1541 | n/a | |
---|
1542 | n/a | m2.foo() |
---|
1543 | n/a | self.assertEqual(m.mock_calls, call().foo().call_list()) |
---|
1544 | n/a | |
---|
1545 | n/a | |
---|
1546 | n/a | def test_attribute_deletion(self): |
---|
1547 | n/a | for mock in (Mock(), MagicMock(), NonCallableMagicMock(), |
---|
1548 | n/a | NonCallableMock()): |
---|
1549 | n/a | self.assertTrue(hasattr(mock, 'm')) |
---|
1550 | n/a | |
---|
1551 | n/a | del mock.m |
---|
1552 | n/a | self.assertFalse(hasattr(mock, 'm')) |
---|
1553 | n/a | |
---|
1554 | n/a | del mock.f |
---|
1555 | n/a | self.assertFalse(hasattr(mock, 'f')) |
---|
1556 | n/a | self.assertRaises(AttributeError, getattr, mock, 'f') |
---|
1557 | n/a | |
---|
1558 | n/a | |
---|
1559 | n/a | def test_class_assignable(self): |
---|
1560 | n/a | for mock in Mock(), MagicMock(): |
---|
1561 | n/a | self.assertNotIsInstance(mock, int) |
---|
1562 | n/a | |
---|
1563 | n/a | mock.__class__ = int |
---|
1564 | n/a | self.assertIsInstance(mock, int) |
---|
1565 | n/a | mock.foo |
---|
1566 | n/a | |
---|
1567 | n/a | |
---|
1568 | n/a | if __name__ == '__main__': |
---|
1569 | n/a | unittest.main() |
---|