patch.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. ==================
  2. Patch Decorators
  3. ==================
  4. .. currentmodule:: mock
  5. .. testsetup::
  6. class SomeClass(object):
  7. static_method = None
  8. class_method = None
  9. attribute = None
  10. sys.modules['package'] = package = Mock(name='package')
  11. sys.modules['package.module'] = package.module
  12. class TestCase(unittest2.TestCase):
  13. def run(self):
  14. result = unittest2.TestResult()
  15. super(unittest2.TestCase, self).run(result)
  16. assert result.wasSuccessful()
  17. .. testcleanup::
  18. patch.TEST_PREFIX = 'test'
  19. The patch decorators are used for patching objects only within the scope of
  20. the function they decorate. They automatically handle the unpatching for you,
  21. even if exceptions are raised. All of these functions can also be used in with
  22. statements or as class decorators.
  23. patch
  24. =====
  25. .. note::
  26. `patch` is straightforward to use. The key is to do the patching in the
  27. right namespace. See the section `where to patch`_.
  28. .. function:: patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)
  29. `patch` acts as a function decorator, class decorator or a context
  30. manager. Inside the body of the function or with statement, the `target`
  31. is patched with a `new` object. When the function/with statement exits
  32. the patch is undone.
  33. If `new` is omitted, then the target is replaced with a
  34. :class:`MagicMock`. If `patch` is used as a decorator and `new` is
  35. omitted, the created mock is passed in as an extra argument to the
  36. decorated function. If `patch` is used as a context manager the created
  37. mock is returned by the context manager.
  38. `target` should be a string in the form `'package.module.ClassName'`. The
  39. `target` is imported and the specified object replaced with the `new`
  40. object, so the `target` must be importable from the environment you are
  41. calling `patch` from. The target is imported when the decorated function
  42. is executed, not at decoration time.
  43. The `spec` and `spec_set` keyword arguments are passed to the `MagicMock`
  44. if patch is creating one for you.
  45. In addition you can pass `spec=True` or `spec_set=True`, which causes
  46. patch to pass in the object being mocked as the spec/spec_set object.
  47. `new_callable` allows you to specify a different class, or callable object,
  48. that will be called to create the `new` object. By default `MagicMock` is
  49. used.
  50. A more powerful form of `spec` is `autospec`. If you set `autospec=True`
  51. then the mock with be created with a spec from the object being replaced.
  52. All attributes of the mock will also have the spec of the corresponding
  53. attribute of the object being replaced. Methods and functions being mocked
  54. will have their arguments checked and will raise a `TypeError` if they are
  55. called with the wrong signature. For mocks
  56. replacing a class, their return value (the 'instance') will have the same
  57. spec as the class. See the :func:`create_autospec` function and
  58. :ref:`auto-speccing`.
  59. Instead of `autospec=True` you can pass `autospec=some_object` to use an
  60. arbitrary object as the spec instead of the one being replaced.
  61. By default `patch` will fail to replace attributes that don't exist. If
  62. you pass in `create=True`, and the attribute doesn't exist, patch will
  63. create the attribute for you when the patched function is called, and
  64. delete it again afterwards. This is useful for writing tests against
  65. attributes that your production code creates at runtime. It is off by by
  66. default because it can be dangerous. With it switched on you can write
  67. passing tests against APIs that don't actually exist!
  68. Patch can be used as a `TestCase` class decorator. It works by
  69. decorating each test method in the class. This reduces the boilerplate
  70. code when your test methods share a common patchings set. `patch` finds
  71. tests by looking for method names that start with `patch.TEST_PREFIX`.
  72. By default this is `test`, which matches the way `unittest` finds tests.
  73. You can specify an alternative prefix by setting `patch.TEST_PREFIX`.
  74. Patch can be used as a context manager, with the with statement. Here the
  75. patching applies to the indented block after the with statement. If you
  76. use "as" then the patched object will be bound to the name after the
  77. "as"; very useful if `patch` is creating a mock object for you.
  78. `patch` takes arbitrary keyword arguments. These will be passed to
  79. the `Mock` (or `new_callable`) on construction.
  80. `patch.dict(...)`, `patch.multiple(...)` and `patch.object(...)` are
  81. available for alternate use-cases.
  82. `patch` as function decorator, creating the mock for you and passing it into
  83. the decorated function:
  84. .. doctest::
  85. >>> @patch('__main__.SomeClass')
  86. ... def function(normal_argument, mock_class):
  87. ... print mock_class is SomeClass
  88. ...
  89. >>> function(None)
  90. True
  91. Patching a class replaces the class with a `MagicMock` *instance*. If the
  92. class is instantiated in the code under test then it will be the
  93. :attr:`~Mock.return_value` of the mock that will be used.
  94. If the class is instantiated multiple times you could use
  95. :attr:`~Mock.side_effect` to return a new mock each time. Alternatively you
  96. can set the `return_value` to be anything you want.
  97. To configure return values on methods of *instances* on the patched class
  98. you must do this on the `return_value`. For example:
  99. .. doctest::
  100. >>> class Class(object):
  101. ... def method(self):
  102. ... pass
  103. ...
  104. >>> with patch('__main__.Class') as MockClass:
  105. ... instance = MockClass.return_value
  106. ... instance.method.return_value = 'foo'
  107. ... assert Class() is instance
  108. ... assert Class().method() == 'foo'
  109. ...
  110. If you use `spec` or `spec_set` and `patch` is replacing a *class*, then the
  111. return value of the created mock will have the same spec.
  112. .. doctest::
  113. >>> Original = Class
  114. >>> patcher = patch('__main__.Class', spec=True)
  115. >>> MockClass = patcher.start()
  116. >>> instance = MockClass()
  117. >>> assert isinstance(instance, Original)
  118. >>> patcher.stop()
  119. The `new_callable` argument is useful where you want to use an alternative
  120. class to the default :class:`MagicMock` for the created mock. For example, if
  121. you wanted a :class:`NonCallableMock` to be used:
  122. .. doctest::
  123. >>> thing = object()
  124. >>> with patch('__main__.thing', new_callable=NonCallableMock) as mock_thing:
  125. ... assert thing is mock_thing
  126. ... thing()
  127. ...
  128. Traceback (most recent call last):
  129. ...
  130. TypeError: 'NonCallableMock' object is not callable
  131. Another use case might be to replace an object with a `StringIO` instance:
  132. .. doctest::
  133. >>> from StringIO import StringIO
  134. >>> def foo():
  135. ... print 'Something'
  136. ...
  137. >>> @patch('sys.stdout', new_callable=StringIO)
  138. ... def test(mock_stdout):
  139. ... foo()
  140. ... assert mock_stdout.getvalue() == 'Something\n'
  141. ...
  142. >>> test()
  143. When `patch` is creating a mock for you, it is common that the first thing
  144. you need to do is to configure the mock. Some of that configuration can be done
  145. in the call to patch. Any arbitrary keywords you pass into the call will be
  146. used to set attributes on the created mock:
  147. .. doctest::
  148. >>> patcher = patch('__main__.thing', first='one', second='two')
  149. >>> mock_thing = patcher.start()
  150. >>> mock_thing.first
  151. 'one'
  152. >>> mock_thing.second
  153. 'two'
  154. As well as attributes on the created mock attributes, like the
  155. :attr:`~Mock.return_value` and :attr:`~Mock.side_effect`, of child mocks can
  156. also be configured. These aren't syntactically valid to pass in directly as
  157. keyword arguments, but a dictionary with these as keys can still be expanded
  158. into a `patch` call using `**`:
  159. .. doctest::
  160. >>> config = {'method.return_value': 3, 'other.side_effect': KeyError}
  161. >>> patcher = patch('__main__.thing', **config)
  162. >>> mock_thing = patcher.start()
  163. >>> mock_thing.method()
  164. 3
  165. >>> mock_thing.other()
  166. Traceback (most recent call last):
  167. ...
  168. KeyError
  169. patch.object
  170. ============
  171. .. function:: patch.object(target, attribute, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)
  172. patch the named member (`attribute`) on an object (`target`) with a mock
  173. object.
  174. `patch.object` can be used as a decorator, class decorator or a context
  175. manager. Arguments `new`, `spec`, `create`, `spec_set`, `autospec` and
  176. `new_callable` have the same meaning as for `patch`. Like `patch`,
  177. `patch.object` takes arbitrary keyword arguments for configuring the mock
  178. object it creates.
  179. When used as a class decorator `patch.object` honours `patch.TEST_PREFIX`
  180. for choosing which methods to wrap.
  181. You can either call `patch.object` with three arguments or two arguments. The
  182. three argument form takes the object to be patched, the attribute name and the
  183. object to replace the attribute with.
  184. When calling with the two argument form you omit the replacement object, and a
  185. mock is created for you and passed in as an extra argument to the decorated
  186. function:
  187. .. doctest::
  188. >>> @patch.object(SomeClass, 'class_method')
  189. ... def test(mock_method):
  190. ... SomeClass.class_method(3)
  191. ... mock_method.assert_called_with(3)
  192. ...
  193. >>> test()
  194. `spec`, `create` and the other arguments to `patch.object` have the same
  195. meaning as they do for `patch`.
  196. patch.dict
  197. ==========
  198. .. function:: patch.dict(in_dict, values=(), clear=False, **kwargs)
  199. Patch a dictionary, or dictionary like object, and restore the dictionary
  200. to its original state after the test.
  201. `in_dict` can be a dictionary or a mapping like container. If it is a
  202. mapping then it must at least support getting, setting and deleting items
  203. plus iterating over keys.
  204. `in_dict` can also be a string specifying the name of the dictionary, which
  205. will then be fetched by importing it.
  206. `values` can be a dictionary of values to set in the dictionary. `values`
  207. can also be an iterable of `(key, value)` pairs.
  208. If `clear` is True then the dictionary will be cleared before the new
  209. values are set.
  210. `patch.dict` can also be called with arbitrary keyword arguments to set
  211. values in the dictionary.
  212. `patch.dict` can be used as a context manager, decorator or class
  213. decorator. When used as a class decorator `patch.dict` honours
  214. `patch.TEST_PREFIX` for choosing which methods to wrap.
  215. `patch.dict` can be used to add members to a dictionary, or simply let a test
  216. change a dictionary, and ensure the dictionary is restored when the test
  217. ends.
  218. .. doctest::
  219. >>> from mock import patch
  220. >>> foo = {}
  221. >>> with patch.dict(foo, {'newkey': 'newvalue'}):
  222. ... assert foo == {'newkey': 'newvalue'}
  223. ...
  224. >>> assert foo == {}
  225. >>> import os
  226. >>> with patch.dict('os.environ', {'newkey': 'newvalue'}):
  227. ... print os.environ['newkey']
  228. ...
  229. newvalue
  230. >>> assert 'newkey' not in os.environ
  231. Keywords can be used in the `patch.dict` call to set values in the dictionary:
  232. .. doctest::
  233. >>> mymodule = MagicMock()
  234. >>> mymodule.function.return_value = 'fish'
  235. >>> with patch.dict('sys.modules', mymodule=mymodule):
  236. ... import mymodule
  237. ... mymodule.function('some', 'args')
  238. ...
  239. 'fish'
  240. `patch.dict` can be used with dictionary like objects that aren't actually
  241. dictionaries. At the very minimum they must support item getting, setting,
  242. deleting and either iteration or membership test. This corresponds to the
  243. magic methods `__getitem__`, `__setitem__`, `__delitem__` and either
  244. `__iter__` or `__contains__`.
  245. .. doctest::
  246. >>> class Container(object):
  247. ... def __init__(self):
  248. ... self.values = {}
  249. ... def __getitem__(self, name):
  250. ... return self.values[name]
  251. ... def __setitem__(self, name, value):
  252. ... self.values[name] = value
  253. ... def __delitem__(self, name):
  254. ... del self.values[name]
  255. ... def __iter__(self):
  256. ... return iter(self.values)
  257. ...
  258. >>> thing = Container()
  259. >>> thing['one'] = 1
  260. >>> with patch.dict(thing, one=2, two=3):
  261. ... assert thing['one'] == 2
  262. ... assert thing['two'] == 3
  263. ...
  264. >>> assert thing['one'] == 1
  265. >>> assert list(thing) == ['one']
  266. patch.multiple
  267. ==============
  268. .. function:: patch.multiple(target, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)
  269. Perform multiple patches in a single call. It takes the object to be
  270. patched (either as an object or a string to fetch the object by importing)
  271. and keyword arguments for the patches::
  272. with patch.multiple(settings, FIRST_PATCH='one', SECOND_PATCH='two'):
  273. ...
  274. Use :data:`DEFAULT` as the value if you want `patch.multiple` to create
  275. mocks for you. In this case the created mocks are passed into a decorated
  276. function by keyword, and a dictionary is returned when `patch.multiple` is
  277. used as a context manager.
  278. `patch.multiple` can be used as a decorator, class decorator or a context
  279. manager. The arguments `spec`, `spec_set`, `create`, `autospec` and
  280. `new_callable` have the same meaning as for `patch`. These arguments will
  281. be applied to *all* patches done by `patch.multiple`.
  282. When used as a class decorator `patch.multiple` honours `patch.TEST_PREFIX`
  283. for choosing which methods to wrap.
  284. If you want `patch.multiple` to create mocks for you, then you can use
  285. :data:`DEFAULT` as the value. If you use `patch.multiple` as a decorator
  286. then the created mocks are passed into the decorated function by keyword.
  287. .. doctest::
  288. >>> thing = object()
  289. >>> other = object()
  290. >>> @patch.multiple('__main__', thing=DEFAULT, other=DEFAULT)
  291. ... def test_function(thing, other):
  292. ... assert isinstance(thing, MagicMock)
  293. ... assert isinstance(other, MagicMock)
  294. ...
  295. >>> test_function()
  296. `patch.multiple` can be nested with other `patch` decorators, but put arguments
  297. passed by keyword *after* any of the standard arguments created by `patch`:
  298. .. doctest::
  299. >>> @patch('sys.exit')
  300. ... @patch.multiple('__main__', thing=DEFAULT, other=DEFAULT)
  301. ... def test_function(mock_exit, other, thing):
  302. ... assert 'other' in repr(other)
  303. ... assert 'thing' in repr(thing)
  304. ... assert 'exit' in repr(mock_exit)
  305. ...
  306. >>> test_function()
  307. If `patch.multiple` is used as a context manager, the value returned by the
  308. context manger is a dictionary where created mocks are keyed by name:
  309. .. doctest::
  310. >>> with patch.multiple('__main__', thing=DEFAULT, other=DEFAULT) as values:
  311. ... assert 'other' in repr(values['other'])
  312. ... assert 'thing' in repr(values['thing'])
  313. ... assert values['thing'] is thing
  314. ... assert values['other'] is other
  315. ...
  316. .. _start-and-stop:
  317. patch methods: start and stop
  318. =============================
  319. All the patchers have `start` and `stop` methods. These make it simpler to do
  320. patching in `setUp` methods or where you want to do multiple patches without
  321. nesting decorators or with statements.
  322. To use them call `patch`, `patch.object` or `patch.dict` as normal and keep a
  323. reference to the returned `patcher` object. You can then call `start` to put
  324. the patch in place and `stop` to undo it.
  325. If you are using `patch` to create a mock for you then it will be returned by
  326. the call to `patcher.start`.
  327. .. doctest::
  328. >>> patcher = patch('package.module.ClassName')
  329. >>> from package import module
  330. >>> original = module.ClassName
  331. >>> new_mock = patcher.start()
  332. >>> assert module.ClassName is not original
  333. >>> assert module.ClassName is new_mock
  334. >>> patcher.stop()
  335. >>> assert module.ClassName is original
  336. >>> assert module.ClassName is not new_mock
  337. A typical use case for this might be for doing multiple patches in the `setUp`
  338. method of a `TestCase`:
  339. .. doctest::
  340. >>> class MyTest(TestCase):
  341. ... def setUp(self):
  342. ... self.patcher1 = patch('package.module.Class1')
  343. ... self.patcher2 = patch('package.module.Class2')
  344. ... self.MockClass1 = self.patcher1.start()
  345. ... self.MockClass2 = self.patcher2.start()
  346. ...
  347. ... def tearDown(self):
  348. ... self.patcher1.stop()
  349. ... self.patcher2.stop()
  350. ...
  351. ... def test_something(self):
  352. ... assert package.module.Class1 is self.MockClass1
  353. ... assert package.module.Class2 is self.MockClass2
  354. ...
  355. >>> MyTest('test_something').run()
  356. .. caution::
  357. If you use this technique you must ensure that the patching is "undone" by
  358. calling `stop`. This can be fiddlier than you might think, because if an
  359. exception is raised in the setUp then tearDown is not called. `unittest2
  360. <http://pypi.python.org/pypi/unittest2>`_ cleanup functions make this
  361. easier.
  362. .. doctest::
  363. >>> class MyTest(TestCase):
  364. ... def setUp(self):
  365. ... patcher = patch('package.module.Class')
  366. ... self.MockClass = patcher.start()
  367. ... self.addCleanup(patcher.stop)
  368. ...
  369. ... def test_something(self):
  370. ... assert package.module.Class is self.MockClass
  371. ...
  372. >>> MyTest('test_something').run()
  373. As an added bonus you no longer need to keep a reference to the `patcher`
  374. object.
  375. It is also possible to stop all patches which have been started by using
  376. `patch.stopall`.
  377. .. function:: patch.stopall
  378. Stop all active patches. Only stops patches started with `start`.
  379. TEST_PREFIX
  380. ===========
  381. All of the patchers can be used as class decorators. When used in this way
  382. they wrap every test method on the class. The patchers recognise methods that
  383. start with `test` as being test methods. This is the same way that the
  384. `unittest.TestLoader` finds test methods by default.
  385. It is possible that you want to use a different prefix for your tests. You can
  386. inform the patchers of the different prefix by setting `patch.TEST_PREFIX`:
  387. .. doctest::
  388. >>> patch.TEST_PREFIX = 'foo'
  389. >>> value = 3
  390. >>>
  391. >>> @patch('__main__.value', 'not three')
  392. ... class Thing(object):
  393. ... def foo_one(self):
  394. ... print value
  395. ... def foo_two(self):
  396. ... print value
  397. ...
  398. >>>
  399. >>> Thing().foo_one()
  400. not three
  401. >>> Thing().foo_two()
  402. not three
  403. >>> value
  404. 3
  405. Nesting Patch Decorators
  406. ========================
  407. If you want to perform multiple patches then you can simply stack up the
  408. decorators.
  409. You can stack up multiple patch decorators using this pattern:
  410. .. doctest::
  411. >>> @patch.object(SomeClass, 'class_method')
  412. ... @patch.object(SomeClass, 'static_method')
  413. ... def test(mock1, mock2):
  414. ... assert SomeClass.static_method is mock1
  415. ... assert SomeClass.class_method is mock2
  416. ... SomeClass.static_method('foo')
  417. ... SomeClass.class_method('bar')
  418. ... return mock1, mock2
  419. ...
  420. >>> mock1, mock2 = test()
  421. >>> mock1.assert_called_once_with('foo')
  422. >>> mock2.assert_called_once_with('bar')
  423. Note that the decorators are applied from the bottom upwards. This is the
  424. standard way that Python applies decorators. The order of the created mocks
  425. passed into your test function matches this order.
  426. Like all context-managers patches can be nested using contextlib's nested
  427. function; *every* patching will appear in the tuple after "as":
  428. .. doctest::
  429. >>> from contextlib import nested
  430. >>> with nested(
  431. ... patch('package.module.ClassName1'),
  432. ... patch('package.module.ClassName2')
  433. ... ) as (MockClass1, MockClass2):
  434. ... assert package.module.ClassName1 is MockClass1
  435. ... assert package.module.ClassName2 is MockClass2
  436. ...
  437. .. _where-to-patch:
  438. Where to patch
  439. ==============
  440. `patch` works by (temporarily) changing the object that a *name* points to with
  441. another one. There can be many names pointing to any individual object, so
  442. for patching to work you must ensure that you patch the name used by the system
  443. under test.
  444. The basic principle is that you patch where an object is *looked up*, which
  445. is not necessarily the same place as where it is defined. A couple of
  446. examples will help to clarify this.
  447. Imagine we have a project that we want to test with the following structure::
  448. a.py
  449. -> Defines SomeClass
  450. b.py
  451. -> from a import SomeClass
  452. -> some_function instantiates SomeClass
  453. Now we want to test `some_function` but we want to mock out `SomeClass` using
  454. `patch`. The problem is that when we import module b, which we will have to
  455. do then it imports `SomeClass` from module a. If we use `patch` to mock out
  456. `a.SomeClass` then it will have no effect on our test; module b already has a
  457. reference to the *real* `SomeClass` and it looks like our patching had no
  458. effect.
  459. The key is to patch out `SomeClass` where it is used (or where it is looked up
  460. ). In this case `some_function` will actually look up `SomeClass` in module b,
  461. where we have imported it. The patching should look like:
  462. `@patch('b.SomeClass')`
  463. However, consider the alternative scenario where instead of `from a import
  464. SomeClass` module b does `import a` and `some_function` uses `a.SomeClass`. Both
  465. of these import forms are common. In this case the class we want to patch is
  466. being looked up on the a module and so we have to patch `a.SomeClass` instead:
  467. `@patch('a.SomeClass')`
  468. Patching Descriptors and Proxy Objects
  469. ======================================
  470. Since version 0.6.0 both patch_ and patch.object_ have been able to correctly
  471. patch and restore descriptors: class methods, static methods and properties.
  472. You should patch these on the *class* rather than an instance.
  473. Since version 0.7.0 patch_ and patch.object_ work correctly with some objects
  474. that proxy attribute access, like the `django setttings object
  475. <http://www.voidspace.org.uk/python/weblog/arch_d7_2010_12_04.shtml#e1198>`_.
  476. .. note::
  477. In django `import settings` and `from django.conf import settings`
  478. return different objects. If you are using libraries / apps that do both you
  479. may have to patch both. Grrr...