/
mocking.txt
414 lines (284 loc) · 11.5 KB
/
mocking.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
Mocking out objects and methods
===============================
Mocking is the process of replacing chunks of complex functionality
that aren't the subject of the test with mock objects that allow you
to check that the mocked out functionality is being used as expected.
In this way, you can break down testing of a complicated set of
interacting components into testing of each individual component.
The behaviour of components can then be tested individually,
irrespective of the behaviour of the components around it.
There are a few implementations of mock objects in the python world. An
excellent example and the one recommended for use with TestFixtures is
the Mock package: http://pypi.python.org/pypi/mock/
Methods of replacement
----------------------
TestFixtures provides three different methods of mocking out
functionality that can be used to replace functions, classes
or even individual methods on a class. Consider the following module:
.. topic:: testfixtures.tests.sample1
:class: module
.. literalinclude:: ../testfixtures/tests/sample1.py
:pyobject: X
.. do the import quietly
>>> from testfixtures.tests.sample1 import X
We want to mock out the ``y`` method of the ``X`` class, with,
for example, the following function:
.. code-block:: python
def mock_y(self):
return 'mock y'
The context manager
~~~~~~~~~~~~~~~~~~~
For replacement of a single thing, it's easiest to use the
:class:`~testfixtures.Replace` context manager:
.. code-block:: python
from testfixtures import Replace
def test_function():
with Replace('testfixtures.tests.sample1.X.y', mock_y):
print(X().y())
For the duration of the ``with`` block, the replacement is used:
>>> test_function()
mock y
For multiple replacements to do, or where the you need access to the replacement
within the code block under test, the :class:`~testfixtures.Replacer` context
manager can be used instead:
.. code-block:: python
from mock import Mock
from testfixtures import Replacer
def test_function():
with Replacer() as replace:
mock_y = replace('testfixtures.tests.sample1.X.y', Mock())
mock_y.return_value = 'mock y'
print(X().y())
For the duration of the ``with`` block, the replacement is used:
>>> test_function()
mock y
The decorator
~~~~~~~~~~~~~
If you are working in a traditional :mod:`unittest` environment and
want to replace different things in different test functions, you may
find the decorator suits your needs better:
.. code-block:: python
from testfixtures import replace
@replace('testfixtures.tests.sample1.X.y', mock_y)
def test_function():
print(X().y())
When using the decorator, the replacement is used for the duration of
the decorated callable's execution:
>>> test_function()
mock y
If you need to manipulate or inspect the object that's used as a
replacement, you can add an extra parameter to your function. The
decorator will see this and pass the replacement in it's place:
.. code-block:: python
from mock import Mock, call
from testfixtures import compare,replace
@replace('testfixtures.tests.sample1.X.y', Mock())
def test_function(mock_y):
mock_y.return_value = 'mock y'
print(X().y())
compare(mock_y.mock_calls, expected=[call()])
The above still results in the same output:
>>> test_function()
mock y
Manual usage
~~~~~~~~~~~~
If you want to replace something for the duration of a doctest or you
want to replace something for every test in a
:class:`~unittest.TestCase`, then you can use the
:class:`~testfixtures.Replacer` manually.
The instantiation and replacement are done in the ``setUp`` function
of the :class:`~unittest.TestCase` or passed to the
:class:`~doctest.DocTestSuite` constructor:
>>> from testfixtures import Replacer
>>> replace = Replacer()
>>> replace('testfixtures.tests.sample1.X.y', mock_y)
<...>
The replacement then stays in place until removed:
>>> X().y()
'mock y'
Then, in the ``tearDown`` function
of the :class:`~unittest.TestCase` or passed to the
:class:`~doctest.DocTestSuite` constructor, the replacement is
removed:
>>> replace.restore()
>>> X().y()
'original y'
The :meth:`~testfixtures.Replacer.restore` method can also be added as an
:meth:`~unittest.TestCase.addCleanup` if that is easier or more compact in your test
suite.
Replacing more than one thing
-----------------------------
Both the :class:`~testfixtures.Replacer` and the
:func:`~testfixtures.replace` decorator can be used to replace more
than one thing at a time. For the former, this is fairly obvious:
.. code-block:: python
def test_function():
with Replacer() as replace:
y = replace('testfixtures.tests.sample1.X.y', Mock())
y.return_value = 'mock y'
aMethod = replace('testfixtures.tests.sample1.X.aMethod', Mock())
aMethod.return_value = 'mock method'
x = X()
print(x.y(), x.aMethod())
.. the result:
>>> test_function()
mock y mock method
For the decorator, it's less obvious but still pretty easy:
.. code-block:: python
from testfixtures import replace
@replace('testfixtures.tests.sample1.X.y', Mock())
@replace('testfixtures.tests.sample1.X.aMethod', Mock())
def test_function(aMethod, y):
print(aMethod, y)
aMethod().return_value = 'mock method'
y().return_value = 'mock y'
x = X()
print(aMethod, y)
print(x.y(), x.aMethod())
You'll notice that you can still get access to the replacements, even
though there are several of them.
Replacing things that may not be there
--------------------------------------
The following code shows a situation where ``hpy`` may or may not be
present depending on whether the ``guppy`` package is installed or
not.
.. topic:: testfixtures.tests.sample2
:class: module
.. literalinclude:: ../testfixtures/tests/sample2.py
:lines: 10-19
To test the behaviour of the code that uses ``hpy`` in both of
these cases, regardless of whether or not the ``guppy`` package is
actually installed, we need to be able to mock out both ``hpy`` and the
``guppy`` global. This is done by doing non-strict replacement, as
shown in the following :class:`~unittest.TestCase`:
.. imports
>>> import unittest,sys
.. code-block:: python
from testfixtures.tests.sample2 import dump
from testfixtures import replace
from mock import Mock, call
class Tests(unittest.TestCase):
@replace('testfixtures.tests.sample2.guppy', True)
@replace('testfixtures.tests.sample2.hpy', Mock(), strict=False)
def test_method(self, hpy):
dump('somepath')
compare([
call(),
call().heap(),
call().heap().stat.dump('somepath')
], hpy.mock_calls)
@replace('testfixtures.tests.sample2.guppy', False)
@replace('testfixtures.tests.sample2.hpy', Mock(), strict=False)
def test_method_no_heapy(self,hpy):
dump('somepath')
compare(hpy.mock_calls,[])
.. the result:
>>> from testfixtures.compat import StringIO
>>> suite = unittest.TestLoader().loadTestsFromTestCase(Tests)
>>> unittest.TextTestRunner(verbosity=0,stream=StringIO()).run(suite)
<unittest...TextTestResult run=2 errors=0 failures=0>
The :meth:`~testfixtures.Replacer.replace` method and calling
a :class:`Replacer` also supports non-strict replacement using the same
keyword parameter.
Replacing items in dictionaries and lists
-----------------------------------------
:class:`~testfixtures.Replace`, :class:`~testfixtures.Replacer` and the
:func:`~testfixtures.replace` decorator can be used to replace items
in dictionaries and lists.
For example, suppose you have a data structure like the following:
.. topic:: testfixtures.tests.sample1
:class: module
.. literalinclude:: ../testfixtures/tests/sample1.py
:lines: 67-70
You can mock out the value associated with ``key`` and the second
element in the ``complex_key`` list as follows:
.. code-block:: python
from pprint import pprint
from testfixtures import Replacer
from testfixtures.tests.sample1 import someDict
def test_function():
with Replacer() as replace:
replace('testfixtures.tests.sample1.someDict.key', 'foo')
replace('testfixtures.tests.sample1.someDict.complex_key.1', 42)
pprint(someDict)
While the replacement is in effect, the new items are in place:
>>> test_function()
{'complex_key': [1, 42, 3], 'key': 'foo'}
When it is no longer in effect, the originals are returned:
>>> pprint(someDict)
{'complex_key': [1, 2, 3], 'key': 'value'}
.. _removing_attr_and_item:
Removing attributes and dictionary items
----------------------------------------
:class:`~testfixtures.Replace`, :class:`~testfixtures.Replacer` and the
:func:`~testfixtures.replace` decorator can be used to remove
attributes from objects and remove items from dictionaries.
For example, suppose you have a data structure like the following:
.. topic:: testfixtures.tests.sample1
:class: module
.. literalinclude:: ../testfixtures/tests/sample1.py
:lines: 67-70
If you want to remove the ``key`` for the duration of a test, you can
do so as follows:
.. code-block:: python
from testfixtures import Replacer, not_there
from testfixtures.tests.sample1 import someDict
def test_function():
with Replace('testfixtures.tests.sample1.someDict.key', not_there):
pprint(someDict)
While the replacement is in effect, ``key`` is gone:
>>> test_function()
{'complex_key': [1, 2, 3]}
When it is no longer in effect, ``key`` is returned:
>>> pprint(someDict)
{'complex_key': [1, 2, 3], 'key': 'value'}
If you want the whole ``someDict`` dictionary to be removed for the
duration of a test, you would do so as follows:
.. code-block:: python
from testfixtures import Replacer, not_there
from testfixtures.tests import sample1
def test_function():
with Replace('testfixtures.tests.sample1.someDict', not_there):
print(hasattr(sample1, 'someDict'))
While the replacement is in effect, ``key`` is gone:
>>> test_function()
False
When it is no longer in effect, ``key`` is returned:
>>> pprint(sample1.someDict)
{'complex_key': [1, 2, 3], 'key': 'value'}
Gotchas
-------
- Make sure you replace the object where it's used and not where it's
defined. For example, with the following code from the
``testfixtures.tests.sample1`` package:
.. literalinclude:: ../testfixtures/tests/sample1.py
:lines: 30-34
You might be tempted to mock things as follows:
>>> replace = Replacer()
>>> replace('time.time', Mock())
<...>
But this won't work:
>>> from testfixtures.tests.sample1 import str_time
>>> type(float(str_time()))
<... 'float'>
You need to replace :func:`~time.time` where it's used, not where
it's defined:
>>> replace('testfixtures.tests.sample1.time', Mock())
<...>
>>> str_time()
"<...Mock...>"
.. cleanup
>>> replace.restore()
A corollary of this is that you need to replace *all* occurrences of
an original to safely be able to test. This can be tricky when an
original is imported into many modules that may be used by a
particular test.
- You can't replace whole top level modules, and nor should you want
to! The reason being that everything up to the last dot in the
replacement target specifies where the replacement will take place,
and the part after the last dot is used as the name of the thing to
be replaced:
>>> Replacer().replace('sys', Mock())
Traceback (most recent call last):
...
ValueError: target must contain at least one dot!