This file is indexed.

/usr/share/doc/python-testfixtures-doc/html/mocking.html is in python-testfixtures-doc 4.14.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Mocking out objects and methods &#8212; testfixtures 4.14.3 documentation</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '4.14.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Mocking dates and times" href="datetime.html" />
    <link rel="prev" title="Comparing objects and sequences" href="comparing.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="datetime.html" title="Mocking dates and times"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="comparing.html" title="Comparing objects and sequences"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">testfixtures 4.14.3 documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="mocking-out-objects-and-methods">
<h1>Mocking out objects and methods<a class="headerlink" href="#mocking-out-objects-and-methods" title="Permalink to this headline"></a></h1>
<p>Mocking is the process of replacing chunks of complex functionality
that aren&#8217;t the subject of the test with mock objects that allow you
to check that the mocked out functionality is being used as expected.</p>
<p>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.</p>
<p>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: <a class="reference external" href="http://pypi.python.org/pypi/mock/">http://pypi.python.org/pypi/mock/</a></p>
<div class="section" id="methods-of-replacement">
<h2>Methods of replacement<a class="headerlink" href="#methods-of-replacement" title="Permalink to this headline"></a></h2>
<p>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:</p>
<div class="module topic">
<p class="topic-title first">testfixtures.tests.sample1</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">X</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">y</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;original y&quot;</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">aMethod</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">cls</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">bMethod</span><span class="p">():</span>
        <span class="k">return</span> <span class="mi">2</span>
</pre></div>
</div>
</div>
<p>We want to mock out the <code class="docutils literal"><span class="pre">y</span></code> method of the <code class="docutils literal"><span class="pre">X</span></code> class, with,
for example, the following function:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">mock_y</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
     <span class="k">return</span> <span class="s1">&#39;mock y&#39;</span>
</pre></div>
</div>
<div class="section" id="the-context-manager">
<h3>The context manager<a class="headerlink" href="#the-context-manager" title="Permalink to this headline"></a></h3>
<p>For replacement of a single thing, it&#8217;s easiest to use the
<a class="reference internal" href="api.html#testfixtures.Replace" title="testfixtures.Replace"><code class="xref py py-class docutils literal"><span class="pre">Replace</span></code></a> context manager:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">Replace</span>

<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
    <span class="k">with</span> <span class="n">Replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.X.y&#39;</span><span class="p">,</span> <span class="n">mock_y</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="n">X</span><span class="p">()</span><span class="o">.</span><span class="n">y</span><span class="p">())</span>
</pre></div>
</div>
<p>For the duration of the <code class="docutils literal"><span class="pre">with</span></code> block, the replacement is used:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">test_function</span><span class="p">()</span>
<span class="go">mock y</span>
</pre></div>
</div>
<p>For multiple replacements to do, or where the you need access to the replacement
within the code block under test, the <a class="reference internal" href="api.html#testfixtures.Replacer" title="testfixtures.Replacer"><code class="xref py py-class docutils literal"><span class="pre">Replacer</span></code></a> context
manager can be used instead:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">mock</span> <span class="kn">import</span> <span class="n">Mock</span>
<span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">Replacer</span>

<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
    <span class="k">with</span> <span class="n">Replacer</span><span class="p">()</span> <span class="k">as</span> <span class="n">replace</span><span class="p">:</span>
        <span class="n">mock_y</span> <span class="o">=</span> <span class="n">replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.X.y&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">())</span>
        <span class="n">mock_y</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s1">&#39;mock y&#39;</span>
        <span class="k">print</span><span class="p">(</span><span class="n">X</span><span class="p">()</span><span class="o">.</span><span class="n">y</span><span class="p">())</span>
</pre></div>
</div>
<p>For the duration of the <code class="docutils literal"><span class="pre">with</span></code> block, the replacement is used:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">test_function</span><span class="p">()</span>
<span class="go">mock y</span>
</pre></div>
</div>
</div>
<div class="section" id="the-decorator">
<h3>The decorator<a class="headerlink" href="#the-decorator" title="Permalink to this headline"></a></h3>
<p>If you are working in a traditional <a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#module-unittest" title="(in Python v3.5)"><code class="xref py py-mod docutils literal"><span class="pre">unittest</span></code></a> environment and
want to replace different things in different test functions, you may
find the decorator suits your needs better:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">replace</span>

