mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-08 20:47:44 +00:00
510 lines
46 KiB
HTML
510 lines
46 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>Getting Started with Mock — 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="Further Examples" href="examples.html" />
|
|
<link rel="prev" title="Mocking Magic Methods" href="magicmock.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="examples.html" title="Further Examples"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="magicmock.html" title="Mocking Magic Methods"
|
|
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="getting-started-with-mock">
|
|
<h1>Getting Started with Mock<a class="headerlink" href="#getting-started-with-mock" title="Permalink to this headline">¶</a></h1>
|
|
<span class="target" id="getting-started"></span><span class="target" id="index-0"></span><div class="section" id="using-mock">
|
|
<h2>Using Mock<a class="headerlink" href="#using-mock" title="Permalink to this headline">¶</a></h2>
|
|
<div class="section" id="mock-patching-methods">
|
|
<h3>Mock Patching Methods<a class="headerlink" href="#mock-patching-methods" title="Permalink to this headline">¶</a></h3>
|
|
<p>Common uses for <tt class="xref py py-class docutils literal"><span class="pre">Mock</span></tt> objects include:</p>
|
|
<ul class="simple">
|
|
<li>Patching methods</li>
|
|
<li>Recording method calls on objects</li>
|
|
</ul>
|
|
<p>You might want to replace a method on an object to check that
|
|
it is called with the correct arguments by another part of the system:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">real</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">real</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">'method'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">real</span><span class="o">.</span><span class="n">method</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="mi">5</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="s">'value'</span><span class="p">)</span>
|
|
<span class="go"><MagicMock name='method()' id='...'></span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Once our mock has been used (<cite>real.method</cite> in this example) it has methods
|
|
and attributes that allow you to make assertions about how it has been used.</p>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">In most of these examples the <tt class="xref py py-class docutils literal"><span class="pre">Mock</span></tt> and <tt class="xref py py-class docutils literal"><span class="pre">MagicMock</span></tt> classes
|
|
are interchangeable. As the <cite>MagicMock</cite> is the more capable class it makes
|
|
a sensible one to use by default.</p>
|
|
</div>
|
|
<p>Once the mock has been called its <tt class="xref py py-attr docutils literal"><span class="pre">called</span></tt> attribute is set to
|
|
<cite>True</cite>. More importantly we can use the <tt class="xref py py-meth docutils literal"><span class="pre">assert_called_with()</span></tt> or
|
|
<tt class="xref py py-meth docutils literal"><span class="pre">assert_called_once_with()</span></tt> method to check that it was called with
|
|
the correct arguments.</p>
|
|
<p>This example tests that calling <cite>ProductionClass().method</cite> results in a call to
|
|
the <cite>something</cite> method:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">mock</span> <span class="kn">import</span> <span class="n">MagicMock</span>
|
|
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">ProductionClass</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">method</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">something</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">def</span> <span class="nf">something</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="n">c</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">pass</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">real</span> <span class="o">=</span> <span class="n">ProductionClass</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">real</span><span class="o">.</span><span class="n">something</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">real</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">real</span><span class="o">.</span><span class="n">something</span><span class="o">.</span><span class="n">assert_called_once_with</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>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mock-for-method-calls-on-an-object">
|
|
<h3>Mock for Method Calls on an Object<a class="headerlink" href="#mock-for-method-calls-on-an-object" title="Permalink to this headline">¶</a></h3>
|
|
<p>In the last example we patched a method directly on an object to check that it
|
|
was called correctly. Another common use case is to pass an object into a
|
|
method (or some part of the system under test) and then check that it is used
|
|
in the correct way.</p>
|
|
<p>The simple <cite>ProductionClass</cite> below has a <cite>closer</cite> method. If it is called with
|
|
an object then it calls <cite>close</cite> on it.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">ProductionClass</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">closer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">something</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">something</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
|
<span class="gp">...</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>So to test it we need to pass in an object with a <cite>close</cite> method and check
|
|
that it was called correctly.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">real</span> <span class="o">=</span> <span class="n">ProductionClass</span><span class="p">()</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="gp">>>> </span><span class="n">real</span><span class="o">.</span><span class="n">closer</span><span class="p">(</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">close</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>We don’t have to do any work to provide the ‘close’ method on our mock.
|
|
Accessing close creates it. So, if ‘close’ hasn’t already been called then
|
|
accessing it in the test will create it, but <tt class="xref py py-meth docutils literal"><span class="pre">assert_called_with()</span></tt>
|
|
will raise a failure exception.</p>
|
|
</div>
|
|
<div class="section" id="mocking-classes">
|
|
<h3>Mocking Classes<a class="headerlink" href="#mocking-classes" title="Permalink to this headline">¶</a></h3>
|
|
<p>A common use case is to mock out classes instantiated by your code under test.
|
|
When you patch a class, then that class is replaced with a mock. Instances
|
|
are created by <em>calling the class</em>. This means you access the “mock instance”
|
|
by looking at the return value of the mocked class.</p>
|
|
<p>In the example below we have a function <cite>some_function</cite> that instantiates <cite>Foo</cite>
|
|
and calls a method on it. The call to <cite>patch</cite> replaces the class <cite>Foo</cite> with a
|
|
mock. The <cite>Foo</cite> instance is the result of calling the mock, so it is configured
|
|
by modifying the mock <tt class="xref py py-attr docutils literal"><span class="pre">return_value</span></tt>.</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">some_function</span><span class="p">():</span>
|
|
<span class="gp">... </span> <span class="n">instance</span> <span class="o">=</span> <span class="n">module</span><span class="o">.</span><span class="n">Foo</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="p">()</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">'module.Foo'</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="n">instance</span> <span class="o">=</span> <span class="n">mock</span><span class="o">.</span><span class="n">return_value</span>
|
|
<span class="gp">... </span> <span class="n">instance</span><span class="o">.</span><span class="n">method</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s">'the result'</span>
|
|
<span class="gp">... </span> <span class="n">result</span> <span class="o">=</span> <span class="n">some_function</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">result</span> <span class="o">==</span> <span class="s">'the result'</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="naming-your-mocks">
|
|
<h3>Naming your mocks<a class="headerlink" href="#naming-your-mocks" title="Permalink to this headline">¶</a></h3>
|
|
<p>It can be useful to give your mocks a name. The name is shown in the repr of
|
|
the mock and can be helpful when the mock appears in test failure messages. The
|
|
name is also propagated to attributes or methods of the 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">MagicMock</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">'foo'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span>
|
|
<span class="go"><MagicMock name='foo' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">method</span>
|
|
<span class="go"><MagicMock name='foo.method' id='...'></span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="tracking-all-calls">
|
|
<h3>Tracking all Calls<a class="headerlink" href="#tracking-all-calls" title="Permalink to this headline">¶</a></h3>
|
|
<p>Often you want to track more than a single call to a method. The
|
|
<tt class="xref py py-attr docutils literal"><span class="pre">mock_calls</span></tt> attribute records all calls
|
|
to child attributes of the mock - and also to their children.</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">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
|
|
<span class="go"><MagicMock name='mock.method()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">attribute</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">53</span><span class="p">)</span>
|
|
<span class="go"><MagicMock name='mock.attribute.method()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call.method(), call.attribute.method(10, x=53)]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you make an assertion about <cite>mock_calls</cite> and any unexpected methods
|
|
have been called, then the assertion will fail. This is useful because as well
|
|
as asserting that the calls you expected have been made, you are also checking
|
|
that they were made in the right order and with no additional calls:</p>
|
|
<p>You use the <tt class="xref py py-data docutils literal"><span class="pre">call</span></tt> object to construct lists for comparing with
|
|
<cite>mock_calls</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="o">.</span><span class="n">method</span><span class="p">(),</span> <span class="n">call</span><span class="o">.</span><span class="n">attribute</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">53</span><span class="p">)]</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">mock_calls</span> <span class="o">==</span> <span class="n">expected</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="setting-return-values-and-attributes">
|
|
<h3>Setting Return Values and Attributes<a class="headerlink" href="#setting-return-values-and-attributes" title="Permalink to this headline">¶</a></h3>
|
|
<p>Setting the return values on a mock object is trivially easy:</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">return_value</span> <span class="o">=</span> <span class="mi">3</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">()</span>
|
|
<span class="go">3</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Of course you can do the same for methods on the 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="o">.</span><span class="n">method</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="mi">3</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
|
|
<span class="go">3</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The return value can also be set in the constructor:</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="mi">3</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">()</span>
|
|
<span class="go">3</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you need an attribute setting on your mock, just do it:</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">x</span> <span class="o">=</span> <span class="mi">3</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">x</span>
|
|
<span class="go">3</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Sometimes you want to mock up a more complex situation, like for example
|
|
<cite>mock.connection.cursor().execute(“SELECT 1”)</cite>. If we wanted this call to
|
|
return a list, then we have to configure the result of the nested call.</p>
|
|
<p>We can use <tt class="xref py py-data docutils literal"><span class="pre">call</span></tt> to construct the set of calls in a “chained call” like
|
|
this for easy assertion afterwards:</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">cursor</span> <span class="o">=</span> <span class="n">mock</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="o">.</span><span class="n">return_value</span>
|
|
<span class="gp">>>> </span><span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="p">[</span><span class="s">'foo'</span><span class="p">]</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"SELECT 1"</span><span class="p">)</span>
|
|
<span class="go">['foo']</span>
|
|
<span class="gp">>>> </span><span class="n">expected</span> <span class="o">=</span> <span class="n">call</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"SELECT 1"</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">mock</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call.connection.cursor(), call.connection.cursor().execute('SELECT 1')]</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">mock_calls</span> <span class="o">==</span> <span class="n">expected</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>It is the call to <cite>.call_list()</cite> that turns our call object into a list of
|
|
calls representing the chained calls.</p>
|
|
</div>
|
|
<div class="section" id="raising-exceptions-with-mocks">
|
|
<h3>Raising exceptions with mocks<a class="headerlink" href="#raising-exceptions-with-mocks" title="Permalink to this headline">¶</a></h3>
|
|
<p>A useful attribute is <tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt>. If you set this to an
|
|
exception class or instance then the exception will be raised when the mock
|
|
is called.</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">side_effect</span><span class="o">=</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="n">mock</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">Exception</span>: <span class="n">Boom!</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="side-effect-functions-and-iterables">
|
|
<h3>Side effect functions and iterables<a class="headerlink" href="#side-effect-functions-and-iterables" title="Permalink to this headline">¶</a></h3>
|
|
<p><cite>side_effect</cite> can also be set to a function or an iterable. The use case for
|
|
<cite>side_effect</cite> as an iterable is where your mock is going to be called several
|
|
times, and you want each call to return a different value. When you set
|
|
<cite>side_effect</cite> to an iterable every call to the mock returns the next value
|
|
from the iterable:</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">MagicMock</span><span class="p">(</span><span class="n">side_effect</span><span class="o">=</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="go">4</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">()</span>
|
|
<span class="go">5</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">()</span>
|
|
<span class="go">6</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>For more advanced use cases, like dynamically varying the return values
|
|
depending on what the mock is called with, <cite>side_effect</cite> can be a function.
|
|
The function will be called with the same arguments as the mock. Whatever the
|
|
function returns is what the call returns:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">vals</span> <span class="o">=</span> <span class="p">{(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span> <span class="mi">1</span><span class="p">,</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="mi">2</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="k">return</span> <span class="n">vals</span><span class="p">[</span><span class="n">args</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">MagicMock</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="mi">1</span><span class="p">,</span> <span class="mi">2</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="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
<span class="go">2</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="creating-a-mock-from-an-existing-object">
|
|
<h3>Creating a Mock from an Existing Object<a class="headerlink" href="#creating-a-mock-from-an-existing-object" title="Permalink to this headline">¶</a></h3>
|
|
<p>One problem with over use of mocking is that it couples your tests to the
|
|
implementation of your mocks rather than your real code. Suppose you have a
|
|
class that implements <cite>some_method</cite>. In a test for another class, you
|
|
provide a mock of this object that <em>also</em> provides <cite>some_method</cite>. If later
|
|
you refactor the first class, so that it no longer has <cite>some_method</cite> - then
|
|
your tests will continue to pass even though your code is now broken!</p>
|
|
<p><cite>Mock</cite> allows you to provide an object as a specification for the mock,
|
|
using the <cite>spec</cite> keyword argument. Accessing methods / attributes on the
|
|
mock that don’t exist on your specification object will immediately raise an
|
|
attribute error. If you change the implementation of your specification, then
|
|
tests that use that class will start failing immediately without you having to
|
|
instantiate the class in those tests.</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">spec</span><span class="o">=</span><span class="n">SomeClass</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">old_method</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">AttributeError</span>: <span class="n">object has no attribute 'old_method'</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you want a stronger form of specification that prevents the setting
|
|
of arbitrary attributes as well as the getting of them then you can use
|
|
<cite>spec_set</cite> instead of <cite>spec</cite>.</p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="patch-decorators">
|
|
<h2>Patch Decorators<a class="headerlink" href="#patch-decorators" title="Permalink to this headline">¶</a></h2>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">With <cite>patch</cite> it matters that you patch objects in the namespace where they
|
|
are looked up. This is normally straightforward, but for a quick guide
|
|
read <a class="reference internal" href="patch.html#where-to-patch"><em>where to patch</em></a>.</p>
|
|
</div>
|
|
<p>A common need in tests is to patch a class attribute or a module attribute,
|
|
for example patching a builtin or patching a class in a module to test that it
|
|
is instantiated. Modules and classes are effectively global, so patching on
|
|
them has to be undone after the test or the patch will persist into other
|
|
tests and cause hard to diagnose problems.</p>
|
|
<p>mock provides three convenient decorators for this: <cite>patch</cite>, <cite>patch.object</cite> and
|
|
<cite>patch.dict</cite>. <cite>patch</cite> takes a single string, of the form
|
|
<cite>package.module.Class.attribute</cite> to specify the attribute you are patching. It
|
|
also optionally takes a value that you want the attribute (or class or
|
|
whatever) to be replaced with. ‘patch.object’ takes an object and the name of
|
|
the attribute you would like patched, plus optionally the value to patch it
|
|
with.</p>
|
|
<p><cite>patch.object</cite>:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">original</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="o">.</span><span class="n">attribute</span>
|
|
<span class="gp">>>> </span><span class="nd">@patch.object</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="s">'attribute'</span><span class="p">,</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">attribute</span><span class="p">)</span>
|
|
<span class="gp">... </span><span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">SomeClass</span><span class="o">.</span><span class="n">attribute</span> <span class="o">==</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">attribute</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">test</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="n">SomeClass</span><span class="o">.</span><span class="n">attribute</span> <span class="o">==</span> <span class="n">original</span>
|
|
|
|
<span class="gp">>>> </span><span class="nd">@patch</span><span class="p">(</span><span class="s">'package.module.attribute'</span><span class="p">,</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">attribute</span><span class="p">)</span>
|
|
<span class="gp">... </span><span class="k">def</span> <span class="nf">test</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">attribute</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">attribute</span> <span class="ow">is</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">attribute</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">test</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you are patching a module (including <cite>__builtin__</cite>) then use <cite>patch</cite>
|
|
instead of <cite>patch.object</cite>:</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">MagicMock</span><span class="p">(</span><span class="n">return_value</span> <span class="o">=</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">file_handle</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">'__builtin__.open'</span><span class="p">,</span> <span class="n">mock</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">handle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">'filename'</span><span class="p">,</span> <span class="s">'r'</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">assert_called_with</span><span class="p">(</span><span class="s">'filename'</span><span class="p">,</span> <span class="s">'r'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="n">handle</span> <span class="o">==</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">file_handle</span><span class="p">,</span> <span class="s">"incorrect file handle returned"</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The module name can be ‘dotted’, in the form <cite>package.module</cite> if needed:</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">'package.module.ClassName.attribute'</span><span class="p">,</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">attribute</span><span class="p">)</span>
|
|
<span class="gp">... </span><span class="k">def</span> <span class="nf">test</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">ClassName</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">ClassName</span><span class="o">.</span><span class="n">attribute</span> <span class="o">==</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">attribute</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">test</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>A nice pattern is to actually decorate test methods themselves:</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">unittest2</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="nd">@patch.object</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="s">'attribute'</span><span class="p">,</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">attribute</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">test_something</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">assertEqual</span><span class="p">(</span><span class="n">SomeClass</span><span class="o">.</span><span class="n">attribute</span><span class="p">,</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">attribute</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">original</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="o">.</span><span class="n">attribute</span>
|
|
<span class="gp">>>> </span><span class="n">MyTest</span><span class="p">(</span><span class="s">'test_something'</span><span class="p">)</span><span class="o">.</span><span class="n">test_something</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="n">SomeClass</span><span class="o">.</span><span class="n">attribute</span> <span class="o">==</span> <span class="n">original</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you want to patch with a Mock, you can use <cite>patch</cite> with only one argument
|
|
(or <cite>patch.object</cite> with two arguments). The mock will be created for you and
|
|
passed into the test function / method:</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">unittest2</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="nd">@patch.object</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="s">'static_method'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">test_something</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mock_method</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="n">SomeClass</span><span class="o">.</span><span class="n">static_method</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="n">mock_method</span><span class="o">.</span><span class="n">assert_called_with</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_something'</span><span class="p">)</span><span class="o">.</span><span class="n">test_something</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>You can stack up multiple patch decorators using this pattern:</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">unittest2</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="nd">@patch</span><span class="p">(</span><span class="s">'package.module.ClassName1'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="nd">@patch</span><span class="p">(</span><span class="s">'package.module.ClassName2'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">test_something</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">MockClass2</span><span class="p">,</span> <span class="n">MockClass1</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">package</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">ClassName1</span> <span class="ow">is</span> <span class="n">MockClass1</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">package</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">ClassName2</span> <span class="ow">is</span> <span class="n">MockClass2</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_something'</span><span class="p">)</span><span class="o">.</span><span class="n">test_something</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>When you nest patch decorators the mocks are passed in to the decorated
|
|
function in the same order they applied (the normal <em>python</em> order that
|
|
decorators are applied). This means from the bottom up, so in the example
|
|
above the mock for <cite>test_module.ClassName2</cite> is passed in first.</p>
|
|
<p>There is also <tt class="xref py py-func docutils literal"><span class="pre">patch.dict()</span></tt> for setting values in a dictionary just
|
|
during a scope and restoring the dictionary to its original state when the test
|
|
ends:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">foo</span> <span class="o">=</span> <span class="p">{</span><span class="s">'key'</span><span class="p">:</span> <span class="s">'value'</span><span class="p">}</span>
|
|
<span class="gp">>>> </span><span class="n">original</span> <span class="o">=</span> <span class="n">foo</span><span class="o">.</span><span class="n">copy</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="n">foo</span><span class="p">,</span> <span class="p">{</span><span class="s">'newkey'</span><span class="p">:</span> <span class="s">'newvalue'</span><span class="p">},</span> <span class="n">clear</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">assert</span> <span class="n">foo</span> <span class="o">==</span> <span class="p">{</span><span class="s">'newkey'</span><span class="p">:</span> <span class="s">'newvalue'</span><span class="p">}</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="k">assert</span> <span class="n">foo</span> <span class="o">==</span> <span class="n">original</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><cite>patch</cite>, <cite>patch.object</cite> and <cite>patch.dict</cite> can all be used as context managers.</p>
|
|
<p>Where you use <cite>patch</cite> to create a mock for you, you can get a reference to the
|
|
mock using the “as” form of the with statement:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">ProductionClass</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">method</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">ProductionClass</span><span class="p">,</span> <span class="s">'method'</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_method</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="n">mock_method</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">real</span> <span class="o">=</span> <span class="n">ProductionClass</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="n">real</span><span class="o">.</span><span class="n">method</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="gp">>>> </span><span class="n">mock_method</span><span class="o">.</span><span class="n">assert_called_with</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>
|
|
</pre></div>
|
|
</div>
|
|
<p>As an alternative <cite>patch</cite>, <cite>patch.object</cite> and <cite>patch.dict</cite> can be used as
|
|
class decorators. When used in this way it is the same as applying the
|
|
decorator indvidually to every method whose name starts with “test”.</p>
|
|
<p>For some more advanced examples, see the <a class="reference internal" href="examples.html#further-examples"><em>Further Examples</em></a> page.</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="#">Getting Started with Mock</a><ul>
|
|
<li><a class="reference internal" href="#using-mock">Using Mock</a><ul>
|
|
<li><a class="reference internal" href="#mock-patching-methods">Mock Patching Methods</a></li>
|
|
<li><a class="reference internal" href="#mock-for-method-calls-on-an-object">Mock for Method Calls on an Object</a></li>
|
|
<li><a class="reference internal" href="#mocking-classes">Mocking Classes</a></li>
|
|
<li><a class="reference internal" href="#naming-your-mocks">Naming your mocks</a></li>
|
|
<li><a class="reference internal" href="#tracking-all-calls">Tracking all Calls</a></li>
|
|
<li><a class="reference internal" href="#setting-return-values-and-attributes">Setting Return Values and Attributes</a></li>
|
|
<li><a class="reference internal" href="#raising-exceptions-with-mocks">Raising exceptions with mocks</a></li>
|
|
<li><a class="reference internal" href="#side-effect-functions-and-iterables">Side effect functions and iterables</a></li>
|
|
<li><a class="reference internal" href="#creating-a-mock-from-an-existing-object">Creating a Mock from an Existing Object</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="reference internal" href="#patch-decorators">Patch Decorators</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="magicmock.html"
|
|
title="previous chapter">Mocking Magic Methods</a></p>
|
|
<h4>Next topic</h4>
|
|
<p class="topless"><a href="examples.html"
|
|
title="next chapter">Further Examples</a></p>
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="_sources/getting-started.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="examples.html" title="Further Examples"
|
|
>next</a> |</li>
|
|
<li class="right" >
|
|
<a href="magicmock.html" title="Mocking Magic Methods"
|
|
>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> |