blob: 4b6616c5069dfd6eab63bad98a6f2ddede3ded9f [file] [log] [blame]
Michael Foord944e02d2012-03-25 23:12:55 +01001.. _further-examples:
2
3:mod:`unittest.mock` --- further examples
4=========================================
5
6.. module:: unittest.mock
7 :synopsis: Mock object library.
8.. moduleauthor:: Michael Foord <michael@python.org>
9.. currentmodule:: unittest.mock
10
11.. versionadded:: 3.3
12
13
14Here are some more examples for some slightly more advanced scenarios than in
15the :ref:`getting started <getting-started>` guide.
16
17
18Mocking chained calls
19---------------------
20
21Mocking chained calls is actually straightforward with mock once you
22understand the :attr:`~Mock.return_value` attribute. When a mock is called for
23the first time, or you fetch its `return_value` before it has been called, a
24new `Mock` is created.
25
26This means that you can see how the object returned from a call to a mocked
27object has been used by interrogating the `return_value` mock:
28
29 >>> mock = Mock()
30 >>> mock().foo(a=2, b=3)
31 <Mock name='mock().foo()' id='...'>
32 >>> mock.return_value.foo.assert_called_with(a=2, b=3)
33
34From here it is a simple step to configure and then make assertions about
35chained calls. Of course another alternative is writing your code in a more
36testable way in the first place...
37
38So, suppose we have some code that looks a little bit like this:
39
40 >>> class Something(object):
41 ... def __init__(self):
42 ... self.backend = BackendProvider()
43 ... def method(self):
44 ... response = self.backend.get_endpoint('foobar').create_call('spam', 'eggs').start_call()
45 ... # more code
46
47Assuming that `BackendProvider` is already well tested, how do we test
48`method()`? Specifically, we want to test that the code section `# more
49code` uses the response object in the correct way.
50
51As this chain of calls is made from an instance attribute we can monkey patch
52the `backend` attribute on a `Something` instance. In this particular case
53we are only interested in the return value from the final call to
54`start_call` so we don't have much configuration to do. Let's assume the
55object it returns is 'file-like', so we'll ensure that our response object
56uses the builtin `file` as its `spec`.
57
58To do this we create a mock instance as our mock backend and create a mock
59response object for it. To set the response as the return value for that final
60`start_call` we could do this:
61
62 `mock_backend.get_endpoint.return_value.create_call.return_value.start_call.return_value = mock_response`.
63
64We can do that in a slightly nicer way using the :meth:`~Mock.configure_mock`
65method to directly set the return value for us:
66
67 >>> something = Something()
68 >>> mock_response = Mock(spec=file)
69 >>> mock_backend = Mock()
70 >>> config = {'get_endpoint.return_value.create_call.return_value.start_call.return_value': mock_response}
71 >>> mock_backend.configure_mock(**config)
72
73With these we monkey patch the "mock backend" in place and can make the real
74call:
75
76 >>> something.backend = mock_backend
77 >>> something.method()
78
79Using :attr:`~Mock.mock_calls` we can check the chained call with a single
80assert. A chained call is several calls in one line of code, so there will be
81several entries in `mock_calls`. We can use :meth:`call.call_list` to create
82this list of calls for us:
83
84 >>> chained = call.get_endpoint('foobar').create_call('spam', 'eggs').start_call()
85 >>> call_list = chained.call_list()
86 >>> assert mock_backend.mock_calls == call_list
87
88
89Partial mocking
90---------------
91
92In some tests I wanted to mock out a call to `datetime.date.today()
93<http://docs.python.org/library/datetime.html#datetime.date.today>`_ to return
94a known date, but I didn't want to prevent the code under test from
95creating new date objects. Unfortunately `datetime.date` is written in C, and
96so I couldn't just monkey-patch out the static `date.today` method.
97
98I found a simple way of doing this that involved effectively wrapping the date
99class with a mock, but passing through calls to the constructor to the real
100class (and returning real instances).
101
102The :func:`patch decorator <patch>` is used here to
103mock out the `date` class in the module under test. The :attr:`side_effect`
104attribute on the mock date class is then set to a lambda function that returns
105a real date. When the mock date class is called a real date will be
106constructed and returned by `side_effect`.
107
108 >>> from datetime import date
109 >>> with patch('mymodule.date') as mock_date:
110 ... mock_date.today.return_value = date(2010, 10, 8)
111 ... mock_date.side_effect = lambda *args, **kw: date(*args, **kw)
112 ...
113 ... assert mymodule.date.today() == date(2010, 10, 8)
114 ... assert mymodule.date(2009, 6, 8) == date(2009, 6, 8)
115 ...
116
117Note that we don't patch `datetime.date` globally, we patch `date` in the
118module that *uses* it. See :ref:`where to patch <where-to-patch>`.
119
120When `date.today()` is called a known date is returned, but calls to the
121`date(...)` constructor still return normal dates. Without this you can find
122yourself having to calculate an expected result using exactly the same
123algorithm as the code under test, which is a classic testing anti-pattern.
124
125Calls to the date constructor are recorded in the `mock_date` attributes
126(`call_count` and friends) which may also be useful for your tests.
127
128An alternative way of dealing with mocking dates, or other builtin classes,
129is discussed in `this blog entry
130<http://williamjohnbert.com/2011/07/how-to-unit-testing-in-django-with-mocking-and-patching/>`_.
131
132
133Mocking a Generator Method
134--------------------------
135
136A Python generator is a function or method that uses the `yield statement
137<http://docs.python.org/reference/simple_stmts.html#the-yield-statement>`_ to
138return a series of values when iterated over [#]_.
139
140A generator method / function is called to return the generator object. It is
141the generator object that is then iterated over. The protocol method for
142iteration is `__iter__
143<http://docs.python.org/library/stdtypes.html#container.__iter__>`_, so we can
144mock this using a `MagicMock`.
145
146Here's an example class with an "iter" method implemented as a generator:
147
148 >>> class Foo(object):
149 ... def iter(self):
150 ... for i in [1, 2, 3]:
151 ... yield i
152 ...
153 >>> foo = Foo()
154 >>> list(foo.iter())
155 [1, 2, 3]
156
157
158How would we mock this class, and in particular its "iter" method?
159
160To configure the values returned from the iteration (implicit in the call to
161`list`), we need to configure the object returned by the call to `foo.iter()`.
162
163 >>> mock_foo = MagicMock()
164 >>> mock_foo.iter.return_value = iter([1, 2, 3])
165 >>> list(mock_foo.iter())
166 [1, 2, 3]
167
168.. [#] There are also generator expressions and more `advanced uses
169 <http://www.dabeaz.com/coroutines/index.html>`_ of generators, but we aren't
170 concerned about them here. A very good introduction to generators and how
171 powerful they are is: `Generator Tricks for Systems Programmers
172 <http://www.dabeaz.com/generators/>`_.
173
174
175Applying the same patch to every test method
176--------------------------------------------
177
178If you want several patches in place for multiple test methods the obvious way
179is to apply the patch decorators to every method. This can feel like unnecessary
180repetition. For Python 2.6 or more recent you can use `patch` (in all its
181various forms) as a class decorator. This applies the patches to all test
182methods on the class. A test method is identified by methods whose names start
183with `test`:
184
185 >>> @patch('mymodule.SomeClass')
186 ... class MyTest(TestCase):
187 ...
188 ... def test_one(self, MockSomeClass):
189 ... self.assertTrue(mymodule.SomeClass is MockSomeClass)
190 ...
191 ... def test_two(self, MockSomeClass):
192 ... self.assertTrue(mymodule.SomeClass is MockSomeClass)
193 ...
194 ... def not_a_test(self):
195 ... return 'something'
196 ...
197 >>> MyTest('test_one').test_one()
198 >>> MyTest('test_two').test_two()
199 >>> MyTest('test_two').not_a_test()
200 'something'
201
202An alternative way of managing patches is to use the :ref:`start-and-stop`.
203These allow you to move the patching into your `setUp` and `tearDown` methods.
204
205 >>> class MyTest(TestCase):
206 ... def setUp(self):
207 ... self.patcher = patch('mymodule.foo')
208 ... self.mock_foo = self.patcher.start()
209 ...
210 ... def test_foo(self):
211 ... self.assertTrue(mymodule.foo is self.mock_foo)
212 ...
213 ... def tearDown(self):
214 ... self.patcher.stop()
215 ...
216 >>> MyTest('test_foo').run()
217
218If you use this technique you must ensure that the patching is "undone" by
219calling `stop`. This can be fiddlier than you might think, because if an
220exception is raised in the setUp then tearDown is not called.
221:meth:`unittest.TestCase.addCleanup` makes this easier:
222
223 >>> class MyTest(TestCase):
224 ... def setUp(self):
225 ... patcher = patch('mymodule.foo')
226 ... self.addCleanup(patcher.stop)
227 ... self.mock_foo = patcher.start()
228 ...
229 ... def test_foo(self):
230 ... self.assertTrue(mymodule.foo is self.mock_foo)
231 ...
232 >>> MyTest('test_foo').run()
233
234
235Mocking Unbound Methods
236-----------------------
237
238Whilst writing tests today I needed to patch an *unbound method* (patching the
239method on the class rather than on the instance). I needed self to be passed
240in as the first argument because I want to make asserts about which objects
241were calling this particular method. The issue is that you can't patch with a
242mock for this, because if you replace an unbound method with a mock it doesn't
243become a bound method when fetched from the instance, and so it doesn't get
244self passed in. The workaround is to patch the unbound method with a real
245function instead. The :func:`patch` decorator makes it so simple to
246patch out methods with a mock that having to create a real function becomes a
247nuisance.
248
249If you pass `autospec=True` to patch then it does the patching with a
250*real* function object. This function object has the same signature as the one
251it is replacing, but delegates to a mock under the hood. You still get your
252mock auto-created in exactly the same way as before. What it means though, is
253that if you use it to patch out an unbound method on a class the mocked
254function will be turned into a bound method if it is fetched from an instance.
255It will have `self` passed in as the first argument, which is exactly what I
256wanted:
257
258 >>> class Foo(object):
259 ... def foo(self):
260 ... pass
261 ...
262 >>> with patch.object(Foo, 'foo', autospec=True) as mock_foo:
263 ... mock_foo.return_value = 'foo'
264 ... foo = Foo()
265 ... foo.foo()
266 ...
267 'foo'
268 >>> mock_foo.assert_called_once_with(foo)
269
270If we don't use `autospec=True` then the unbound method is patched out
271with a Mock instance instead, and isn't called with `self`.
272
273
274Checking multiple calls with mock
275---------------------------------
276
277mock has a nice API for making assertions about how your mock objects are used.
278
279 >>> mock = Mock()
280 >>> mock.foo_bar.return_value = None
281 >>> mock.foo_bar('baz', spam='eggs')
282 >>> mock.foo_bar.assert_called_with('baz', spam='eggs')
283
284If your mock is only being called once you can use the
285:meth:`assert_called_once_with` method that also asserts that the
286:attr:`call_count` is one.
287
288 >>> mock.foo_bar.assert_called_once_with('baz', spam='eggs')
289 >>> mock.foo_bar()
290 >>> mock.foo_bar.assert_called_once_with('baz', spam='eggs')
291 Traceback (most recent call last):
292 ...
293 AssertionError: Expected to be called once. Called 2 times.
294
295Both `assert_called_with` and `assert_called_once_with` make assertions about
296the *most recent* call. If your mock is going to be called several times, and
297you want to make assertions about *all* those calls you can use
298:attr:`~Mock.call_args_list`:
299
300 >>> mock = Mock(return_value=None)
301 >>> mock(1, 2, 3)
302 >>> mock(4, 5, 6)
303 >>> mock()
304 >>> mock.call_args_list
305 [call(1, 2, 3), call(4, 5, 6), call()]
306
307The :data:`call` helper makes it easy to make assertions about these calls. You
308can build up a list of expected calls and compare it to `call_args_list`. This
309looks remarkably similar to the repr of the `call_args_list`:
310
311 >>> expected = [call(1, 2, 3), call(4, 5, 6), call()]
312 >>> mock.call_args_list == expected
313 True
314
315
316Coping with mutable arguments
317-----------------------------
318
319Another situation is rare, but can bite you, is when your mock is called with
320mutable arguments. `call_args` and `call_args_list` store *references* to the
321arguments. If the arguments are mutated by the code under test then you can no
322longer make assertions about what the values were when the mock was called.
323
324Here's some example code that shows the problem. Imagine the following functions
325defined in 'mymodule'::
326
327 def frob(val):
328 pass
329
330 def grob(val):
331 "First frob and then clear val"
332 frob(val)
333 val.clear()
334
335When we try to test that `grob` calls `frob` with the correct argument look
336what happens:
337
338 >>> with patch('mymodule.frob') as mock_frob:
339 ... val = set([6])
340 ... mymodule.grob(val)
341 ...
342 >>> val
343 set([])
344 >>> mock_frob.assert_called_with(set([6]))
345 Traceback (most recent call last):
346 ...
347 AssertionError: Expected: ((set([6]),), {})
348 Called with: ((set([]),), {})
349
350One possibility would be for mock to copy the arguments you pass in. This
351could then cause problems if you do assertions that rely on object identity
352for equality.
353
354Here's one solution that uses the :attr:`side_effect`
355functionality. If you provide a `side_effect` function for a mock then
356`side_effect` will be called with the same args as the mock. This gives us an
357opportunity to copy the arguments and store them for later assertions. In this
358example I'm using *another* mock to store the arguments so that I can use the
359mock methods for doing the assertion. Again a helper function sets this up for
360me.
361
362 >>> from copy import deepcopy
363 >>> from unittest.mock import Mock, patch, DEFAULT
364 >>> def copy_call_args(mock):
365 ... new_mock = Mock()
366 ... def side_effect(*args, **kwargs):
367 ... args = deepcopy(args)
368 ... kwargs = deepcopy(kwargs)
369 ... new_mock(*args, **kwargs)
370 ... return DEFAULT
371 ... mock.side_effect = side_effect
372 ... return new_mock
373 ...
374 >>> with patch('mymodule.frob') as mock_frob:
375 ... new_mock = copy_call_args(mock_frob)
376 ... val = set([6])
377 ... mymodule.grob(val)
378 ...
379 >>> new_mock.assert_called_with(set([6]))
380 >>> new_mock.call_args
381 call(set([6]))
382
383`copy_call_args` is called with the mock that will be called. It returns a new
384mock that we do the assertion on. The `side_effect` function makes a copy of
385the args and calls our `new_mock` with the copy.
386
387.. note::
388
389 If your mock is only going to be used once there is an easier way of
390 checking arguments at the point they are called. You can simply do the
391 checking inside a `side_effect` function.
392
393 >>> def side_effect(arg):
394 ... assert arg == set([6])
395 ...
396 >>> mock = Mock(side_effect=side_effect)
397 >>> mock(set([6]))
398 >>> mock(set())
399 Traceback (most recent call last):
400 ...
401 AssertionError
402
403An alternative approach is to create a subclass of `Mock` or `MagicMock` that
404copies (using :func:`copy.deepcopy`) the arguments.
405Here's an example implementation:
406
407 >>> from copy import deepcopy
408 >>> class CopyingMock(MagicMock):
409 ... def __call__(self, *args, **kwargs):
410 ... args = deepcopy(args)
411 ... kwargs = deepcopy(kwargs)
412 ... return super(CopyingMock, self).__call__(*args, **kwargs)
413 ...
414 >>> c = CopyingMock(return_value=None)
415 >>> arg = set()
416 >>> c(arg)
417 >>> arg.add(1)
418 >>> c.assert_called_with(set())
419 >>> c.assert_called_with(arg)
420 Traceback (most recent call last):
421 ...
422 AssertionError: Expected call: mock(set([1]))
423 Actual call: mock(set([]))
424 >>> c.foo
425 <CopyingMock name='mock.foo' id='...'>
426
427When you subclass `Mock` or `MagicMock` all dynamically created attributes,
428and the `return_value` will use your subclass automatically. That means all
429children of a `CopyingMock` will also have the type `CopyingMock`.
430
431
432Multiple calls with different effects
433-------------------------------------
434
435Handling code that needs to behave differently on subsequent calls during the
436test can be tricky. For example you may have a function that needs to raise
437an exception the first time it is called but returns a response on the second
438call (testing retry behaviour).
439
440One approach is to use a :attr:`side_effect` function that replaces itself. The
441first time it is called the `side_effect` sets a new `side_effect` that will
442be used for the second call. It then raises an exception:
443
444 >>> def side_effect(*args):
445 ... def second_call(*args):
446 ... return 'response'
447 ... mock.side_effect = second_call
448 ... raise Exception('boom')
449 ...
450 >>> mock = Mock(side_effect=side_effect)
451 >>> mock('first')
452 Traceback (most recent call last):
453 ...
454 Exception: boom
455 >>> mock('second')
456 'response'
457 >>> mock.assert_called_with('second')
458
459Another perfectly valid way would be to pop return values from a list. If the
460return value is an exception, raise it instead of returning it:
461
462 >>> returns = [Exception('boom'), 'response']
463 >>> def side_effect(*args):
464 ... result = returns.pop(0)
465 ... if isinstance(result, Exception):
466 ... raise result
467 ... return result
468 ...
469 >>> mock = Mock(side_effect=side_effect)
470 >>> mock('first')
471 Traceback (most recent call last):
472 ...
473 Exception: boom
474 >>> mock('second')
475 'response'
476 >>> mock.assert_called_with('second')
477
478Which approach you prefer is a matter of taste. The first approach is actually
479a line shorter but maybe the second approach is more readable.
480
481
482Nesting Patches
483---------------
484
485Using patch as a context manager is nice, but if you do multiple patches you
486can end up with nested with statements indenting further and further to the
487right:
488
489 >>> class MyTest(TestCase):
490 ...
491 ... def test_foo(self):
492 ... with patch('mymodule.Foo') as mock_foo:
493 ... with patch('mymodule.Bar') as mock_bar:
494 ... with patch('mymodule.Spam') as mock_spam:
495 ... assert mymodule.Foo is mock_foo
496 ... assert mymodule.Bar is mock_bar
497 ... assert mymodule.Spam is mock_spam
498 ...
499 >>> original = mymodule.Foo
500 >>> MyTest('test_foo').test_foo()
501 >>> assert mymodule.Foo is original
502
503With unittest `cleanup` functions and the :ref:`start-and-stop` we can
504achieve the same effect without the nested indentation. A simple helper
505method, `create_patch`, puts the patch in place and returns the created mock
506for us:
507
508 >>> class MyTest(TestCase):
509 ...
510 ... def create_patch(self, name):
511 ... patcher = patch(name)
512 ... thing = patcher.start()
513 ... self.addCleanup(patcher.stop)
514 ... return thing
515 ...
516 ... def test_foo(self):
517 ... mock_foo = self.create_patch('mymodule.Foo')
518 ... mock_bar = self.create_patch('mymodule.Bar')
519 ... mock_spam = self.create_patch('mymodule.Spam')
520 ...
521 ... assert mymodule.Foo is mock_foo
522 ... assert mymodule.Bar is mock_bar
523 ... assert mymodule.Spam is mock_spam
524 ...
525 >>> original = mymodule.Foo
526 >>> MyTest('test_foo').run()
527 >>> assert mymodule.Foo is original
528
529
530Mocking a dictionary with MagicMock
531-----------------------------------
532
533You may want to mock a dictionary, or other container object, recording all
534access to it whilst having it still behave like a dictionary.
535
536We can do this with :class:`MagicMock`, which will behave like a dictionary,
537and using :data:`~Mock.side_effect` to delegate dictionary access to a real
538underlying dictionary that is under our control.
539
540When the `__getitem__` and `__setitem__` methods of our `MagicMock` are called
541(normal dictionary access) then `side_effect` is called with the key (and in
542the case of `__setitem__` the value too). We can also control what is returned.
543
544After the `MagicMock` has been used we can use attributes like
545:data:`~Mock.call_args_list` to assert about how the dictionary was used:
546
547 >>> my_dict = {'a': 1, 'b': 2, 'c': 3}
548 >>> def getitem(name):
549 ... return my_dict[name]
550 ...
551 >>> def setitem(name, val):
552 ... my_dict[name] = val
553 ...
554 >>> mock = MagicMock()
555 >>> mock.__getitem__.side_effect = getitem
556 >>> mock.__setitem__.side_effect = setitem
557
558.. note::
559
560 An alternative to using `MagicMock` is to use `Mock` and *only* provide
561 the magic methods you specifically want:
562
563 >>> mock = Mock()
564 >>> mock.__setitem__ = Mock(side_effect=getitem)
565 >>> mock.__getitem__ = Mock(side_effect=setitem)
566
567 A *third* option is to use `MagicMock` but passing in `dict` as the `spec`
568 (or `spec_set`) argument so that the `MagicMock` created only has
569 dictionary magic methods available:
570
571 >>> mock = MagicMock(spec_set=dict)
572 >>> mock.__getitem__.side_effect = getitem
573 >>> mock.__setitem__.side_effect = setitem
574
575With these side effect functions in place, the `mock` will behave like a normal
576dictionary but recording the access. It even raises a `KeyError` if you try
577to access a key that doesn't exist.
578
579 >>> mock['a']
580 1
581 >>> mock['c']
582 3
583 >>> mock['d']
584 Traceback (most recent call last):
585 ...
586 KeyError: 'd'
587 >>> mock['b'] = 'fish'
588 >>> mock['d'] = 'eggs'
589 >>> mock['b']
590 'fish'
591 >>> mock['d']
592 'eggs'
593
594After it has been used you can make assertions about the access using the normal
595mock methods and attributes:
596
597 >>> mock.__getitem__.call_args_list
598 [call('a'), call('c'), call('d'), call('b'), call('d')]
599 >>> mock.__setitem__.call_args_list
600 [call('b', 'fish'), call('d', 'eggs')]
601 >>> my_dict
602 {'a': 1, 'c': 3, 'b': 'fish', 'd': 'eggs'}
603
604
605Mock subclasses and their attributes
606------------------------------------
607
608There are various reasons why you might want to subclass `Mock`. One reason
609might be to add helper methods. Here's a silly example:
610
611 >>> class MyMock(MagicMock):
612 ... def has_been_called(self):
613 ... return self.called
614 ...
615 >>> mymock = MyMock(return_value=None)
616 >>> mymock
617 <MyMock id='...'>
618 >>> mymock.has_been_called()
619 False
620 >>> mymock()
621 >>> mymock.has_been_called()
622 True
623
624The standard behaviour for `Mock` instances is that attributes and the return
625value mocks are of the same type as the mock they are accessed on. This ensures
626that `Mock` attributes are `Mocks` and `MagicMock` attributes are `MagicMocks`
627[#]_. So if you're subclassing to add helper methods then they'll also be
628available on the attributes and return value mock of instances of your
629subclass.
630
631 >>> mymock.foo
632 <MyMock name='mock.foo' id='...'>
633 >>> mymock.foo.has_been_called()
634 False
635 >>> mymock.foo()
636 <MyMock name='mock.foo()' id='...'>
637 >>> mymock.foo.has_been_called()
638 True
639
640Sometimes this is inconvenient. For example, `one user
641<https://code.google.com/p/mock/issues/detail?id=105>`_ is subclassing mock to
642created a `Twisted adaptor
643<http://twistedmatrix.com/documents/11.0.0/api/twisted.python.components.html>`_.
644Having this applied to attributes too actually causes errors.
645
646`Mock` (in all its flavours) uses a method called `_get_child_mock` to create
647these "sub-mocks" for attributes and return values. You can prevent your
648subclass being used for attributes by overriding this method. The signature is
649that it takes arbitrary keyword arguments (`**kwargs`) which are then passed
650onto the mock constructor:
651
652 >>> class Subclass(MagicMock):
653 ... def _get_child_mock(self, **kwargs):
654 ... return MagicMock(**kwargs)
655 ...
656 >>> mymock = Subclass()
657 >>> mymock.foo
658 <MagicMock name='mock.foo' id='...'>
659 >>> assert isinstance(mymock, Subclass)
660 >>> assert not isinstance(mymock.foo, Subclass)
661 >>> assert not isinstance(mymock(), Subclass)
662
663.. [#] An exception to this rule are the non-callable mocks. Attributes use the
664 callable variant because otherwise non-callable mocks couldn't have callable
665 methods.
666
667
668Mocking imports with patch.dict
669-------------------------------
670
671One situation where mocking can be hard is where you have a local import inside
672a function. These are harder to mock because they aren't using an object from
673the module namespace that we can patch out.
674
675Generally local imports are to be avoided. They are sometimes done to prevent
676circular dependencies, for which there is *usually* a much better way to solve
677the problem (refactor the code) or to prevent "up front costs" by delaying the
678import. This can also be solved in better ways than an unconditional local
679import (store the module as a class or module attribute and only do the import
680on first use).
681
682That aside there is a way to use `mock` to affect the results of an import.
683Importing fetches an *object* from the `sys.modules` dictionary. Note that it
684fetches an *object*, which need not be a module. Importing a module for the
685first time results in a module object being put in `sys.modules`, so usually
686when you import something you get a module back. This need not be the case
687however.
688
689This means you can use :func:`patch.dict` to *temporarily* put a mock in place
690in `sys.modules`. Any imports whilst this patch is active will fetch the mock.
691When the patch is complete (the decorated function exits, the with statement
692body is complete or `patcher.stop()` is called) then whatever was there
693previously will be restored safely.
694
695Here's an example that mocks out the 'fooble' module.
696
697 >>> mock = Mock()
698 >>> with patch.dict('sys.modules', {'fooble': mock}):
699 ... import fooble
700 ... fooble.blob()
701 ...
702 <Mock name='mock.blob()' id='...'>
703 >>> assert 'fooble' not in sys.modules
704 >>> mock.blob.assert_called_once_with()
705
706As you can see the `import fooble` succeeds, but on exit there is no 'fooble'
707left in `sys.modules`.
708
709This also works for the `from module import name` form:
710
711 >>> mock = Mock()
712 >>> with patch.dict('sys.modules', {'fooble': mock}):
713 ... from fooble import blob
714 ... blob.blip()
715 ...
716 <Mock name='mock.blob.blip()' id='...'>
717 >>> mock.blob.blip.assert_called_once_with()
718
719With slightly more work you can also mock package imports:
720
721 >>> mock = Mock()
722 >>> modules = {'package': mock, 'package.module': mock.module}
723 >>> with patch.dict('sys.modules', modules):
724 ... from package.module import fooble
725 ... fooble()
726 ...
727 <Mock name='mock.module.fooble()' id='...'>
728 >>> mock.module.fooble.assert_called_once_with()
729
730
731Tracking order of calls and less verbose call assertions
732--------------------------------------------------------
733
734The :class:`Mock` class allows you to track the *order* of method calls on
735your mock objects through the :attr:`~Mock.method_calls` attribute. This
736doesn't allow you to track the order of calls between separate mock objects,
737however we can use :attr:`~Mock.mock_calls` to achieve the same effect.
738
739Because mocks track calls to child mocks in `mock_calls`, and accessing an
740arbitrary attribute of a mock creates a child mock, we can create our separate
741mocks from a parent one. Calls to those child mock will then all be recorded,
742in order, in the `mock_calls` of the parent:
743
744 >>> manager = Mock()
745 >>> mock_foo = manager.foo
746 >>> mock_bar = manager.bar
747
748 >>> mock_foo.something()
749 <Mock name='mock.foo.something()' id='...'>
750 >>> mock_bar.other.thing()
751 <Mock name='mock.bar.other.thing()' id='...'>
752
753 >>> manager.mock_calls
754 [call.foo.something(), call.bar.other.thing()]
755
756We can then assert about the calls, including the order, by comparing with
757the `mock_calls` attribute on the manager mock:
758
759 >>> expected_calls = [call.foo.something(), call.bar.other.thing()]
760 >>> manager.mock_calls == expected_calls
761 True
762
763If `patch` is creating, and putting in place, your mocks then you can attach
764them to a manager mock using the :meth:`~Mock.attach_mock` method. After
765attaching calls will be recorded in `mock_calls` of the manager.
766
767 >>> manager = MagicMock()
768 >>> with patch('mymodule.Class1') as MockClass1:
769 ... with patch('mymodule.Class2') as MockClass2:
770 ... manager.attach_mock(MockClass1, 'MockClass1')
771 ... manager.attach_mock(MockClass2, 'MockClass2')
772 ... MockClass1().foo()
773 ... MockClass2().bar()
774 ...
775 <MagicMock name='mock.MockClass1().foo()' id='...'>
776 <MagicMock name='mock.MockClass2().bar()' id='...'>
777 >>> manager.mock_calls
778 [call.MockClass1(),
779 call.MockClass1().foo(),
780 call.MockClass2(),
781 call.MockClass2().bar()]
782
783If many calls have been made, but you're only interested in a particular
784sequence of them then an alternative is to use the
785:meth:`~Mock.assert_has_calls` method. This takes a list of calls (constructed
786with the :data:`call` object). If that sequence of calls are in
787:attr:`~Mock.mock_calls` then the assert succeeds.
788
789 >>> m = MagicMock()
790 >>> m().foo().bar().baz()
791 <MagicMock name='mock().foo().bar().baz()' id='...'>
792 >>> m.one().two().three()
793 <MagicMock name='mock.one().two().three()' id='...'>
794 >>> calls = call.one().two().three().call_list()
795 >>> m.assert_has_calls(calls)
796
797Even though the chained call `m.one().two().three()` aren't the only calls that
798have been made to the mock, the assert still succeeds.
799
800Sometimes a mock may have several calls made to it, and you are only interested
801in asserting about *some* of those calls. You may not even care about the
802order. In this case you can pass `any_order=True` to `assert_has_calls`:
803
804 >>> m = MagicMock()
805 >>> m(1), m.two(2, 3), m.seven(7), m.fifty('50')
806 (...)
807 >>> calls = [call.fifty('50'), call(1), call.seven(7)]
808 >>> m.assert_has_calls(calls, any_order=True)
809
810
811More complex argument matching
812------------------------------
813
814Using the same basic concept as :data:`ANY` we can implement matchers to do more
815complex assertions on objects used as arguments to mocks.
816
817Suppose we expect some object to be passed to a mock that by default
818compares equal based on object identity (which is the Python default for user
819defined classes). To use :meth:`~Mock.assert_called_with` we would need to pass
820in the exact same object. If we are only interested in some of the attributes
821of this object then we can create a matcher that will check these attributes
822for us.
823
824You can see in this example how a 'standard' call to `assert_called_with` isn't
825sufficient:
826
827 >>> class Foo(object):
828 ... def __init__(self, a, b):
829 ... self.a, self.b = a, b
830 ...
831 >>> mock = Mock(return_value=None)
832 >>> mock(Foo(1, 2))
833 >>> mock.assert_called_with(Foo(1, 2))
834 Traceback (most recent call last):
835 ...
836 AssertionError: Expected: call(<__main__.Foo object at 0x...>)
837 Actual call: call(<__main__.Foo object at 0x...>)
838
839A comparison function for our `Foo` class might look something like this:
840
841 >>> def compare(self, other):
842 ... if not type(self) == type(other):
843 ... return False
844 ... if self.a != other.a:
845 ... return False
846 ... if self.b != other.b:
847 ... return False
848 ... return True
849 ...
850
851And a matcher object that can use comparison functions like this for its
852equality operation would look something like this:
853
854 >>> class Matcher(object):
855 ... def __init__(self, compare, some_obj):
856 ... self.compare = compare
857 ... self.some_obj = some_obj
858 ... def __eq__(self, other):
859 ... return self.compare(self.some_obj, other)
860 ...
861
862Putting all this together:
863
864 >>> match_foo = Matcher(compare, Foo(1, 2))
865 >>> mock.assert_called_with(match_foo)
866
867The `Matcher` is instantiated with our compare function and the `Foo` object
868we want to compare against. In `assert_called_with` the `Matcher` equality
869method will be called, which compares the object the mock was called with
870against the one we created our matcher with. If they match then
871`assert_called_with` passes, and if they don't an `AssertionError` is raised:
872
873 >>> match_wrong = Matcher(compare, Foo(3, 4))
874 >>> mock.assert_called_with(match_wrong)
875 Traceback (most recent call last):
876 ...
877 AssertionError: Expected: ((<Matcher object at 0x...>,), {})
878 Called with: ((<Foo object at 0x...>,), {})
879
880With a bit of tweaking you could have the comparison function raise the
881`AssertionError` directly and provide a more useful failure message.
882
883As of version 1.5, the Python testing library `PyHamcrest
884<http://pypi.python.org/pypi/PyHamcrest>`_ provides similar functionality,
885that may be useful here, in the form of its equality matcher
886(`hamcrest.library.integration.match_equality
887<http://packages.python.org/PyHamcrest/integration.html#hamcrest.library.integration.match_equality>`_).