examples.txt 35 KB

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