mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-05 16:46:26 +00:00
1006 lines
108 KiB
HTML
1006 lines
108 KiB
HTML
|
|
<!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>Further Examples — Mock 1.0.0 documentation</title>
|
|
|
|
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
|
|
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT: '',
|
|
VERSION: '1.0.0',
|
|
COLLAPSE_INDEX: false,
|
|
FILE_SUFFIX: '.html',
|
|
HAS_SOURCE: true
|
|
};
|
|
</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="top" title="Mock 1.0.0 documentation" href="index.html" />
|
|
<link rel="next" title="Mock Library Comparison" href="compare.html" />
|
|
<link rel="prev" title="Getting Started with Mock" href="getting-started.html" />
|
|
</head>
|
|
<body>
|
|
<div class="related">
|
|
<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="compare.html" title="Mock Library Comparison"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="getting-started.html" title="Getting Started with Mock"
|
|
accesskey="P">previous</a> |</li>
|
|
<li><a href="index.html">Mock 1.0.0 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body">
|
|
|
|
<div class="section" id="further-examples">
|
|
<span id="id1"></span><h1>Further Examples<a class="headerlink" href="#further-examples" title="Permalink to this headline">¶</a></h1>
|
|
<p>For comprehensive examples, see the unit tests included in the full source
|
|
distribution.</p>
|
|
<p>Here are some more examples for some slightly more advanced scenarios than in
|
|
the <a class="reference internal" href="getting-started.html#getting-started"><em>getting started</em></a> guide.</p>
|
|
<div class="section" id="mocking-chained-calls">
|
|
<h2>Mocking chained calls<a class="headerlink" href="#mocking-chained-calls" title="Permalink to this headline">¶</a></h2>
|
|
<p>Mocking chained calls is actually straightforward with mock once you
|
|
understand the <a class="reference internal" href="mock.html#mock.Mock.return_value" title="mock.Mock.return_value"><tt class="xref py py-attr docutils literal"><span class="pre">return_value</span></tt></a> attribute. When a mock is called for
|
|
the first time, or you fetch its <cite>return_value</cite> before it has been called, a
|
|
new <cite>Mock</cite> is created.</p>
|
|
<p>This means that you can see how the object returned from a call to a mocked
|
|
object has been used by interrogating the <cite>return_value</cite> mock:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">()</span><span class="o">.</span><span class="n">foo</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
|
|
<span class="go"><Mock name='mock().foo()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">return_value</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>From here it is a simple step to configure and then make assertions about
|
|
chained calls. Of course another alternative is writing your code in a more
|
|
testable way in the first place...</p>
|
|
<p>So, suppose we have some code that looks a little bit like this:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Something</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">backend</span> <span class="o">=</span> <span class="n">BackendProvider</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">backend</span><span class="o">.</span><span class="n">get_endpoint</span><span class="p">(</span><span class="s">'foobar'</span><span class="p">)</span><span class="o">.</span><span class="n">create_call</span><span class="p">(</span><span class="s">'spam'</span><span class="p">,</span> <span class="s">'eggs'</span><span class="p">)</span><span class="o">.</span><span class="n">start_call</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="c"># more code</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Assuming that <cite>BackendProvider</cite> is already well tested, how do we test
|
|
<cite>method()</cite>? Specifically, we want to test that the code section <cite># more
|
|
code</cite> uses the response object in the correct way.</p>
|
|
<p>As this chain of calls is made from an instance attribute we can monkey patch
|
|
the <cite>backend</cite> attribute on a <cite>Something</cite> instance. In this particular case
|
|
we are only interested in the return value from the final call to
|
|
<cite>start_call</cite> so we don’t have much configuration to do. Let’s assume the
|
|
object it returns is ‘file-like’, so we’ll ensure that our response object
|
|
uses the builtin <cite>file</cite> as its <cite>spec</cite>.</p>
|
|
<p>To do this we create a mock instance as our mock backend and create a mock
|
|
response object for it. To set the response as the return value for that final
|
|
<cite>start_call</cite> we could do this:</p>
|
|
<blockquote>
|
|
<div><cite>mock_backend.get_endpoint.return_value.create_call.return_value.start_call.return_value = mock_response</cite>.</div></blockquote>
|
|
<p>We can do that in a slightly nicer way using the <a class="reference internal" href="mock.html#mock.Mock.configure_mock" title="mock.Mock.configure_mock"><tt class="xref py py-meth docutils literal"><span class="pre">configure_mock()</span></tt></a>
|
|
method to directly set the return value for us:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">something</span> <span class="o">=</span> <span class="n">Something</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock_response</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">spec</span><span class="o">=</span><span class="nb">file</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock_backend</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">config</span> <span class="o">=</span> <span class="p">{</span><span class="s">'get_endpoint.return_value.create_call.return_value.start_call.return_value'</span><span class="p">:</span> <span class="n">mock_response</span><span class="p">}</span>
|
|
<span class="gp">>>> </span><span class="n">mock_backend</span><span class="o">.</span><span class="n">configure_mock</span><span class="p">(</span><span class="o">**</span><span class="n">config</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>With these we monkey patch the “mock backend” in place and can make the real
|
|
call:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">something</span><span class="o">.</span><span class="n">backend</span> <span class="o">=</span> <span class="n">mock_backend</span>
|
|
<span class="gp">>>> </span><span class="n">something</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Using <a class="reference internal" href="mock.html#mock.Mock.mock_calls" title="mock.Mock.mock_calls"><tt class="xref py py-attr docutils literal"><span class="pre">mock_calls</span></tt></a> we can check the chained call with a single
|
|
assert. A chained call is several calls in one line of code, so there will be
|
|
several entries in <cite>mock_calls</cite>. We can use <a class="reference internal" href="helpers.html#mock.call.call_list" title="mock.call.call_list"><tt class="xref py py-meth docutils literal"><span class="pre">call.call_list()</span></tt></a> to create
|
|
this list of calls for us:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">chained</span> <span class="o">=</span> <span class="n">call</span><span class="o">.</span><span class="n">get_endpoint</span><span class="p">(</span><span class="s">'foobar'</span><span class="p">)</span><span class="o">.</span><span class="n">create_call</span><span class="p">(</span><span class="s">'spam'</span><span class="p">,</span> <span class="s">'eggs'</span><span class="p">)</span><span class="o">.</span><span class="n">start_call</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">call_list</span> <span class="o">=</span> <span class="n">chained</span><span class="o">.</span><span class="n">call_list</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="n">mock_backend</span><span class="o">.</span><span class="n">mock_calls</span> <span class="o">==</span> <span class="n">call_list</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="partial-mocking">
|
|
<h2>Partial mocking<a class="headerlink" href="#partial-mocking" title="Permalink to this headline">¶</a></h2>
|
|
<p>In some tests I wanted to mock out a call to <a class="reference external" href="http://docs.python.org/library/datetime.html#datetime.date.today">datetime.date.today()</a> to return
|
|
a known date, but I didn’t want to prevent the code under test from
|
|
creating new date objects. Unfortunately <cite>datetime.date</cite> is written in C, and
|
|
so I couldn’t just monkey-patch out the static <cite>date.today</cite> method.</p>
|
|
<p>I found a simple way of doing this that involved effectively wrapping the date
|
|
class with a mock, but passing through calls to the constructor to the real
|
|
class (and returning real instances).</p>
|
|
<p>The <a class="reference internal" href="patch.html#mock.patch" title="mock.patch"><tt class="xref py py-func docutils literal"><span class="pre">patch</span> <span class="pre">decorator</span></tt></a> is used here to
|
|
mock out the <cite>date</cite> class in the module under test. The <tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt>
|
|
attribute on the mock date class is then set to a lambda function that returns
|
|
a real date. When the mock date class is called a real date will be
|
|
constructed and returned by <cite>side_effect</cite>.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span>
|
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.date'</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_date</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="n">mock_date</span><span class="o">.</span><span class="n">today</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="mi">2010</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">mock_date</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="k">lambda</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">:</span> <span class="n">date</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span> <span class="o">==</span> <span class="n">date</span><span class="p">(</span><span class="mi">2010</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2009</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span> <span class="o">==</span> <span class="n">date</span><span class="p">(</span><span class="mi">2009</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Note that we don’t patch <cite>datetime.date</cite> globally, we patch <cite>date</cite> in the
|
|
module that <em>uses</em> it. See <a class="reference internal" href="patch.html#where-to-patch"><em>where to patch</em></a>.</p>
|
|
<p>When <cite>date.today()</cite> is called a known date is returned, but calls to the
|
|
<cite>date(...)</cite> constructor still return normal dates. Without this you can find
|
|
yourself having to calculate an expected result using exactly the same
|
|
algorithm as the code under test, which is a classic testing anti-pattern.</p>
|
|
<p>Calls to the date constructor are recorded in the <cite>mock_date</cite> attributes
|
|
(<cite>call_count</cite> and friends) which may also be useful for your tests.</p>
|
|
<p>An alternative way of dealing with mocking dates, or other builtin classes,
|
|
is discussed in <a class="reference external" href="http://williamjohnbert.com/2011/07/how-to-unit-testing-in-django-with-mocking-and-patching/">this blog entry</a>.</p>
|
|
</div>
|
|
<div class="section" id="mocking-a-generator-method">
|
|
<h2>Mocking a Generator Method<a class="headerlink" href="#mocking-a-generator-method" title="Permalink to this headline">¶</a></h2>
|
|
<p>A Python generator is a function or method that uses the <a class="reference external" href="http://docs.python.org/reference/simple_stmts.html#the-yield-statement">yield statement</a> to
|
|
return a series of values when iterated over <a class="footnote-reference" href="#id3" id="id2">[1]</a>.</p>
|
|
<p>A generator method / function is called to return the generator object. It is
|
|
the generator object that is then iterated over. The protocol method for
|
|
iteration is <a class="reference external" href="http://docs.python.org/library/stdtypes.html#container.__iter__">__iter__</a>, so we can
|
|
mock this using a <cite>MagicMock</cite>.</p>
|
|
<p>Here’s an example class with an “iter” method implemented as a generator:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">iter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</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="gp">... </span> <span class="k">yield</span> <span class="n">i</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">foo</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">foo</span><span class="o">.</span><span class="n">iter</span><span class="p">())</span>
|
|
<span class="go">[1, 2, 3]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>How would we mock this class, and in particular its “iter” method?</p>
|
|
<p>To configure the values returned from the iteration (implicit in the call to
|
|
<cite>list</cite>), we need to configure the object returned by the call to <cite>foo.iter()</cite>.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock_foo</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock_foo</span><span class="o">.</span><span class="n">iter</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="nb">iter</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="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">mock_foo</span><span class="o">.</span><span class="n">iter</span><span class="p">())</span>
|
|
<span class="go">[1, 2, 3]</span>
|
|
</pre></div>
|
|
</div>
|
|
<table class="docutils footnote" frame="void" id="id3" rules="none">
|
|
<colgroup><col class="label" /><col /></colgroup>
|
|
<tbody valign="top">
|
|
<tr><td class="label"><a class="fn-backref" href="#id2">[1]</a></td><td>There are also generator expressions and more <a class="reference external" href="http://www.dabeaz.com/coroutines/index.html">advanced uses</a> of generators, but we aren’t
|
|
concerned about them here. A very good introduction to generators and how
|
|
powerful they are is: <a class="reference external" href="http://www.dabeaz.com/generators/">Generator Tricks for Systems Programmers</a>.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="section" id="applying-the-same-patch-to-every-test-method">
|
|
<h2>Applying the same patch to every test method<a class="headerlink" href="#applying-the-same-patch-to-every-test-method" title="Permalink to this headline">¶</a></h2>
|
|
<p>If you want several patches in place for multiple test methods the obvious way
|
|
is to apply the patch decorators to every method. This can feel like unnecessary
|
|
repetition. For Python 2.6 or more recent you can use <cite>patch</cite> (in all its
|
|
various forms) as a class decorator. This applies the patches to all test
|
|
methods on the class. A test method is identified by methods whose names start
|
|
with <cite>test</cite>:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nd">@patch</span><span class="p">(</span><span class="s">'mymodule.SomeClass'</span><span class="p">)</span>
|
|
<span class="gp">... </span><span class="k">class</span> <span class="nc">MyTest</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">test_one</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">MockSomeClass</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span><span class="n">mymodule</span><span class="o">.</span><span class="n">SomeClass</span> <span class="ow">is</span> <span class="n">MockSomeClass</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">test_two</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">MockSomeClass</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span><span class="n">mymodule</span><span class="o">.</span><span class="n">SomeClass</span> <span class="ow">is</span> <span class="n">MockSomeClass</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">not_a_test</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="s">'something'</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">MyTest</span><span class="p">(</span><span class="s">'test_one'</span><span class="p">)</span><span class="o">.</span><span class="n">test_one</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">MyTest</span><span class="p">(</span><span class="s">'test_two'</span><span class="p">)</span><span class="o">.</span><span class="n">test_two</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">MyTest</span><span class="p">(</span><span class="s">'test_two'</span><span class="p">)</span><span class="o">.</span><span class="n">not_a_test</span><span class="p">()</span>
|
|
<span class="go">'something'</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>An alternative way of managing patches is to use the <a class="reference internal" href="patch.html#start-and-stop"><em>patch methods: start and stop</em></a>.
|
|
These allow you to move the patching into your <cite>setUp</cite> and <cite>tearDown</cite> methods.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyTest</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">patcher</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.foo'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">mock_foo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">patcher</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">test_foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span><span class="n">mymodule</span><span class="o">.</span><span class="n">foo</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">mock_foo</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">patcher</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">MyTest</span><span class="p">(</span><span class="s">'test_foo'</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you use this technique you must ensure that the patching is “undone” by
|
|
calling <cite>stop</cite>. This can be fiddlier than you might think, because if an
|
|
exception is raised in the setUp then tearDown is not called. <a class="reference external" href="http://pypi.python.org/pypi/unittest2">unittest2</a> cleanup functions make this simpler:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyTest</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">patcher</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.foo'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">addCleanup</span><span class="p">(</span><span class="n">patcher</span><span class="o">.</span><span class="n">stop</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">mock_foo</span> <span class="o">=</span> <span class="n">patcher</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">test_foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span><span class="n">mymodule</span><span class="o">.</span><span class="n">foo</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">mock_foo</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">MyTest</span><span class="p">(</span><span class="s">'test_foo'</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mocking-unbound-methods">
|
|
<h2>Mocking Unbound Methods<a class="headerlink" href="#mocking-unbound-methods" title="Permalink to this headline">¶</a></h2>
|
|
<p>Whilst writing tests today I needed to patch an <em>unbound method</em> (patching the
|
|
method on the class rather than on the instance). I needed self to be passed
|
|
in as the first argument because I want to make asserts about which objects
|
|
were calling this particular method. The issue is that you can’t patch with a
|
|
mock for this, because if you replace an unbound method with a mock it doesn’t
|
|
become a bound method when fetched from the instance, and so it doesn’t get
|
|
self passed in. The workaround is to patch the unbound method with a real
|
|
function instead. The <a class="reference internal" href="patch.html#mock.patch" title="mock.patch"><tt class="xref py py-func docutils literal"><span class="pre">patch()</span></tt></a> decorator makes it so simple to
|
|
patch out methods with a mock that having to create a real function becomes a
|
|
nuisance.</p>
|
|
<p>If you pass <cite>autospec=True</cite> to patch then it does the patching with a
|
|
<em>real</em> function object. This function object has the same signature as the one
|
|
it is replacing, but delegates to a mock under the hood. You still get your
|
|
mock auto-created in exactly the same way as before. What it means though, is
|
|
that if you use it to patch out an unbound method on a class the mocked
|
|
function will be turned into a bound method if it is fetched from an instance.
|
|
It will have <cite>self</cite> passed in as the first argument, which is exactly what I
|
|
wanted:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">object</span><span class="p">(</span><span class="n">Foo</span><span class="p">,</span> <span class="s">'foo'</span><span class="p">,</span> <span class="n">autospec</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_foo</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="n">mock_foo</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s">'foo'</span>
|
|
<span class="gp">... </span> <span class="n">foo</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="n">foo</span><span class="o">.</span><span class="n">foo</span><span class="p">()</span>
|
|
<span class="gp">...</span>
|
|
<span class="go">'foo'</span>
|
|
<span class="gp">>>> </span><span class="n">mock_foo</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If we don’t use <cite>autospec=True</cite> then the unbound method is patched out
|
|
with a Mock instance instead, and isn’t called with <cite>self</cite>.</p>
|
|
</div>
|
|
<div class="section" id="checking-multiple-calls-with-mock">
|
|
<h2>Checking multiple calls with mock<a class="headerlink" href="#checking-multiple-calls-with-mock" title="Permalink to this headline">¶</a></h2>
|
|
<p>mock has a nice API for making assertions about how your mock objects are used.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">foo_bar</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">foo_bar</span><span class="p">(</span><span class="s">'baz'</span><span class="p">,</span> <span class="n">spam</span><span class="o">=</span><span class="s">'eggs'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">foo_bar</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="s">'baz'</span><span class="p">,</span> <span class="n">spam</span><span class="o">=</span><span class="s">'eggs'</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If your mock is only being called once you can use the
|
|
<tt class="xref py py-meth docutils literal"><span class="pre">assert_called_once_with()</span></tt> method that also asserts that the
|
|
<tt class="xref py py-attr docutils literal"><span class="pre">call_count</span></tt> is one.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">foo_bar</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s">'baz'</span><span class="p">,</span> <span class="n">spam</span><span class="o">=</span><span class="s">'eggs'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">foo_bar</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">foo_bar</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s">'baz'</span><span class="p">,</span> <span class="n">spam</span><span class="o">=</span><span class="s">'eggs'</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">AssertionError</span>: <span class="n">Expected to be called once. Called 2 times.</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Both <cite>assert_called_with</cite> and <cite>assert_called_once_with</cite> make assertions about
|
|
the <em>most recent</em> call. If your mock is going to be called several times, and
|
|
you want to make assertions about <em>all</em> those calls you can use
|
|
<a class="reference internal" href="mock.html#mock.Mock.call_args_list" title="mock.Mock.call_args_list"><tt class="xref py py-attr docutils literal"><span class="pre">call_args_list</span></tt></a>:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">return_value</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</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="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">call_args_list</span>
|
|
<span class="go">[call(1, 2, 3), call(4, 5, 6), call()]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The <a class="reference internal" href="helpers.html#mock.call" title="mock.call"><tt class="xref py py-data docutils literal"><span class="pre">call</span></tt></a> helper makes it easy to make assertions about these calls. You
|
|
can build up a list of expected calls and compare it to <cite>call_args_list</cite>. This
|
|
looks remarkably similar to the repr of the <cite>call_args_list</cite>:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">expected</span> <span class="o">=</span> <span class="p">[</span><span class="n">call</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="n">call</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">),</span> <span class="n">call</span><span class="p">()]</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">call_args_list</span> <span class="o">==</span> <span class="n">expected</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="coping-with-mutable-arguments">
|
|
<h2>Coping with mutable arguments<a class="headerlink" href="#coping-with-mutable-arguments" title="Permalink to this headline">¶</a></h2>
|
|
<p>Another situation is rare, but can bite you, is when your mock is called with
|
|
mutable arguments. <cite>call_args</cite> and <cite>call_args_list</cite> store <em>references</em> to the
|
|
arguments. If the arguments are mutated by the code under test then you can no
|
|
longer make assertions about what the values were when the mock was called.</p>
|
|
<p>Here’s some example code that shows the problem. Imagine the following functions
|
|
defined in ‘mymodule’:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">frob</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
|
|
<span class="k">pass</span>
|
|
|
|
<span class="k">def</span> <span class="nf">grob</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
|
|
<span class="s">"First frob and then clear val"</span>
|
|
<span class="n">frob</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
|
|
<span class="n">val</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>When we try to test that <cite>grob</cite> calls <cite>frob</cite> with the correct argument look
|
|
what happens:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.frob'</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_frob</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="n">val</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="mi">6</span><span class="p">])</span>
|
|
<span class="gp">... </span> <span class="n">mymodule</span><span class="o">.</span><span class="n">grob</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">val</span>
|
|
<span class="go">set([])</span>
|
|
<span class="gp">>>> </span><span class="n">mock_frob</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="mi">6</span><span class="p">]))</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">AssertionError: Expected</span>: <span class="n">((set([6]),), {})</span>
|
|
<span class="go">Called with: ((set([]),), {})</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>One possibility would be for mock to copy the arguments you pass in. This
|
|
could then cause problems if you do assertions that rely on object identity
|
|
for equality.</p>
|
|
<p>Here’s one solution that uses the <tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt>
|
|
functionality. If you provide a <cite>side_effect</cite> function for a mock then
|
|
<cite>side_effect</cite> will be called with the same args as the mock. This gives us an
|
|
opportunity to copy the arguments and store them for later assertions. In this
|
|
example I’m using <em>another</em> mock to store the arguments so that I can use the
|
|
mock methods for doing the assertion. Again a helper function sets this up for
|
|
me.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">deepcopy</span>
|
|
<span class="gp">>>> </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">patch</span><span class="p">,</span> <span class="n">DEFAULT</span>
|
|
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">copy_call_args</span><span class="p">(</span><span class="n">mock</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">new_mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">side_effect</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">args</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">kwargs</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">new_mock</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">DEFAULT</span>
|
|
<span class="gp">... </span> <span class="n">mock</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="n">side_effect</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">new_mock</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.frob'</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_frob</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="n">new_mock</span> <span class="o">=</span> <span class="n">copy_call_args</span><span class="p">(</span><span class="n">mock_frob</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">val</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="mi">6</span><span class="p">])</span>
|
|
<span class="gp">... </span> <span class="n">mymodule</span><span class="o">.</span><span class="n">grob</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">new_mock</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="mi">6</span><span class="p">]))</span>
|
|
<span class="gp">>>> </span><span class="n">new_mock</span><span class="o">.</span><span class="n">call_args</span>
|
|
<span class="go">call(set([6]))</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><cite>copy_call_args</cite> is called with the mock that will be called. It returns a new
|
|
mock that we do the assertion on. The <cite>side_effect</cite> function makes a copy of
|
|
the args and calls our <cite>new_mock</cite> with the copy.</p>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p>If your mock is only going to be used once there is an easier way of
|
|
checking arguments at the point they are called. You can simply do the
|
|
checking inside a <cite>side_effect</cite> function.</p>
|
|
<div class="last highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">side_effect</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">arg</span> <span class="o">==</span> <span class="nb">set</span><span class="p">([</span><span class="mi">6</span><span class="p">])</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">side_effect</span><span class="o">=</span><span class="n">side_effect</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="mi">6</span><span class="p">]))</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="nb">set</span><span class="p">())</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">AssertionError</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<p>An alternative approach is to create a subclass of <cite>Mock</cite> or <cite>MagicMock</cite> that
|
|
copies (using <a class="reference external" href="http://docs.python.org/library/copy.html#copy.deepcopy">copy.deepcopy</a>) the arguments.
|
|
Here’s an example implementation:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">deepcopy</span>
|
|
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">CopyingMock</span><span class="p">(</span><span class="n">MagicMock</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">args</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">kwargs</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">CopyingMock</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__call__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">CopyingMock</span><span class="p">(</span><span class="n">return_value</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">arg</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">c</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">arg</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="nb">set</span><span class="p">())</span>
|
|
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">AssertionError: Expected call</span>: <span class="n">mock(set([1]))</span>
|
|
<span class="go">Actual call: mock(set([]))</span>
|
|
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">foo</span>
|
|
<span class="go"><CopyingMock name='mock.foo' id='...'></span>
|
|
</pre></div>
|
|
</div>
|
|
<p>When you subclass <cite>Mock</cite> or <cite>MagicMock</cite> all dynamically created attributes,
|
|
and the <cite>return_value</cite> will use your subclass automatically. That means all
|
|
children of a <cite>CopyingMock</cite> will also have the type <cite>CopyingMock</cite>.</p>
|
|
</div>
|
|
<div class="section" id="raising-exceptions-on-attribute-access">
|
|
<h2>Raising exceptions on attribute access<a class="headerlink" href="#raising-exceptions-on-attribute-access" title="Permalink to this headline">¶</a></h2>
|
|
<p>You can use <a class="reference internal" href="mock.html#mock.PropertyMock" title="mock.PropertyMock"><tt class="xref py py-class docutils literal"><span class="pre">PropertyMock</span></tt></a> to mimic the behaviour of properties. This
|
|
includes raising exceptions when an attribute is accessed.</p>
|
|
<p>Here’s an example raising a <cite>ValueError</cite> when the ‘foo’ attribute is accessed:</p>
|
|
<div class="highlight-python"><pre>>>> m = MagicMock()
|
|
>>> p = PropertyMock(side_effect=ValueError)
|
|
>>> type(m).foo = p
|
|
>>> m.foo
|
|
Traceback (most recent call last):
|
|
....
|
|
ValueError</pre>
|
|
</div>
|
|
<p>Because every mock object has its own type, a new subclass of whichever mock
|
|
class you’re using, all mock objects are isolated from each other. You can
|
|
safely attach properties (or other descriptors or whatever you want in fact)
|
|
to <cite>type(mock)</cite> without affecting other mock objects.</p>
|
|
</div>
|
|
<div class="section" id="multiple-calls-with-different-effects">
|
|
<h2>Multiple calls with different effects<a class="headerlink" href="#multiple-calls-with-different-effects" title="Permalink to this headline">¶</a></h2>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">In mock 1.0 the handling of iterable <cite>side_effect</cite> was changed. Any
|
|
exceptions in the iterable will be raised instead of returned.</p>
|
|
</div>
|
|
<p>Handling code that needs to behave differently on subsequent calls during the
|
|
test can be tricky. For example you may have a function that needs to raise
|
|
an exception the first time it is called but returns a response on the second
|
|
call (testing retry behaviour).</p>
|
|
<p>One approach is to use a <tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt> function that replaces itself. The
|
|
first time it is called the <cite>side_effect</cite> sets a new <cite>side_effect</cite> that will
|
|
be used for the second call. It then raises an exception:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">side_effect</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">second_call</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="s">'response'</span>
|
|
<span class="gp">... </span> <span class="n">mock</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="n">second_call</span>
|
|
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">'boom'</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">side_effect</span><span class="o">=</span><span class="n">side_effect</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="s">'first'</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">Exception</span>: <span class="n">boom</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="s">'second'</span><span class="p">)</span>
|
|
<span class="go">'response'</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="s">'second'</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Another perfectly valid way would be to pop return values from a list. If the
|
|
return value is an exception, raise it instead of returning it:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">returns</span> <span class="o">=</span> <span class="p">[</span><span class="ne">Exception</span><span class="p">(</span><span class="s">'boom'</span><span class="p">),</span> <span class="s">'response'</span><span class="p">]</span>
|
|
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">side_effect</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">result</span> <span class="o">=</span> <span class="n">returns</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="ne">Exception</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">raise</span> <span class="n">result</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">result</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">side_effect</span><span class="o">=</span><span class="n">side_effect</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="s">'first'</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">Exception</span>: <span class="n">boom</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="s">'second'</span><span class="p">)</span>
|
|
<span class="go">'response'</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="s">'second'</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Which approach you prefer is a matter of taste. The first approach is actually
|
|
a line shorter but maybe the second approach is more readable.</p>
|
|
</div>
|
|
<div class="section" id="nesting-patches">
|
|
<h2>Nesting Patches<a class="headerlink" href="#nesting-patches" title="Permalink to this headline">¶</a></h2>
|
|
<p>Using patch as a context manager is nice, but if you do multiple patches you
|
|
can end up with nested with statements indenting further and further to the
|
|
right:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyTest</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">test_foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">with</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.Foo'</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_foo</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="k">with</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.Bar'</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_bar</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="k">with</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.Spam'</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_spam</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Foo</span> <span class="ow">is</span> <span class="n">mock_foo</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Bar</span> <span class="ow">is</span> <span class="n">mock_bar</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Spam</span> <span class="ow">is</span> <span class="n">mock_spam</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">original</span> <span class="o">=</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Foo</span>
|
|
<span class="gp">>>> </span><span class="n">MyTest</span><span class="p">(</span><span class="s">'test_foo'</span><span class="p">)</span><span class="o">.</span><span class="n">test_foo</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Foo</span> <span class="ow">is</span> <span class="n">original</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>With <a class="reference external" href="http://pypi.python.org/pypi/unittest2">unittest2</a> <cite>cleanup</cite> functions and the <a class="reference internal" href="patch.html#start-and-stop"><em>patch methods: start and stop</em></a> we can
|
|
achieve the same effect without the nested indentation. A simple helper
|
|
method, <cite>create_patch</cite>, puts the patch in place and returns the created mock
|
|
for us:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyTest</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">create_patch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">patcher</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">thing</span> <span class="o">=</span> <span class="n">patcher</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">addCleanup</span><span class="p">(</span><span class="n">patcher</span><span class="o">.</span><span class="n">stop</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">thing</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">test_foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">mock_foo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_patch</span><span class="p">(</span><span class="s">'mymodule.Foo'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">mock_bar</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_patch</span><span class="p">(</span><span class="s">'mymodule.Bar'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">mock_spam</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_patch</span><span class="p">(</span><span class="s">'mymodule.Spam'</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Foo</span> <span class="ow">is</span> <span class="n">mock_foo</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Bar</span> <span class="ow">is</span> <span class="n">mock_bar</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Spam</span> <span class="ow">is</span> <span class="n">mock_spam</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">original</span> <span class="o">=</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Foo</span>
|
|
<span class="gp">>>> </span><span class="n">MyTest</span><span class="p">(</span><span class="s">'test_foo'</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="n">mymodule</span><span class="o">.</span><span class="n">Foo</span> <span class="ow">is</span> <span class="n">original</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mocking-a-dictionary-with-magicmock">
|
|
<h2>Mocking a dictionary with MagicMock<a class="headerlink" href="#mocking-a-dictionary-with-magicmock" title="Permalink to this headline">¶</a></h2>
|
|
<p>You may want to mock a dictionary, or other container object, recording all
|
|
access to it whilst having it still behave like a dictionary.</p>
|
|
<p>We can do this with <a class="reference internal" href="magicmock.html#mock.MagicMock" title="mock.MagicMock"><tt class="xref py py-class docutils literal"><span class="pre">MagicMock</span></tt></a>, which will behave like a dictionary,
|
|
and using <a class="reference internal" href="mock.html#mock.Mock.side_effect" title="mock.Mock.side_effect"><tt class="xref py py-data docutils literal"><span class="pre">side_effect</span></tt></a> to delegate dictionary access to a real
|
|
underlying dictionary that is under our control.</p>
|
|
<p>When the <cite>__getitem__</cite> and <cite>__setitem__</cite> methods of our <cite>MagicMock</cite> are called
|
|
(normal dictionary access) then <cite>side_effect</cite> is called with the key (and in
|
|
the case of <cite>__setitem__</cite> the value too). We can also control what is returned.</p>
|
|
<p>After the <cite>MagicMock</cite> has been used we can use attributes like
|
|
<a class="reference internal" href="mock.html#mock.Mock.call_args_list" title="mock.Mock.call_args_list"><tt class="xref py py-data docutils literal"><span class="pre">call_args_list</span></tt></a> to assert about how the dictionary was used:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">my_dict</span> <span class="o">=</span> <span class="p">{</span><span class="s">'a'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'b'</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s">'c'</span><span class="p">:</span> <span class="mi">3</span><span class="p">}</span>
|
|
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">getitem</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">my_dict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">setitem</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">my_dict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__getitem__</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="n">getitem</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__setitem__</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="n">setitem</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p>An alternative to using <cite>MagicMock</cite> is to use <cite>Mock</cite> and <em>only</em> provide
|
|
the magic methods you specifically want:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__setitem__</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">side_effect</span><span class="o">=</span><span class="n">getitem</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__getitem__</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">side_effect</span><span class="o">=</span><span class="n">setitem</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>A <em>third</em> option is to use <cite>MagicMock</cite> but passing in <cite>dict</cite> as the <cite>spec</cite>
|
|
(or <cite>spec_set</cite>) argument so that the <cite>MagicMock</cite> created only has
|
|
dictionary magic methods available:</p>
|
|
<div class="last highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">(</span><span class="n">spec_set</span><span class="o">=</span><span class="nb">dict</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__getitem__</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="n">getitem</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__setitem__</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="n">setitem</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<p>With these side effect functions in place, the <cite>mock</cite> will behave like a normal
|
|
dictionary but recording the access. It even raises a <cite>KeyError</cite> if you try
|
|
to access a key that doesn’t exist.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span><span class="p">[</span><span class="s">'a'</span><span class="p">]</span>
|
|
<span class="go">1</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">[</span><span class="s">'c'</span><span class="p">]</span>
|
|
<span class="go">3</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">[</span><span class="s">'d'</span><span class="p">]</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">KeyError</span>: <span class="n">'d'</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">[</span><span class="s">'b'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'fish'</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">[</span><span class="s">'d'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'eggs'</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">[</span><span class="s">'b'</span><span class="p">]</span>
|
|
<span class="go">'fish'</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">[</span><span class="s">'d'</span><span class="p">]</span>
|
|
<span class="go">'eggs'</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>After it has been used you can make assertions about the access using the normal
|
|
mock methods and attributes:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__getitem__</span><span class="o">.</span><span class="n">call_args_list</span>
|
|
<span class="go">[call('a'), call('c'), call('d'), call('b'), call('d')]</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">__setitem__</span><span class="o">.</span><span class="n">call_args_list</span>
|
|
<span class="go">[call('b', 'fish'), call('d', 'eggs')]</span>
|
|
<span class="gp">>>> </span><span class="n">my_dict</span>
|
|
<span class="go">{'a': 1, 'c': 3, 'b': 'fish', 'd': 'eggs'}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mock-subclasses-and-their-attributes">
|
|
<h2>Mock subclasses and their attributes<a class="headerlink" href="#mock-subclasses-and-their-attributes" title="Permalink to this headline">¶</a></h2>
|
|
<p>There are various reasons why you might want to subclass <cite>Mock</cite>. One reason
|
|
might be to add helper methods. Here’s a silly example:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyMock</span><span class="p">(</span><span class="n">MagicMock</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">has_been_called</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">called</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span> <span class="o">=</span> <span class="n">MyMock</span><span class="p">(</span><span class="n">return_value</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span>
|
|
<span class="go"><MyMock id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span><span class="o">.</span><span class="n">has_been_called</span><span class="p">()</span>
|
|
<span class="go">False</span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span><span class="o">.</span><span class="n">has_been_called</span><span class="p">()</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The standard behaviour for <cite>Mock</cite> instances is that attributes and the return
|
|
value mocks are of the same type as the mock they are accessed on. This ensures
|
|
that <cite>Mock</cite> attributes are <cite>Mocks</cite> and <cite>MagicMock</cite> attributes are <cite>MagicMocks</cite>
|
|
<a class="footnote-reference" href="#id5" id="id4">[2]</a>. So if you’re subclassing to add helper methods then they’ll also be
|
|
available on the attributes and return value mock of instances of your
|
|
subclass.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mymock</span><span class="o">.</span><span class="n">foo</span>
|
|
<span class="go"><MyMock name='mock.foo' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">has_been_called</span><span class="p">()</span>
|
|
<span class="go">False</span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span><span class="o">.</span><span class="n">foo</span><span class="p">()</span>
|
|
<span class="go"><MyMock name='mock.foo()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">has_been_called</span><span class="p">()</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Sometimes this is inconvenient. For example, <a class="reference external" href="https://code.google.com/p/mock/issues/detail?id=105">one user</a> is subclassing mock to
|
|
created a <a class="reference external" href="http://twistedmatrix.com/documents/11.0.0/api/twisted.python.components.html">Twisted adaptor</a>.
|
|
Having this applied to attributes too actually causes errors.</p>
|
|
<p><cite>Mock</cite> (in all its flavours) uses a method called <cite>_get_child_mock</cite> to create
|
|
these “sub-mocks” for attributes and return values. You can prevent your
|
|
subclass being used for attributes by overriding this method. The signature is
|
|
that it takes arbitrary keyword arguments (<cite>**kwargs</cite>) which are then passed
|
|
onto the mock constructor:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Subclass</span><span class="p">(</span><span class="n">MagicMock</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">_get_child_mock</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">MagicMock</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span> <span class="o">=</span> <span class="n">Subclass</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mymock</span><span class="o">.</span><span class="n">foo</span>
|
|
<span class="go"><MagicMock name='mock.foo' id='...'></span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">mymock</span><span class="p">,</span> <span class="n">Subclass</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">mymock</span><span class="o">.</span><span class="n">foo</span><span class="p">,</span> <span class="n">Subclass</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">mymock</span><span class="p">(),</span> <span class="n">Subclass</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<table class="docutils footnote" frame="void" id="id5" rules="none">
|
|
<colgroup><col class="label" /><col /></colgroup>
|
|
<tbody valign="top">
|
|
<tr><td class="label"><a class="fn-backref" href="#id4">[2]</a></td><td>An exception to this rule are the non-callable mocks. Attributes use the
|
|
callable variant because otherwise non-callable mocks couldn’t have callable
|
|
methods.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div class="section" id="mocking-imports-with-patch-dict">
|
|
<h2>Mocking imports with patch.dict<a class="headerlink" href="#mocking-imports-with-patch-dict" title="Permalink to this headline">¶</a></h2>
|
|
<p>One situation where mocking can be hard is where you have a local import inside
|
|
a function. These are harder to mock because they aren’t using an object from
|
|
the module namespace that we can patch out.</p>
|
|
<p>Generally local imports are to be avoided. They are sometimes done to prevent
|
|
circular dependencies, for which there is <em>usually</em> a much better way to solve
|
|
the problem (refactor the code) or to prevent “up front costs” by delaying the
|
|
import. This can also be solved in better ways than an unconditional local
|
|
import (store the module as a class or module attribute and only do the import
|
|
on first use).</p>
|
|
<p>That aside there is a way to use <cite>mock</cite> to affect the results of an import.
|
|
Importing fetches an <em>object</em> from the <cite>sys.modules</cite> dictionary. Note that it
|
|
fetches an <em>object</em>, which need not be a module. Importing a module for the
|
|
first time results in a module object being put in <cite>sys.modules</cite>, so usually
|
|
when you import something you get a module back. This need not be the case
|
|
however.</p>
|
|
<p>This means you can use <a class="reference internal" href="patch.html#mock.patch.dict" title="mock.patch.dict"><tt class="xref py py-func docutils literal"><span class="pre">patch.dict()</span></tt></a> to <em>temporarily</em> put a mock in place
|
|
in <cite>sys.modules</cite>. Any imports whilst this patch is active will fetch the mock.
|
|
When the patch is complete (the decorated function exits, the with statement
|
|
body is complete or <cite>patcher.stop()</cite> is called) then whatever was there
|
|
previously will be restored safely.</p>
|
|
<p>Here’s an example that mocks out the ‘fooble’ module.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">dict</span><span class="p">(</span><span class="s">'sys.modules'</span><span class="p">,</span> <span class="p">{</span><span class="s">'fooble'</span><span class="p">:</span> <span class="n">mock</span><span class="p">}):</span>
|
|
<span class="gp">... </span> <span class="kn">import</span> <span class="nn">fooble</span>
|
|
<span class="gp">... </span> <span class="n">fooble</span><span class="o">.</span><span class="n">blob</span><span class="p">()</span>
|
|
<span class="gp">...</span>
|
|
<span class="go"><Mock name='mock.blob()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="s">'fooble'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">blob</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>As you can see the <cite>import fooble</cite> succeeds, but on exit there is no ‘fooble’
|
|
left in <cite>sys.modules</cite>.</p>
|
|
<p>This also works for the <cite>from module import name</cite> form:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">dict</span><span class="p">(</span><span class="s">'sys.modules'</span><span class="p">,</span> <span class="p">{</span><span class="s">'fooble'</span><span class="p">:</span> <span class="n">mock</span><span class="p">}):</span>
|
|
<span class="gp">... </span> <span class="kn">from</span> <span class="nn">fooble</span> <span class="kn">import</span> <span class="n">blob</span>
|
|
<span class="gp">... </span> <span class="n">blob</span><span class="o">.</span><span class="n">blip</span><span class="p">()</span>
|
|
<span class="gp">...</span>
|
|
<span class="go"><Mock name='mock.blob.blip()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">blob</span><span class="o">.</span><span class="n">blip</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>With slightly more work you can also mock package imports:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">modules</span> <span class="o">=</span> <span class="p">{</span><span class="s">'package'</span><span class="p">:</span> <span class="n">mock</span><span class="p">,</span> <span class="s">'package.module'</span><span class="p">:</span> <span class="n">mock</span><span class="o">.</span><span class="n">module</span><span class="p">}</span>
|
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">dict</span><span class="p">(</span><span class="s">'sys.modules'</span><span class="p">,</span> <span class="n">modules</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="kn">from</span> <span class="nn">package.module</span> <span class="kn">import</span> <span class="n">fooble</span>
|
|
<span class="gp">... </span> <span class="n">fooble</span><span class="p">()</span>
|
|
<span class="gp">...</span>
|
|
<span class="go"><Mock name='mock.module.fooble()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">fooble</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="tracking-order-of-calls-and-less-verbose-call-assertions">
|
|
<h2>Tracking order of calls and less verbose call assertions<a class="headerlink" href="#tracking-order-of-calls-and-less-verbose-call-assertions" title="Permalink to this headline">¶</a></h2>
|
|
<p>The <a class="reference internal" href="mock.html#mock.Mock" title="mock.Mock"><tt class="xref py py-class docutils literal"><span class="pre">Mock</span></tt></a> class allows you to track the <em>order</em> of method calls on
|
|
your mock objects through the <a class="reference internal" href="mock.html#mock.Mock.method_calls" title="mock.Mock.method_calls"><tt class="xref py py-attr docutils literal"><span class="pre">method_calls</span></tt></a> attribute. This
|
|
doesn’t allow you to track the order of calls between separate mock objects,
|
|
however we can use <a class="reference internal" href="mock.html#mock.Mock.mock_calls" title="mock.Mock.mock_calls"><tt class="xref py py-attr docutils literal"><span class="pre">mock_calls</span></tt></a> to achieve the same effect.</p>
|
|
<p>Because mocks track calls to child mocks in <cite>mock_calls</cite>, and accessing an
|
|
arbitrary attribute of a mock creates a child mock, we can create our separate
|
|
mocks from a parent one. Calls to those child mock will then all be recorded,
|
|
in order, in the <cite>mock_calls</cite> of the parent:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">manager</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock_foo</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">foo</span>
|
|
<span class="gp">>>> </span><span class="n">mock_bar</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">bar</span>
|
|
|
|
<span class="gp">>>> </span><span class="n">mock_foo</span><span class="o">.</span><span class="n">something</span><span class="p">()</span>
|
|
<span class="go"><Mock name='mock.foo.something()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock_bar</span><span class="o">.</span><span class="n">other</span><span class="o">.</span><span class="n">thing</span><span class="p">()</span>
|
|
<span class="go"><Mock name='mock.bar.other.thing()' id='...'></span>
|
|
|
|
<span class="gp">>>> </span><span class="n">manager</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call.foo.something(), call.bar.other.thing()]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>We can then assert about the calls, including the order, by comparing with
|
|
the <cite>mock_calls</cite> attribute on the manager mock:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">expected_calls</span> <span class="o">=</span> <span class="p">[</span><span class="n">call</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">something</span><span class="p">(),</span> <span class="n">call</span><span class="o">.</span><span class="n">bar</span><span class="o">.</span><span class="n">other</span><span class="o">.</span><span class="n">thing</span><span class="p">()]</span>
|
|
<span class="gp">>>> </span><span class="n">manager</span><span class="o">.</span><span class="n">mock_calls</span> <span class="o">==</span> <span class="n">expected_calls</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If <cite>patch</cite> is creating, and putting in place, your mocks then you can attach
|
|
them to a manager mock using the <a class="reference internal" href="mock.html#mock.Mock.attach_mock" title="mock.Mock.attach_mock"><tt class="xref py py-meth docutils literal"><span class="pre">attach_mock()</span></tt></a> method. After
|
|
attaching calls will be recorded in <cite>mock_calls</cite> of the manager.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">manager</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.Class1'</span><span class="p">)</span> <span class="k">as</span> <span class="n">MockClass1</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="k">with</span> <span class="n">patch</span><span class="p">(</span><span class="s">'mymodule.Class2'</span><span class="p">)</span> <span class="k">as</span> <span class="n">MockClass2</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="n">manager</span><span class="o">.</span><span class="n">attach_mock</span><span class="p">(</span><span class="n">MockClass1</span><span class="p">,</span> <span class="s">'MockClass1'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">manager</span><span class="o">.</span><span class="n">attach_mock</span><span class="p">(</span><span class="n">MockClass2</span><span class="p">,</span> <span class="s">'MockClass2'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">MockClass1</span><span class="p">()</span><span class="o">.</span><span class="n">foo</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="n">MockClass2</span><span class="p">()</span><span class="o">.</span><span class="n">bar</span><span class="p">()</span>
|
|
<span class="gp">...</span>
|
|
<span class="go"><MagicMock name='mock.MockClass1().foo()' id='...'></span>
|
|
<span class="go"><MagicMock name='mock.MockClass2().bar()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">manager</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call.MockClass1(),</span>
|
|
<span class="go"> call.MockClass1().foo(),</span>
|
|
<span class="go"> call.MockClass2(),</span>
|
|
<span class="go"> call.MockClass2().bar()]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If many calls have been made, but you’re only interested in a particular
|
|
sequence of them then an alternative is to use the
|
|
<a class="reference internal" href="mock.html#mock.Mock.assert_has_calls" title="mock.Mock.assert_has_calls"><tt class="xref py py-meth docutils literal"><span class="pre">assert_has_calls()</span></tt></a> method. This takes a list of calls (constructed
|
|
with the <a class="reference internal" href="helpers.html#mock.call" title="mock.call"><tt class="xref py py-data docutils literal"><span class="pre">call</span></tt></a> object). If that sequence of calls are in
|
|
<a class="reference internal" href="mock.html#mock.Mock.mock_calls" title="mock.Mock.mock_calls"><tt class="xref py py-attr docutils literal"><span class="pre">mock_calls</span></tt></a> then the assert succeeds.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span><span class="o">.</span><span class="n">foo</span><span class="p">()</span><span class="o">.</span><span class="n">bar</span><span class="p">()</span><span class="o">.</span><span class="n">baz</span><span class="p">()</span>
|
|
<span class="go"><MagicMock name='mock().foo().bar().baz()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">one</span><span class="p">()</span><span class="o">.</span><span class="n">two</span><span class="p">()</span><span class="o">.</span><span class="n">three</span><span class="p">()</span>
|
|
<span class="go"><MagicMock name='mock.one().two().three()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">calls</span> <span class="o">=</span> <span class="n">call</span><span class="o">.</span><span class="n">one</span><span class="p">()</span><span class="o">.</span><span class="n">two</span><span class="p">()</span><span class="o">.</span><span class="n">three</span><span class="p">()</span><span class="o">.</span><span class="n">call_list</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">assert_has_calls</span><span class="p">(</span><span class="n">calls</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Even though the chained call <cite>m.one().two().three()</cite> aren’t the only calls that
|
|
have been made to the mock, the assert still succeeds.</p>
|
|
<p>Sometimes a mock may have several calls made to it, and you are only interested
|
|
in asserting about <em>some</em> of those calls. You may not even care about the
|
|
order. In this case you can pass <cite>any_order=True</cite> to <cite>assert_has_calls</cite>:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">m</span><span class="o">.</span><span class="n">two</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="n">m</span><span class="o">.</span><span class="n">seven</span><span class="p">(</span><span class="mi">7</span><span class="p">),</span> <span class="n">m</span><span class="o">.</span><span class="n">fifty</span><span class="p">(</span><span class="s">'50'</span><span class="p">)</span>
|
|
<span class="go">(...)</span>
|
|
<span class="gp">>>> </span><span class="n">calls</span> <span class="o">=</span> <span class="p">[</span><span class="n">call</span><span class="o">.</span><span class="n">fifty</span><span class="p">(</span><span class="s">'50'</span><span class="p">),</span> <span class="n">call</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">call</span><span class="o">.</span><span class="n">seven</span><span class="p">(</span><span class="mi">7</span><span class="p">)]</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">assert_has_calls</span><span class="p">(</span><span class="n">calls</span><span class="p">,</span> <span class="n">any_order</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="more-complex-argument-matching">
|
|
<h2>More complex argument matching<a class="headerlink" href="#more-complex-argument-matching" title="Permalink to this headline">¶</a></h2>
|
|
<p>Using the same basic concept as <cite>ANY</cite> we can implement matchers to do more
|
|
complex assertions on objects used as arguments to mocks.</p>
|
|
<p>Suppose we expect some object to be passed to a mock that by default
|
|
compares equal based on object identity (which is the Python default for user
|
|
defined classes). To use <a class="reference internal" href="mock.html#mock.Mock.assert_called_with" title="mock.Mock.assert_called_with"><tt class="xref py py-meth docutils literal"><span class="pre">assert_called_with()</span></tt></a> we would need to pass
|
|
in the exact same object. If we are only interested in some of the attributes
|
|
of this object then we can create a matcher that will check these attributes
|
|
for us.</p>
|
|
<p>You can see in this example how a ‘standard’ call to <cite>assert_called_with</cite> isn’t
|
|
sufficient:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">return_value</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="n">Foo</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="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="n">Foo</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="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">AssertionError: Expected</span>: <span class="n">call(<__main__.Foo object at 0x...>)</span>
|
|
<span class="go">Actual call: call(<__main__.Foo object at 0x...>)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>A comparison function for our <cite>Foo</cite> class might look something like this:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">compare</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="n">other</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="bp">False</span>
|
|
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">!=</span> <span class="n">other</span><span class="o">.</span><span class="n">a</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="bp">False</span>
|
|
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">b</span> <span class="o">!=</span> <span class="n">other</span><span class="o">.</span><span class="n">b</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="bp">False</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="bp">True</span>
|
|
<span class="gp">...</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>And a matcher object that can use comparison functions like this for its
|
|
equality operation would look something like this:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Matcher</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">compare</span><span class="p">,</span> <span class="n">some_obj</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">compare</span> <span class="o">=</span> <span class="n">compare</span>
|
|
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">some_obj</span> <span class="o">=</span> <span class="n">some_obj</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">some_obj</span><span class="p">,</span> <span class="n">other</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Putting all this together:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">match_foo</span> <span class="o">=</span> <span class="n">Matcher</span><span class="p">(</span><span class="n">compare</span><span class="p">,</span> <span class="n">Foo</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="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="n">match_foo</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The <cite>Matcher</cite> is instantiated with our compare function and the <cite>Foo</cite> object
|
|
we want to compare against. In <cite>assert_called_with</cite> the <cite>Matcher</cite> equality
|
|
method will be called, which compares the object the mock was called with
|
|
against the one we created our matcher with. If they match then
|
|
<cite>assert_called_with</cite> passes, and if they don’t an <cite>AssertionError</cite> is raised:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">match_wrong</span> <span class="o">=</span> <span class="n">Matcher</span><span class="p">(</span><span class="n">compare</span><span class="p">,</span> <span class="n">Foo</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="n">match_wrong</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">AssertionError: Expected</span>: <span class="n">((<Matcher object at 0x...>,), {})</span>
|
|
<span class="go">Called with: ((<Foo object at 0x...>,), {})</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>With a bit of tweaking you could have the comparison function raise the
|
|
<cite>AssertionError</cite> directly and provide a more useful failure message.</p>
|
|
<p>As of version 1.5, the Python testing library <a class="reference external" href="http://pypi.python.org/pypi/PyHamcrest">PyHamcrest</a> provides similar functionality,
|
|
that may be useful here, in the form of its equality matcher
|
|
(<a class="reference external" href="http://packages.python.org/PyHamcrest/integration.html#hamcrest.library.integration.match_equality">hamcrest.library.integration.match_equality</a>).</p>
|
|
</div>
|
|
<div class="section" id="less-verbose-configuration-of-mock-objects">
|
|
<h2>Less verbose configuration of mock objects<a class="headerlink" href="#less-verbose-configuration-of-mock-objects" title="Permalink to this headline">¶</a></h2>
|
|
<p>This recipe, for easier configuration of mock objects, is now part of <cite>Mock</cite>.
|
|
See the <a class="reference internal" href="mock.html#mock.Mock.configure_mock" title="mock.Mock.configure_mock"><tt class="xref py py-meth docutils literal"><span class="pre">configure_mock()</span></tt></a> method.</p>
|
|
</div>
|
|
<div class="section" id="matching-any-argument-in-assertions">
|
|
<h2>Matching any argument in assertions<a class="headerlink" href="#matching-any-argument-in-assertions" title="Permalink to this headline">¶</a></h2>
|
|
<p>This example is now built in to mock. See <a class="reference internal" href="helpers.html#mock.ANY" title="mock.ANY"><tt class="xref py py-data docutils literal"><span class="pre">ANY</span></tt></a>.</p>
|
|
</div>
|
|
<div class="section" id="mocking-properties">
|
|
<h2>Mocking Properties<a class="headerlink" href="#mocking-properties" title="Permalink to this headline">¶</a></h2>
|
|
<p>This example is now built in to mock. See <a class="reference internal" href="mock.html#mock.PropertyMock" title="mock.PropertyMock"><tt class="xref py py-class docutils literal"><span class="pre">PropertyMock</span></tt></a>.</p>
|
|
</div>
|
|
<div class="section" id="mocking-open">
|
|
<h2>Mocking open<a class="headerlink" href="#mocking-open" title="Permalink to this headline">¶</a></h2>
|
|
<p>This example is now built in to mock. See <a class="reference internal" href="helpers.html#mock.mock_open" title="mock.mock_open"><tt class="xref py py-func docutils literal"><span class="pre">mock_open()</span></tt></a>.</p>
|
|
</div>
|
|
<div class="section" id="mocks-without-some-attributes">
|
|
<h2>Mocks without some attributes<a class="headerlink" href="#mocks-without-some-attributes" title="Permalink to this headline">¶</a></h2>
|
|
<p>This example is now built in to mock. See <a class="reference internal" href="mock.html#deleting-attributes"><em>Deleting Attributes</em></a>.</p>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar">
|
|
<div class="sphinxsidebarwrapper">
|
|
<h3><a href="index.html">Table Of Contents</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#">Further Examples</a><ul>
|
|
<li><a class="reference internal" href="#mocking-chained-calls">Mocking chained calls</a></li>
|
|
<li><a class="reference internal" href="#partial-mocking">Partial mocking</a></li>
|
|
<li><a class="reference internal" href="#mocking-a-generator-method">Mocking a Generator Method</a></li>
|
|
<li><a class="reference internal" href="#applying-the-same-patch-to-every-test-method">Applying the same patch to every test method</a></li>
|
|
<li><a class="reference internal" href="#mocking-unbound-methods">Mocking Unbound Methods</a></li>
|
|
<li><a class="reference internal" href="#checking-multiple-calls-with-mock">Checking multiple calls with mock</a></li>
|
|
<li><a class="reference internal" href="#coping-with-mutable-arguments">Coping with mutable arguments</a></li>
|
|
<li><a class="reference internal" href="#raising-exceptions-on-attribute-access">Raising exceptions on attribute access</a></li>
|
|
<li><a class="reference internal" href="#multiple-calls-with-different-effects">Multiple calls with different effects</a></li>
|
|
<li><a class="reference internal" href="#nesting-patches">Nesting Patches</a></li>
|
|
<li><a class="reference internal" href="#mocking-a-dictionary-with-magicmock">Mocking a dictionary with MagicMock</a></li>
|
|
<li><a class="reference internal" href="#mock-subclasses-and-their-attributes">Mock subclasses and their attributes</a></li>
|
|
<li><a class="reference internal" href="#mocking-imports-with-patch-dict">Mocking imports with patch.dict</a></li>
|
|
<li><a class="reference internal" href="#tracking-order-of-calls-and-less-verbose-call-assertions">Tracking order of calls and less verbose call assertions</a></li>
|
|
<li><a class="reference internal" href="#more-complex-argument-matching">More complex argument matching</a></li>
|
|
<li><a class="reference internal" href="#less-verbose-configuration-of-mock-objects">Less verbose configuration of mock objects</a></li>
|
|
<li><a class="reference internal" href="#matching-any-argument-in-assertions">Matching any argument in assertions</a></li>
|
|
<li><a class="reference internal" href="#mocking-properties">Mocking Properties</a></li>
|
|
<li><a class="reference internal" href="#mocking-open">Mocking open</a></li>
|
|
<li><a class="reference internal" href="#mocks-without-some-attributes">Mocks without some attributes</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="getting-started.html"
|
|
title="previous chapter">Getting Started with Mock</a></p>
|
|
<h4>Next topic</h4>
|
|
<p class="topless"><a href="compare.html"
|
|
title="next chapter">Mock Library Comparison</a></p>
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="_sources/examples.txt"
|
|
rel="nofollow">Show Source</a></li>
|
|
</ul>
|
|
<div id="searchbox" style="display: none">
|
|
<h3>Quick search</h3>
|
|
<form class="search" action="search.html" method="get">
|
|
<input type="text" name="q" />
|
|
<input type="submit" value="Go" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
<p class="searchtip" style="font-size: 90%">
|
|
Enter search terms or a module, class or function name.
|
|
</p>
|
|
</div>
|
|
<script type="text/javascript">$('#searchbox').show(0);</script>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related">
|
|
<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="compare.html" title="Mock Library Comparison"
|
|
>next</a> |</li>
|
|
<li class="right" >
|
|
<a href="getting-started.html" title="Getting Started with Mock"
|
|
>previous</a> |</li>
|
|
<li><a href="index.html">Mock 1.0.0 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© Copyright 2007-2012, Michael Foord & the mock team.
|
|
Last updated on Oct 07, 2012.
|
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
|
|
</div>
|
|
</body>
|
|
</html> |