<span class="nd">@replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.X.y&#39;</span><span class="p">,</span> <span class="n">mock_y</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
    <span class="k">print</span><span class="p">(</span><span class="n">X</span><span class="p">()</span><span class="o">.</span><span class="n">y</span><span class="p">())</span>
</pre></div>
</div>
<p>When using the decorator, the replacement is used for the duration of
the decorated callable&#8217;s execution:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">test_function</span><span class="p">()</span>
<span class="go">mock y</span>
</pre></div>
</div>
<p>If you need to manipulate or inspect the object that&#8217;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&#8217;s place:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">mock</span> <span class="kn">import</span> <span class="n">Mock</span><span class="p">,</span> <span class="n">call</span>
<span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">compare</span><span class="p">,</span><span class="n">replace</span>

<span class="nd">@replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.X.y&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">(</span><span class="n">mock_y</span><span class="p">):</span>
    <span class="n">mock_y</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s1">&#39;mock y&#39;</span>
    <span class="k">print</span><span class="p">(</span><span class="n">X</span><span class="p">()</span><span class="o">.</span><span class="n">y</span><span class="p">())</span>
    <span class="n">compare</span><span class="p">(</span><span class="n">mock_y</span><span class="o">.</span><span class="n">mock_calls</span><span class="p">,</span> <span class="n">expected</span><span class="o">=</span><span class="p">[</span><span class="n">call</span><span class="p">()])</span>
</pre></div>
</div>
<p>The above still results in the same output:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">test_function</span><span class="p">()</span>
<span class="go">mock y</span>
</pre></div>
</div>
</div>
<div class="section" id="manual-usage">
<h3>Manual usage<a class="headerlink" href="#manual-usage" title="Permalink to this headline"></a></h3>
<p>If you want to replace something for the duration of a doctest or you
want to replace something for every test in a
<a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#unittest.TestCase" title="(in Python v3.5)"><code class="xref py py-class docutils literal"><span class="pre">TestCase</span></code></a>, then you can use the
<a class="reference internal" href="api.html#testfixtures.Replacer" title="testfixtures.Replacer"><code class="xref py py-class docutils literal"><span class="pre">Replacer</span></code></a> manually.</p>
<p>The instantiation and replacement are done in the <code class="docutils literal"><span class="pre">setUp</span></code> function
of the <a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#unittest.TestCase" title="(in Python v3.5)"><code class="xref py py-class docutils literal"><span class="pre">TestCase</span></code></a> or passed to the
<code class="xref py py-class docutils literal"><span class="pre">DocTestSuite</span></code> constructor:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="k">import</span> <span class="n">Replacer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">replace</span> <span class="o">=</span> <span class="n">Replacer</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.X.y&#39;</span><span class="p">,</span> <span class="n">mock_y</span><span class="p">)</span>
<span class="go">&lt;...&gt;</span>
</pre></div>
</div>
<p>The replacement then stays in place until removed:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">X</span><span class="p">()</span><span class="o">.</span><span class="n">y</span><span class="p">()</span>
<span class="go">&#39;mock y&#39;</span>
</pre></div>
</div>
<p>Then, in the <code class="docutils literal"><span class="pre">tearDown</span></code> function
of the <a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#unittest.TestCase" title="(in Python v3.5)"><code class="xref py py-class docutils literal"><span class="pre">TestCase</span></code></a> or passed to the
<code class="xref py py-class docutils literal"><span class="pre">DocTestSuite</span></code> constructor, the replacement is
removed:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">replace</span><span class="o">.</span><span class="n">restore</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span><span class="p">()</span><span class="o">.</span><span class="n">y</span><span class="p">()</span>
<span class="go">&#39;original y&#39;</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="api.html#testfixtures.Replacer.restore" title="testfixtures.Replacer.restore"><code class="xref py py-meth docutils literal"><span class="pre">restore()</span></code></a> method can also be added as an
<a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.addCleanup" title="(in Python v3.5)"><code class="xref py py-meth docutils literal"><span class="pre">addCleanup()</span></code></a> if that is easier or more compact in your test
suite.</p>
</div>
</div>
<div class="section" id="replacing-more-than-one-thing">
<h2>Replacing more than one thing<a class="headerlink" href="#replacing-more-than-one-thing" title="Permalink to this headline"></a></h2>
<p>Both the <a class="reference internal" href="api.html#testfixtures.Replacer" title="testfixtures.Replacer"><code class="xref py py-class docutils literal"><span class="pre">Replacer</span></code></a> and the
<a class="reference internal" href="api.html#testfixtures.replace" title="testfixtures.replace"><code class="xref py py-func docutils literal"><span class="pre">replace()</span></code></a> decorator can be used to replace more
than one thing at a time. For the former, this is fairly obvious:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
    <span class="k">with</span> <span class="n">Replacer</span><span class="p">()</span> <span class="k">as</span> <span class="n">replace</span><span class="p">:</span>
        <span class="n">y</span> <span class="o">=</span> <span class="n">replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.X.y&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">())</span>
        <span class="n">y</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s1">&#39;mock y&#39;</span>
        <span class="n">aMethod</span> <span class="o">=</span> <span class="n">replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.X.aMethod&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">())</span>
        <span class="n">aMethod</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s1">&#39;mock method&#39;</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">X</span><span class="p">()</span>
        <span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">y</span><span class="p">(),</span> <span class="n">x</span><span class="o">.</span><span class="n">aMethod</span><span class="p">())</span>
</pre></div>
</div>
<p>For the decorator, it&#8217;s less obvious but still pretty easy:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">replace</span>

<span class="nd">@replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.X.y&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">())</span>
<span class="nd">@replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.X.aMethod&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">(</span><span class="n">aMethod</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
    <span class="k">print</span><span class="p">(</span><span class="n">aMethod</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
    <span class="n">aMethod</span><span class="p">()</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s1">&#39;mock method&#39;</span>
    <span class="n">y</span><span class="p">()</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s1">&#39;mock y&#39;</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">X</span><span class="p">()</span>
    <span class="k">print</span><span class="p">(</span><span class="n">aMethod</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">y</span><span class="p">(),</span> <span class="n">x</span><span class="o">.</span><span class="n">aMethod</span><span class="p">())</span>
</pre></div>
</div>
<p>You&#8217;ll notice that you can still get access to the replacements, even
though there are several of them.</p>
</div>
<div class="section" id="replacing-things-that-may-not-be-there">
<h2>Replacing things that may not be there<a class="headerlink" href="#replacing-things-that-may-not-be-there" title="Permalink to this headline"></a></h2>
<p>The following code shows a situation where <code class="docutils literal"><span class="pre">hpy</span></code> may or may not be
present depending on whether the <code class="docutils literal"><span class="pre">guppy</span></code> package is installed or
not.</p>
<div class="module topic">
<p class="topic-title first">testfixtures.tests.sample2</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">guppy</span> <span class="k">import</span> <span class="n">hpy</span>
    <span class="n">guppy</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="n">guppy</span> <span class="o">=</span> <span class="kc">False</span>


<span class="k">def</span> <span class="nf">dump</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">guppy</span><span class="p">:</span>
        <span class="n">hpy</span><span class="p">()</span><span class="o">.</span><span class="n">heap</span><span class="p">()</span><span class="o">.</span><span class="n">stat</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>To test the behaviour of the code that uses <code class="docutils literal"><span class="pre">hpy</span></code> in both of
these cases, regardless of whether or not the <code class="docutils literal"><span class="pre">guppy</span></code> package is
actually installed, we need to be able to mock out both <code class="docutils literal"><span class="pre">hpy</span></code> and the
<code class="docutils literal"><span class="pre">guppy</span></code> global. This is done by doing non-strict replacement, as
shown in the following <a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#unittest.TestCase" title="(in Python v3.5)"><code class="xref py py-class docutils literal"><span class="pre">TestCase</span></code></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures.tests.sample2</span> <span class="kn">import</span> <span class="n">dump</span>
<span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">replace</span>
<span class="kn">from</span> <span class="nn">mock</span> <span class="kn">import</span> <span class="n">Mock</span><span class="p">,</span> <span class="n">call</span>

<span class="k">class</span> <span class="nc">Tests</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>

    <span class="nd">@replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample2.guppy&#39;</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
    <span class="nd">@replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample2.hpy&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">(),</span> <span class="n">strict</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">test_method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hpy</span><span class="p">):</span>

        <span class="n">dump</span><span class="p">(</span><span class="s1">&#39;somepath&#39;</span><span class="p">)</span>

        <span class="n">compare</span><span class="p">([</span>
                 <span class="n">call</span><span class="p">(),</span>
                 <span class="n">call</span><span class="p">()</span><span class="o">.</span><span class="n">heap</span><span class="p">(),</span>
                 <span class="n">call</span><span class="p">()</span><span class="o">.</span><span class="n">heap</span><span class="p">()</span><span class="o">.</span><span class="n">stat</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="s1">&#39;somepath&#39;</span><span class="p">)</span>
               <span class="p">],</span> <span class="n">hpy</span><span class="o">.</span><span class="n">mock_calls</span><span class="p">)</span>

    <span class="nd">@replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample2.guppy&#39;</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
    <span class="nd">@replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample2.hpy&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">(),</span> <span class="n">strict</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">test_method_no_heapy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">hpy</span><span class="p">):</span>

        <span class="n">dump</span><span class="p">(</span><span class="s1">&#39;somepath&#39;</span><span class="p">)</span>

        <span class="n">compare</span><span class="p">(</span><span class="n">hpy</span><span class="o">.</span><span class="n">mock_calls</span><span class="p">,[])</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="api.html#testfixtures.Replacer.replace" title="testfixtures.Replacer.replace"><code class="xref py py-meth docutils literal"><span class="pre">replace()</span></code></a> method and calling
a <code class="xref py py-class docutils literal"><span class="pre">Replacer</span></code> also supports non-strict replacement using the same
keyword parameter.</p>
</div>
<div class="section" id="replacing-items-in-dictionaries-and-lists">
<h2>Replacing items in dictionaries and lists<a class="headerlink" href="#replacing-items-in-dictionaries-and-lists" title="Permalink to this headline"></a></h2>
<p><a class="reference internal" href="api.html#testfixtures.Replace" title="testfixtures.Replace"><code class="xref py py-class docutils literal"><span class="pre">Replace</span></code></a>, <a class="reference internal" href="api.html#testfixtures.Replacer" title="testfixtures.Replacer"><code class="xref py py-class docutils literal"><span class="pre">Replacer</span></code></a> and the
<a class="reference internal" href="api.html#testfixtures.replace" title="testfixtures.replace"><code class="xref py py-func docutils literal"><span class="pre">replace()</span></code></a> decorator can be used to replace items
in dictionaries and lists.</p>
<p>For example, suppose you have a data structure like the following:</p>
<div class="module topic">
<p class="topic-title first">testfixtures.tests.sample1</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">someDict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
    <span class="n">key</span><span class="o">=</span><span class="s1">&#39;value&#39;</span><span class="p">,</span>
    <span class="n">complex_key</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
    <span class="p">)</span>
</pre></div>
</div>
</div>
<p>You can mock out the value associated with <code class="docutils literal"><span class="pre">key</span></code> and the second
element in the <code class="docutils literal"><span class="pre">complex_key</span></code> list as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pprint</span> <span class="kn">import</span> <span class="n">pprint</span>
<span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">Replacer</span>
<span class="kn">from</span> <span class="nn">testfixtures.tests.sample1</span> <span class="kn">import</span> <span class="n">someDict</span>

<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
    <span class="k">with</span> <span class="n">Replacer</span><span class="p">()</span> <span class="k">as</span> <span class="n">replace</span><span class="p">:</span>
        <span class="n">replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.someDict.key&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span>
        <span class="n">replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.someDict.complex_key.1&#39;</span><span class="p">,</span> <span class="mi">42</span><span class="p">)</span>
        <span class="n">pprint</span><span class="p">(</span><span class="n">someDict</span><span class="p">)</span>
</pre></div>
</div>
<p>While the replacement is in effect, the new items are in place:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">test_function</span><span class="p">()</span>
<span class="go">{&#39;complex_key&#39;: [1, 42, 3], &#39;key&#39;: &#39;foo&#39;}</span>
</pre></div>
</div>
<p>When it is no longer in effect, the originals are returned:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">someDict</span><span class="p">)</span>
<span class="go">{&#39;complex_key&#39;: [1, 2, 3], &#39;key&#39;: &#39;value&#39;}</span>
</pre></div>
</div>
</div>
<div class="section" id="removing-attributes-and-dictionary-items">
<span id="removing-attr-and-item"></span><h2>Removing attributes and dictionary items<a class="headerlink" href="#removing-attributes-and-dictionary-items" title="Permalink to this headline"></a></h2>
<p><a class="reference internal" href="api.html#testfixtures.Replace" title="testfixtures.Replace"><code class="xref py py-class docutils literal"><span class="pre">Replace</span></code></a>, <a class="reference internal" href="api.html#testfixtures.Replacer" title="testfixtures.Replacer"><code class="xref py py-class docutils literal"><span class="pre">Replacer</span></code></a> and the
<a class="reference internal" href="api.html#testfixtures.replace" title="testfixtures.replace"><code class="xref py py-func docutils literal"><span class="pre">replace()</span></code></a> decorator can be used to remove
attributes from objects and remove items from dictionaries.</p>
<p>For example, suppose you have a data structure like the following:</p>
<div class="module topic">
<p class="topic-title first">testfixtures.tests.sample1</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">someDict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
    <span class="n">key</span><span class="o">=</span><span class="s1">&#39;value&#39;</span><span class="p">,</span>
    <span class="n">complex_key</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
    <span class="p">)</span>
</pre></div>
</div>
</div>
<p>If you want to remove the <code class="docutils literal"><span class="pre">key</span></code> for the duration of a test, you can
do so as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">Replace</span><span class="p">,</span> <span class="n">not_there</span>
<span class="kn">from</span> <span class="nn">testfixtures.tests.sample1</span> <span class="kn">import</span> <span class="n">someDict</span>

<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
    <span class="k">with</span> <span class="n">Replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.someDict.key&#39;</span><span class="p">,</span> <span class="n">not_there</span><span class="p">):</span>
        <span class="n">pprint</span><span class="p">(</span><span class="n">someDict</span><span class="p">)</span>
</pre></div>
</div>
<p>While the replacement is in effect, <code class="docutils literal"><span class="pre">key</span></code> is gone:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">test_function</span><span class="p">()</span>
<span class="go">{&#39;complex_key&#39;: [1, 2, 3]}</span>
</pre></div>
</div>
<p>When it is no longer in effect, <code class="docutils literal"><span class="pre">key</span></code> is returned:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">someDict</span><span class="p">)</span>
<span class="go">{&#39;complex_key&#39;: [1, 2, 3], &#39;key&#39;: &#39;value&#39;}</span>
</pre></div>
</div>
<p>If you want the whole <code class="docutils literal"><span class="pre">someDict</span></code> dictionary to be removed for the
duration of a test, you would do so as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">Replace</span><span class="p">,</span> <span class="n">not_there</span>
<span class="kn">from</span> <span class="nn">testfixtures.tests</span> <span class="kn">import</span> <span class="n">sample1</span>

<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
    <span class="k">with</span> <span class="n">Replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.someDict&#39;</span><span class="p">,</span> <span class="n">not_there</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">sample1</span><span class="p">,</span> <span class="s1">&#39;someDict&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>While the replacement is in effect, <code class="docutils literal"><span class="pre">key</span></code> is gone:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">test_function</span><span class="p">()</span>
<span class="go">False</span>
</pre></div>
</div>
<p>When it is no longer in effect, <code class="docutils literal"><span class="pre">key</span></code> is returned:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">sample1</span><span class="o">.</span><span class="n">someDict</span><span class="p">)</span>
<span class="go">{&#39;complex_key&#39;: [1, 2, 3], &#39;key&#39;: &#39;value&#39;}</span>
</pre></div>
</div>
</div>
<div class="section" id="gotchas">
<h2>Gotchas<a class="headerlink" href="#gotchas" title="Permalink to this headline"></a></h2>
<ul>
<li><p class="first">Make sure you replace the object where it&#8217;s used and not where it&#8217;s
defined. For example, with the following code from the
<code class="docutils literal"><span class="pre">testfixtures.tests.sample1</span></code> package:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">time</span> <span class="k">import</span> <span class="n">time</span>


<span class="k">def</span> <span class="nf">str_time</span><span class="p">():</span>
    <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">time</span><span class="p">())</span>
</pre></div>
</div>
<p>You might be tempted to mock things as follows:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">replace</span> <span class="o">=</span> <span class="n">Replacer</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;time.time&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">())</span>
<span class="go">&lt;...&gt;</span>
</pre></div>
</div>
<p>But this won&#8217;t work:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">testfixtures.tests.sample1</span> <span class="k">import</span> <span class="n">str_time</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">str_time</span><span class="p">()))</span>
<span class="go">&lt;... &#39;float&#39;&gt;</span>
</pre></div>
</div>
<p>You need to replace <a class="reference external" href="https://docs.python.org/3.5/library/time.html#time.time" title="(in Python v3.5)"><code class="xref py py-func docutils literal"><span class="pre">time()</span></code></a> where it&#8217;s used, not where
it&#8217;s defined:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;testfixtures.tests.sample1.time&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">())</span>
<span class="go">&lt;...&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">str_time</span><span class="p">()</span>
<span class="go">&quot;&lt;...Mock...&gt;&quot;</span>
</pre></div>
</div>
<p>A corollary of this is that you need to replace <em>all</em> 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.</p>
</li>
<li><p class="first">You can&#8217;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:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Replacer</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;sys&#39;</span><span class="p">,</span> <span class="n">Mock</span><span class="p">())</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">target must contain at least one dot!</span>
</pre></div>
</div>
</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Mocking out objects and methods</a><ul>
<li><a class="reference internal" href="#methods-of-replacement">Methods of replacement</a><ul>
<li><a class="reference internal" href="#the-context-manager">The context manager</a></li>
<li><a class="reference internal" href="#the-decorator">The decorator</a></li>
<li><a class="reference internal" href="#manual-usage">Manual usage</a></li>
</ul>
</li>
<li><a class="reference internal" href="#replacing-more-than-one-thing">Replacing more than one thing</a></li>
<li><a class="reference internal" href="#replacing-things-that-may-not-be-there">Replacing things that may not be there</a></li>
<li><a class="reference internal" href="#replacing-items-in-dictionaries-and-lists">Replacing items in dictionaries and lists</a></li>
<li><a class="reference internal" href="#removing-attributes-and-dictionary-items">Removing attributes and dictionary items</a></li>
<li><a class="reference internal" href="#gotchas">Gotchas</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="comparing.html"
                        title="previous chapter">Comparing objects and sequences</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="datetime.html"
                        title="next chapter">Mocking dates and times</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/mocking.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="datetime.html" title="Mocking dates and times"
             >next</a> |</li>
        <li class="right" >
          <a href="comparing.html" title="Comparing objects and sequences"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">testfixtures 4.14.3 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2008-2017 Simplistix Ltd, 2016-2017 Chris Withers.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
    </div>
  </body>
</html>