зеркало из https://github.com/mozilla/gecko-dev.git
875 строки
79 KiB
HTML
875 строки
79 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>The Mock Class — 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="Patch Decorators" href="patch.html" />
|
|
<link rel="prev" title="Mock - Mocking and Testing Library" href="index.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="patch.html" title="Patch Decorators"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="index.html" title="Mock - Mocking and Testing Library"
|
|
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="the-mock-class">
|
|
<h1>The Mock Class<a class="headerlink" href="#the-mock-class" title="Permalink to this headline">¶</a></h1>
|
|
<p><cite>Mock</cite> is a flexible mock object intended to replace the use of stubs and
|
|
test doubles throughout your code. Mocks are callable and create attributes as
|
|
new mocks when you access them <a class="footnote-reference" href="#id3" id="id1">[1]</a>. Accessing the same attribute will always
|
|
return the same mock. Mocks record how you use them, allowing you to make
|
|
assertions about what your code has done to them.</p>
|
|
<p><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> is a subclass of <cite>Mock</cite> with all the magic methods
|
|
pre-created and ready to use. There are also non-callable variants, useful
|
|
when you are mocking out objects that aren’t callable:
|
|
<a class="reference internal" href="#mock.NonCallableMock" title="mock.NonCallableMock"><tt class="xref py py-class docutils literal"><span class="pre">NonCallableMock</span></tt></a> and <a class="reference internal" href="magicmock.html#mock.NonCallableMagicMock" title="mock.NonCallableMagicMock"><tt class="xref py py-class docutils literal"><span class="pre">NonCallableMagicMock</span></tt></a></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></tt></a> decorators makes it easy to temporarily replace classes
|
|
in a particular module with a <cite>Mock</cite> object. By default <cite>patch</cite> will create
|
|
a <cite>MagicMock</cite> for you. You can specify an alternative class of <cite>Mock</cite> using
|
|
the <cite>new_callable</cite> argument to <cite>patch</cite>.</p>
|
|
<span class="target" id="index-0"></span><span class="target" id="index-1"></span><span class="target" id="index-2"></span><span class="target" id="index-3"></span><span class="target" id="index-4"></span><dl class="class">
|
|
<dt id="mock.Mock">
|
|
<em class="property">class </em><tt class="descname">Mock</tt><big>(</big><em>spec=None</em>, <em>side_effect=None</em>, <em>return_value=DEFAULT</em>, <em>wraps=None</em>, <em>name=None</em>, <em>spec_set=None</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.Mock" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Create a new <cite>Mock</cite> object. <cite>Mock</cite> takes several optional arguments
|
|
that specify the behaviour of the Mock object:</p>
|
|
<ul>
|
|
<li><p class="first"><cite>spec</cite>: This can be either a list of strings or an existing object (a
|
|
class or instance) that acts as the specification for the mock object. If
|
|
you pass in an object then a list of strings is formed by calling dir on
|
|
the object (excluding unsupported magic attributes and methods).
|
|
Accessing any attribute not in this list will raise an <cite>AttributeError</cite>.</p>
|
|
<p>If <cite>spec</cite> is an object (rather than a list of strings) then
|
|
<a class="reference internal" href="#mock.Mock.__class__" title="mock.Mock.__class__"><tt class="xref py py-attr docutils literal"><span class="pre">__class__</span></tt></a> returns the class of the spec object. This allows mocks
|
|
to pass <cite>isinstance</cite> tests.</p>
|
|
</li>
|
|
<li><p class="first"><cite>spec_set</cite>: A stricter variant of <cite>spec</cite>. If used, attempting to <em>set</em>
|
|
or get an attribute on the mock that isn’t on the object passed as
|
|
<cite>spec_set</cite> will raise an <cite>AttributeError</cite>.</p>
|
|
</li>
|
|
<li><p class="first"><cite>side_effect</cite>: A function to be called whenever the Mock is called. See
|
|
the <a class="reference internal" href="#mock.Mock.side_effect" title="mock.Mock.side_effect"><tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt></a> attribute. Useful for raising exceptions or
|
|
dynamically changing return values. The function is called with the same
|
|
arguments as the mock, and unless it returns <a class="reference internal" href="sentinel.html#mock.DEFAULT" title="mock.DEFAULT"><tt class="xref py py-data docutils literal"><span class="pre">DEFAULT</span></tt></a>, the return
|
|
value of this function is used as the return value.</p>
|
|
<p>Alternatively <cite>side_effect</cite> can be an exception class or instance. In
|
|
this case the exception will be raised when the mock is called.</p>
|
|
<p>If <cite>side_effect</cite> is an iterable then each call to the mock will return
|
|
the next value from the iterable. If any of the members of the iterable
|
|
are exceptions they will be raised instead of returned.</p>
|
|
<p>A <cite>side_effect</cite> can be cleared by setting it to <cite>None</cite>.</p>
|
|
</li>
|
|
<li><p class="first"><cite>return_value</cite>: The value returned when the mock is called. By default
|
|
this is a new Mock (created on first access). See the
|
|
<a class="reference internal" href="#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.</p>
|
|
</li>
|
|
<li><p class="first"><cite>wraps</cite>: Item for the mock object to wrap. If <cite>wraps</cite> is not None then
|
|
calling the Mock will pass the call through to the wrapped object
|
|
(returning the real result and ignoring <cite>return_value</cite>). Attribute access
|
|
on the mock will return a Mock object that wraps the corresponding
|
|
attribute of the wrapped object (so attempting to access an attribute
|
|
that doesn’t exist will raise an <cite>AttributeError</cite>).</p>
|
|
<p>If the mock has an explicit <cite>return_value</cite> set then calls are not passed
|
|
to the wrapped object and the <cite>return_value</cite> is returned instead.</p>
|
|
</li>
|
|
<li><p class="first"><cite>name</cite>: If the mock has a name then it will be used in the repr of the
|
|
mock. This can be useful for debugging. The name is propagated to child
|
|
mocks.</p>
|
|
</li>
|
|
</ul>
|
|
<p>Mocks can also be called with arbitrary keyword arguments. These will be
|
|
used to set attributes on the mock after it is created. See the
|
|
<a class="reference internal" href="#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 for details.</p>
|
|
<dl class="method">
|
|
<dt id="mock.Mock.assert_called_with">
|
|
<tt class="descname">assert_called_with</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.Mock.assert_called_with" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>This method is a convenient way of asserting that calls are made in a
|
|
particular way:</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="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">test</span><span class="o">=</span><span class="s">'wow'</span><span class="p">)</span>
|
|
<span class="go"><Mock name='mock.method()' id='...'></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">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> <span class="n">test</span><span class="o">=</span><span class="s">'wow'</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="mock.Mock.assert_called_once_with">
|
|
<tt class="descname">assert_called_once_with</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.Mock.assert_called_once_with" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Assert that the mock was called exactly once and with the specified
|
|
arguments.</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="s">'foo'</span><span class="p">,</span> <span class="n">bar</span><span class="o">=</span><span class="s">'baz'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="n">bar</span><span class="o">=</span><span class="s">'baz'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="n">bar</span><span class="o">=</span><span class="s">'baz'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s">'foo'</span><span class="p">,</span> <span class="n">bar</span><span class="o">=</span><span class="s">'baz'</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">AssertionError</span>: <span class="n">Expected to be called once. Called 2 times.</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="mock.Mock.assert_any_call">
|
|
<tt class="descname">assert_any_call</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.Mock.assert_any_call" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>assert the mock has been called with the specified arguments.</p>
|
|
<p>The assert passes if the mock has <em>ever</em> been called, unlike
|
|
<a class="reference internal" href="#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> and <a class="reference internal" href="#mock.Mock.assert_called_once_with" title="mock.Mock.assert_called_once_with"><tt class="xref py py-meth docutils literal"><span class="pre">assert_called_once_with()</span></tt></a> that
|
|
only pass if the call is the most recent 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">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="n">arg</span><span class="o">=</span><span class="s">'thing'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(</span><span class="s">'some'</span><span class="p">,</span> <span class="s">'thing'</span><span class="p">,</span> <span class="s">'else'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">assert_any_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="n">arg</span><span class="o">=</span><span class="s">'thing'</span><span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="mock.Mock.assert_has_calls">
|
|
<tt class="descname">assert_has_calls</tt><big>(</big><em>calls</em>, <em>any_order=False</em><big>)</big><a class="headerlink" href="#mock.Mock.assert_has_calls" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>assert the mock has been called with the specified calls.
|
|
The <cite>mock_calls</cite> list is checked for the calls.</p>
|
|
<p>If <cite>any_order</cite> is False (the default) then the calls must be
|
|
sequential. There can be extra calls before or after the
|
|
specified calls.</p>
|
|
<p>If <cite>any_order</cite> is True then the calls can be in any order, but
|
|
they must all appear in <a class="reference internal" href="#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>.</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="gp">>>> </span><span class="n">mock</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="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="gp">>>> </span><span class="n">calls</span> <span class="o">=</span> <span class="p">[</span><span class="n">call</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="n">call</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="o">.</span><span class="n">assert_has_calls</span><span class="p">(</span><span class="n">calls</span><span class="p">)</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="p">(</span><span class="mi">4</span><span class="p">),</span> <span class="n">call</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="n">call</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="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>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="mock.Mock.reset_mock">
|
|
<tt class="descname">reset_mock</tt><big>(</big><big>)</big><a class="headerlink" href="#mock.Mock.reset_mock" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>The reset_mock method resets all the call attributes on a mock object:</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="s">'hello'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">called</span>
|
|
<span class="go">True</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">reset_mock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">called</span>
|
|
<span class="go">False</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>This can be useful where you want to make a series of assertions that
|
|
reuse the same object. Note that <cite>reset_mock</cite> <em>doesn’t</em> clear the
|
|
return value, <a class="reference internal" href="#mock.Mock.side_effect" title="mock.Mock.side_effect"><tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt></a> or any child attributes you have
|
|
set using normal assignment. Child mocks and the return value mock
|
|
(if any) are reset as well.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="mock.Mock.mock_add_spec">
|
|
<tt class="descname">mock_add_spec</tt><big>(</big><em>spec</em>, <em>spec_set=False</em><big>)</big><a class="headerlink" href="#mock.Mock.mock_add_spec" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Add a spec to a mock. <cite>spec</cite> can either be an object or a
|
|
list of strings. Only attributes on the <cite>spec</cite> can be fetched as
|
|
attributes from the mock.</p>
|
|
<p>If <cite>spec_set</cite> is <cite>True</cite> then only attributes on the spec can be set.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="mock.Mock.attach_mock">
|
|
<tt class="descname">attach_mock</tt><big>(</big><em>mock</em>, <em>attribute</em><big>)</big><a class="headerlink" href="#mock.Mock.attach_mock" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Attach a mock as an attribute of this one, replacing its name and
|
|
parent. Calls to the attached mock will be recorded in the
|
|
<a class="reference internal" href="#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> and <a class="reference internal" href="#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> attributes of this one.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="mock.Mock.configure_mock">
|
|
<tt class="descname">configure_mock</tt><big>(</big><em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.Mock.configure_mock" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Set attributes on the mock through keyword arguments.</p>
|
|
<p>Attributes plus return values and side effects can be set on child
|
|
mocks using standard dot notation and unpacking a dictionary in the
|
|
method call:</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">attrs</span> <span class="o">=</span> <span class="p">{</span><span class="s">'method.return_value'</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s">'other.side_effect'</span><span class="p">:</span> <span class="ne">KeyError</span><span class="p">}</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">configure_mock</span><span class="p">(</span><span class="o">**</span><span class="n">attrs</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">3</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">other</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">KeyError</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The same thing can be achieved in the constructor call to mocks:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">attrs</span> <span class="o">=</span> <span class="p">{</span><span class="s">'method.return_value'</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s">'other.side_effect'</span><span class="p">:</span> <span class="ne">KeyError</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="n">some_attribute</span><span class="o">=</span><span class="s">'eggs'</span><span class="p">,</span> <span class="o">**</span><span class="n">attrs</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">some_attribute</span>
|
|
<span class="go">'eggs'</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>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">other</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">KeyError</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><cite>configure_mock</cite> exists to make it easier to do configuration
|
|
after the mock has been created.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="mock.Mock.__dir__">
|
|
<tt class="descname">__dir__</tt><big>(</big><big>)</big><a class="headerlink" href="#mock.Mock.__dir__" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><cite>Mock</cite> objects limit the results of <cite>dir(some_mock)</cite> to useful results.
|
|
For mocks with a <cite>spec</cite> this includes all the permitted attributes
|
|
for the mock.</p>
|
|
<p>See <a class="reference internal" href="helpers.html#mock.FILTER_DIR" title="mock.FILTER_DIR"><tt class="xref py py-data docutils literal"><span class="pre">FILTER_DIR</span></tt></a> for what this filtering does, and how to
|
|
switch it off.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="mock.Mock._get_child_mock">
|
|
<tt class="descname">_get_child_mock</tt><big>(</big><em>**kw</em><big>)</big><a class="headerlink" href="#mock.Mock._get_child_mock" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Create the child mocks for attributes and return value.
|
|
By default child mocks will be the same type as the parent.
|
|
Subclasses of Mock may want to override this to customize the way
|
|
child mocks are made.</p>
|
|
<p>For non-callable mocks the callable variant will be used (rather than
|
|
any custom subclass).</p>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="mock.Mock.called">
|
|
<tt class="descname">called</tt><a class="headerlink" href="#mock.Mock.called" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>A boolean representing whether or not the mock object has been 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">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="o">.</span><span class="n">called</span>
|
|
<span class="go">False</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">called</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="mock.Mock.call_count">
|
|
<tt class="descname">call_count</tt><a class="headerlink" href="#mock.Mock.call_count" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>An integer telling you how many times the mock object has been 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">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="o">.</span><span class="n">call_count</span>
|
|
<span class="go">0</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="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">call_count</span>
|
|
<span class="go">2</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="mock.Mock.return_value">
|
|
<tt class="descname">return_value</tt><a class="headerlink" href="#mock.Mock.return_value" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Set this to configure the value returned by calling 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">return_value</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="go">'fish'</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The default return value is a mock object and you can configure it in
|
|
the normal way:</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="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="n">mock</span><span class="o">.</span><span class="n">return_value</span><span class="p">()</span>
|
|
<span class="go"><Mock name='mock()()' 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">assert_called_with</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><cite>return_value</cite> 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="o">.</span><span class="n">return_value</span>
|
|
<span class="go">3</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">()</span>
|
|
<span class="go">3</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="mock.Mock.side_effect">
|
|
<tt class="descname">side_effect</tt><a class="headerlink" href="#mock.Mock.side_effect" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>This can either be a function to be called when the mock is called,
|
|
or an exception (class or instance) to be raised.</p>
|
|
<p>If you pass in a function it will be called with same arguments as the
|
|
mock and unless the function returns the <a class="reference internal" href="sentinel.html#mock.DEFAULT" title="mock.DEFAULT"><tt class="xref py py-data docutils literal"><span class="pre">DEFAULT</span></tt></a> singleton the
|
|
call to the mock will then return whatever the function returns. If the
|
|
function returns <a class="reference internal" href="sentinel.html#mock.DEFAULT" title="mock.DEFAULT"><tt class="xref py py-data docutils literal"><span class="pre">DEFAULT</span></tt></a> then the mock will return its normal
|
|
value (from the <a class="reference internal" href="#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>.</p>
|
|
<p>An example of a mock that raises an exception (to test exception
|
|
handling of an API):</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">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>
|
|
<p>Using <cite>side_effect</cite> to return a sequence of values:</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">side_effect</span> <span class="o">=</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="mi">1</span><span class="p">]</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="p">(),</span> <span class="n">mock</span><span class="p">(),</span> <span class="n">mock</span><span class="p">()</span>
|
|
<span class="go">(3, 2, 1)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The <cite>side_effect</cite> function is called with the same arguments as the
|
|
mock (so it is wise for it to take arbitrary args and keyword
|
|
arguments) and whatever it returns is used as the return value for
|
|
the call. The exception is if <cite>side_effect</cite> returns <a class="reference internal" href="sentinel.html#mock.DEFAULT" title="mock.DEFAULT"><tt class="xref py py-data docutils literal"><span class="pre">DEFAULT</span></tt></a>,
|
|
in which case the normal <a class="reference internal" href="#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> is 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="n">return_value</span><span class="o">=</span><span class="mi">3</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="k">return</span> <span class="n">DEFAULT</span>
|
|
<span class="gp">...</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="n">mock</span><span class="p">()</span>
|
|
<span class="go">3</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><cite>side_effect</cite> can be set in the constructor. Here’s an example that
|
|
adds one to the value the mock is called with and returns it:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">side_effect</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">value</span><span class="p">:</span> <span class="n">value</span> <span class="o">+</span> <span class="mi">1</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="mi">3</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="o">-</span><span class="mi">8</span><span class="p">)</span>
|
|
<span class="go">-7</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Setting <cite>side_effect</cite> to <cite>None</cite> clears it:</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">Mock</span>
|
|
<span class="gp">>>> </span><span class="n">m</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">KeyError</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">m</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">KeyError</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span>
|
|
<span class="go">3</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="mock.Mock.call_args">
|
|
<tt class="descname">call_args</tt><a class="headerlink" href="#mock.Mock.call_args" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>This is either <cite>None</cite> (if the mock hasn’t been called), or the
|
|
arguments that the mock was last called with. This will be in the
|
|
form of a tuple: the first member is any ordered arguments the mock
|
|
was called with (or an empty tuple) and the second member is any
|
|
keyword arguments (or an empty dictionary).</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="k">print</span> <span class="n">mock</span><span class="o">.</span><span class="n">call_args</span>
|
|
<span class="go">None</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</span>
|
|
<span class="go">call()</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">call_args</span> <span class="o">==</span> <span class="p">()</span>
|
|
<span class="go">True</span>
|
|
<span class="gp">>>> </span><span class="n">mock</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">call_args</span>
|
|
<span class="go">call(3, 4)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">call_args</span> <span class="o">==</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="go">True</span>
|
|
<span class="gp">>>> </span><span class="n">mock</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">'fish'</span><span class="p">,</span> <span class="nb">next</span><span class="o">=</span><span class="s">'w00t!'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">call_args</span>
|
|
<span class="go">call(3, 4, 5, key='fish', next='w00t!')</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><cite>call_args</cite>, along with members of the lists <a class="reference internal" href="#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>,
|
|
<a class="reference internal" href="#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> and <a class="reference internal" href="#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> are <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> objects.
|
|
These are tuples, so they can be unpacked to get at the individual
|
|
arguments and make more complex assertions. See
|
|
<a class="reference internal" href="helpers.html#calls-as-tuples"><em>calls as tuples</em></a>.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="mock.Mock.call_args_list">
|
|
<tt class="descname">call_args_list</tt><a class="headerlink" href="#mock.Mock.call_args_list" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>This is a list of all the calls made to the mock object in sequence
|
|
(so the length of the list is the number of times it has been
|
|
called). Before any calls have been made it is an empty list. 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 can be used for conveniently constructing lists of
|
|
calls to compare with <cite>call_args_list</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">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="gp">>>> </span><span class="n">mock</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="p">(</span><span class="n">key</span><span class="o">=</span><span class="s">'fish'</span><span class="p">,</span> <span class="nb">next</span><span class="o">=</span><span class="s">'w00t!'</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(), call(3, 4), call(key='fish', next='w00t!')]</span>
|
|
<span class="gp">>>> </span><span class="n">expected</span> <span class="o">=</span> <span class="p">[(),</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="p">({</span><span class="s">'key'</span><span class="p">:</span> <span class="s">'fish'</span><span class="p">,</span> <span class="s">'next'</span><span class="p">:</span> <span class="s">'w00t!'</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>
|
|
<p>Members of <cite>call_args_list</cite> are <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> objects. These can be
|
|
unpacked as tuples to get at the individual arguments. See
|
|
<a class="reference internal" href="helpers.html#calls-as-tuples"><em>calls as tuples</em></a>.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="mock.Mock.method_calls">
|
|
<tt class="descname">method_calls</tt><a class="headerlink" href="#mock.Mock.method_calls" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>As well as tracking calls to themselves, mocks also track calls to
|
|
methods and attributes, and <em>their</em> 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">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="p">()</span>
|
|
<span class="go"><Mock name='mock.method()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">property</span><span class="o">.</span><span class="n">method</span><span class="o">.</span><span class="n">attribute</span><span class="p">()</span>
|
|
<span class="go"><Mock name='mock.property.method.attribute()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">method_calls</span>
|
|
<span class="go">[call.method(), call.property.method.attribute()]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Members of <cite>method_calls</cite> are <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> objects. These can be
|
|
unpacked as tuples to get at the individual arguments. See
|
|
<a class="reference internal" href="helpers.html#calls-as-tuples"><em>calls as tuples</em></a>.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="mock.Mock.mock_calls">
|
|
<tt class="descname">mock_calls</tt><a class="headerlink" href="#mock.Mock.mock_calls" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p><cite>mock_calls</cite> records <em>all</em> calls to the mock object, its methods, magic
|
|
methods <em>and</em> return value mocks.</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">result</span> <span class="o">=</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="o">.</span><span class="n">first</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
|
|
<span class="go"><MagicMock name='mock.first()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">second</span><span class="p">()</span>
|
|
<span class="go"><MagicMock name='mock.second()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">mock</span><span class="p">)</span>
|
|
<span class="go">1</span>
|
|
<span class="gp">>>> </span><span class="n">result</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
|
<span class="go"><MagicMock name='mock()()' id='...'></span>
|
|
<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="o">.</span><span class="n">first</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">3</span><span class="p">),</span> <span class="n">call</span><span class="o">.</span><span class="n">second</span><span class="p">(),</span>
|
|
<span class="gp">... </span><span class="n">call</span><span class="o">.</span><span class="n">__int__</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="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>Members of <cite>mock_calls</cite> are <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> objects. These can be
|
|
unpacked as tuples to get at the individual arguments. See
|
|
<a class="reference internal" href="helpers.html#calls-as-tuples"><em>calls as tuples</em></a>.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="mock.Mock.__class__">
|
|
<tt class="descname">__class__</tt><a class="headerlink" href="#mock.Mock.__class__" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>Normally the <cite>__class__</cite> attribute of an object will return its type.
|
|
For a mock object with a <cite>spec</cite> <cite>__class__</cite> returns the spec class
|
|
instead. This allows mock objects to pass <cite>isinstance</cite> tests for the
|
|
object they are replacing / masquerading as:</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="mi">3</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><cite>__class__</cite> is assignable to, this allows a mock to pass an
|
|
<cite>isinstance</cite> check without forcing you to use a spec:</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">__class__</span> <span class="o">=</span> <span class="nb">dict</span>
|
|
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="nb">dict</span><span class="p">)</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
</dd></dl>
|
|
|
|
<dl class="class">
|
|
<dt id="mock.NonCallableMock">
|
|
<em class="property">class </em><tt class="descname">NonCallableMock</tt><big>(</big><em>spec=None</em>, <em>wraps=None</em>, <em>name=None</em>, <em>spec_set=None</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.NonCallableMock" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>A non-callable version of <cite>Mock</cite>. The constructor parameters have the same
|
|
meaning of <cite>Mock</cite>, with the exception of <cite>return_value</cite> and <cite>side_effect</cite>
|
|
which have no meaning on a non-callable mock.</p>
|
|
</dd></dl>
|
|
|
|
<p>Mock objects that use a class or an instance as a <cite>spec</cite> or <cite>spec_set</cite> are able
|
|
to pass <cite>isintance</cite> 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="nb">isinstance</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="n">SomeClass</span><span class="p">)</span>
|
|
<span class="go">True</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">spec_set</span><span class="o">=</span><span class="n">SomeClass</span><span class="p">())</span>
|
|
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="n">SomeClass</span><span class="p">)</span>
|
|
<span class="go">True</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The <cite>Mock</cite> classes have support for mocking magic methods. See <a class="reference internal" href="magicmock.html#magic-methods"><em>magic
|
|
methods</em></a> for the full details.</p>
|
|
<p>The mock classes and 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> decorators all take arbitrary keyword
|
|
arguments for configuration. For the <cite>patch</cite> decorators the keywords are
|
|
passed to the constructor of the mock being created. The keyword arguments
|
|
are for configuring attributes of the mock:</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="n">attribute</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">other</span><span class="o">=</span><span class="s">'fish'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">attribute</span>
|
|
<span class="go">3</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">other</span>
|
|
<span class="go">'fish'</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The return value and side effect of child mocks can be set in the same way,
|
|
using dotted notation. As you can’t use dotted names directly in a call you
|
|
have to create a dictionary and unpack it using <cite>**</cite>:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">attrs</span> <span class="o">=</span> <span class="p">{</span><span class="s">'method.return_value'</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s">'other.side_effect'</span><span class="p">:</span> <span class="ne">KeyError</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="n">some_attribute</span><span class="o">=</span><span class="s">'eggs'</span><span class="p">,</span> <span class="o">**</span><span class="n">attrs</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">some_attribute</span>
|
|
<span class="go">'eggs'</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>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">other</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">KeyError</span>
|
|
</pre></div>
|
|
</div>
|
|
<dl class="class">
|
|
<dt id="mock.PropertyMock">
|
|
<em class="property">class </em><tt class="descname">PropertyMock</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.PropertyMock" title="Permalink to this definition">¶</a></dt>
|
|
<dd><p>A mock intended to be used as a property, or other descriptor, on a class.
|
|
<cite>PropertyMock</cite> provides <cite>__get__</cite> and <cite>__set__</cite> methods so you can specify
|
|
a return value when it is fetched.</p>
|
|
<p>Fetching a <cite>PropertyMock</cite> instance from an object calls the mock, with
|
|
no args. Setting it calls the mock with the value being set.</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="nd">@property</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">return</span> <span class="s">'something'</span>
|
|
<span class="gp">... </span> <span class="nd">@foo.setter</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="n">value</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="p">(</span><span class="s">'__main__.Foo.foo'</span><span class="p">,</span> <span class="n">new_callable</span><span class="o">=</span><span class="n">PropertyMock</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">'mockity-mock'</span>
|
|
<span class="gp">... </span> <span class="n">this_foo</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="k">print</span> <span class="n">this_foo</span><span class="o">.</span><span class="n">foo</span>
|
|
<span class="gp">... </span> <span class="n">this_foo</span><span class="o">.</span><span class="n">foo</span> <span class="o">=</span> <span class="mi">6</span>
|
|
<span class="gp">...</span>
|
|
<span class="go">mockity-mock</span>
|
|
<span class="gp">>>> </span><span class="n">mock_foo</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call(), call(6)]</span>
|
|
</pre></div>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<p>Because of the way mock attributes are stored you can’t directly attach a
|
|
<cite>PropertyMock</cite> to a mock object. Instead you can attach it to the mock type
|
|
object:</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">p</span> <span class="o">=</span> <span class="n">PropertyMock</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="nb">type</span><span class="p">(</span><span class="n">m</span><span class="p">)</span><span class="o">.</span><span class="n">foo</span> <span class="o">=</span> <span class="n">p</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">foo</span>
|
|
<span class="go">3</span>
|
|
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<span class="target" id="index-5"></span></div>
|
|
<div class="section" id="calling">
|
|
<span id="index-6"></span><h1>Calling<a class="headerlink" href="#calling" title="Permalink to this headline">¶</a></h1>
|
|
<p>Mock objects are callable. The call will return the value set as the
|
|
<a class="reference internal" href="#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. The default return value is a new Mock
|
|
object; it is created the first time the return value is accessed (either
|
|
explicitly or by calling the Mock) - but it is stored and the same one
|
|
returned each time.</p>
|
|
<p>Calls made to the object will be recorded in the attributes
|
|
like <a class="reference internal" href="#mock.Mock.call_args" title="mock.Mock.call_args"><tt class="xref py py-attr docutils literal"><span class="pre">call_args</span></tt></a> and <a class="reference internal" href="#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>
|
|
<p>If <a class="reference internal" href="#mock.Mock.side_effect" title="mock.Mock.side_effect"><tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt></a> is set then it will be called after the call has
|
|
been recorded, so if <cite>side_effect</cite> raises an exception the call is still
|
|
recorded.</p>
|
|
<p>The simplest way to make a mock raise an exception when called is to make
|
|
<a class="reference internal" href="#mock.Mock.side_effect" title="mock.Mock.side_effect"><tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt></a> an exception class or instance:</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="n">side_effect</span><span class="o">=</span><span class="ne">IndexError</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="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">IndexError</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call(1, 2, 3)]</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s">'Bang!'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">(</span><span class="s">'two'</span><span class="p">,</span> <span class="s">'three'</span><span class="p">,</span> <span class="s">'four'</span><span class="p">)</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">KeyError</span>: <span class="n">'Bang!'</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call(1, 2, 3), call('two', 'three', 'four')]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If <cite>side_effect</cite> is a function then whatever that function returns is what
|
|
calls to the mock return. The <cite>side_effect</cite> function is called with the
|
|
same arguments as the mock. This allows you to vary the return value of the
|
|
call dynamically, based on the input:</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="n">value</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">value</span> <span class="o">+</span> <span class="mi">1</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">m</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">m</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
|
<span class="go">2</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
|
|
<span class="go">3</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call(1), call(2)]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you want the mock to still return the default return value (a new mock), or
|
|
any set return value, then there are two ways of doing this. Either return
|
|
<cite>mock.return_value</cite> from inside <cite>side_effect</cite>, or return <a class="reference internal" href="sentinel.html#mock.DEFAULT" title="mock.DEFAULT"><tt class="xref py py-data docutils literal"><span class="pre">DEFAULT</span></tt></a>:</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="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="k">return</span> <span class="n">m</span><span class="o">.</span><span class="n">return_value</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">m</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="n">m</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">m</span><span class="p">()</span>
|
|
<span class="go">3</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="k">return</span> <span class="n">DEFAULT</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">m</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="n">m</span><span class="p">()</span>
|
|
<span class="go">3</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>To remove a <cite>side_effect</cite>, and return to the default behaviour, set the
|
|
<cite>side_effect</cite> to <cite>None</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="n">return_value</span><span class="o">=</span><span class="mi">6</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="k">return</span> <span class="mi">3</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">m</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="n">m</span><span class="p">()</span>
|
|
<span class="go">3</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="bp">None</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span>
|
|
<span class="go">6</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The <cite>side_effect</cite> can also be any iterable object. Repeated calls to the mock
|
|
will return values from the iterable (until the iterable is exhausted and
|
|
a <cite>StopIteration</cite> is raised):</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="n">side_effect</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span>
|
|
<span class="go">1</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span>
|
|
<span class="go">2</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span>
|
|
<span class="go">3</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">StopIteration</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If any members of the iterable are exceptions they will be raised instead of
|
|
returned:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">iterable</span> <span class="o">=</span> <span class="p">(</span><span class="mi">33</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">,</span> <span class="mi">66</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">m</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">iterable</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span>
|
|
<span class="go">33</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="c">...</span>
|
|
<span class="gr">ValueError</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="p">()</span>
|
|
<span class="go">66</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="deleting-attributes">
|
|
<span id="id2"></span><h1>Deleting Attributes<a class="headerlink" href="#deleting-attributes" title="Permalink to this headline">¶</a></h1>
|
|
<p>Mock objects create attributes on demand. This allows them to pretend to be
|
|
objects of any type.</p>
|
|
<p>You may want a mock object to return <cite>False</cite> to a <cite>hasattr</cite> call, or raise an
|
|
<cite>AttributeError</cite> when an attribute is fetched. You can do this by providing
|
|
an object as a <cite>spec</cite> for a mock, but that isn’t always convenient.</p>
|
|
<p>You “block” attributes by deleting them. Once deleted, accessing an attribute
|
|
will raise an <cite>AttributeError</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="gp">>>> </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="s">'m'</span><span class="p">)</span>
|
|
<span class="go">True</span>
|
|
<span class="gp">>>> </span><span class="k">del</span> <span class="n">mock</span><span class="o">.</span><span class="n">m</span>
|
|
<span class="gp">>>> </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="s">'m'</span><span class="p">)</span>
|
|
<span class="go">False</span>
|
|
<span class="gp">>>> </span><span class="k">del</span> <span class="n">mock</span><span class="o">.</span><span class="n">f</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">f</span>
|
|
<span class="gt">Traceback (most recent call last):</span>
|
|
<span class="o">...</span>
|
|
<span class="gr">AttributeError</span>: <span class="n">f</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="attaching-mocks-as-attributes">
|
|
<h1>Attaching Mocks as Attributes<a class="headerlink" href="#attaching-mocks-as-attributes" title="Permalink to this headline">¶</a></h1>
|
|
<p>When you attach a mock as an attribute of another mock (or as the return
|
|
value) it becomes a “child” of that mock. Calls to the child are recorded in
|
|
the <a class="reference internal" href="#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> and <a class="reference internal" href="#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> attributes of the
|
|
parent. This is useful for configuring child mocks and then attaching them to
|
|
the parent, or for attaching mocks to a parent that records all calls to the
|
|
children and allows you to make assertions about the order of calls between
|
|
mocks:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">parent</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">child1</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="bp">None</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">child2</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="bp">None</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">parent</span><span class="o">.</span><span class="n">child1</span> <span class="o">=</span> <span class="n">child1</span>
|
|
<span class="gp">>>> </span><span class="n">parent</span><span class="o">.</span><span class="n">child2</span> <span class="o">=</span> <span class="n">child2</span>
|
|
<span class="gp">>>> </span><span class="n">child1</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">child2</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">parent</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call.child1(1), call.child2(2)]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The exception to this is if the mock has a name. This allows you to prevent
|
|
the “parenting” if for some reason you don’t want it to happen.</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">not_a_child</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">'not-a-child'</span><span class="p">)</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">not_a_child</span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">attribute</span><span class="p">()</span>
|
|
<span class="go"><MagicMock name='not-a-child()' id='...'></span>
|
|
<span class="gp">>>> </span><span class="n">mock</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[]</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Mocks created for you by <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> are automatically given names. To
|
|
attach mocks that have names to a parent you use the <a class="reference internal" href="#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:</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">thing1</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">thing2</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>
|
|
<span class="gp">>>> </span><span class="n">parent</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">'__main__.thing1'</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="k">as</span> <span class="n">child1</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">'__main__.thing2'</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="k">as</span> <span class="n">child2</span><span class="p">:</span>
|
|
<span class="gp">... </span> <span class="n">parent</span><span class="o">.</span><span class="n">attach_mock</span><span class="p">(</span><span class="n">child1</span><span class="p">,</span> <span class="s">'child1'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">parent</span><span class="o">.</span><span class="n">attach_mock</span><span class="p">(</span><span class="n">child2</span><span class="p">,</span> <span class="s">'child2'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">child1</span><span class="p">(</span><span class="s">'one'</span><span class="p">)</span>
|
|
<span class="gp">... </span> <span class="n">child2</span><span class="p">(</span><span class="s">'two'</span><span class="p">)</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">parent</span><span class="o">.</span><span class="n">mock_calls</span>
|
|
<span class="go">[call.child1('one'), call.child2('two')]</span>
|
|
</pre></div>
|
|
</div>
|
|
<hr class="docutils" />
|
|
<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="#id1">[1]</a></td><td>The only exceptions are magic methods and attributes (those that have
|
|
leading and trailing double underscores). Mock doesn’t create these but
|
|
instead of raises an <tt class="docutils literal"><span class="pre">AttributeError</span></tt>. This is because the interpreter
|
|
will often implicitly request these methods, and gets <em>very</em> confused to
|
|
get a new Mock object when it expects a magic method. If you need magic
|
|
method support see <a class="reference internal" href="magicmock.html#magic-methods"><em>magic methods</em></a>.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</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="#">The Mock Class</a></li>
|
|
<li><a class="reference internal" href="#calling">Calling</a></li>
|
|
<li><a class="reference internal" href="#deleting-attributes">Deleting Attributes</a></li>
|
|
<li><a class="reference internal" href="#attaching-mocks-as-attributes">Attaching Mocks as Attributes</a></li>
|
|
</ul>
|
|
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="index.html"
|
|
title="previous chapter">Mock - Mocking and Testing Library</a></p>
|
|
<h4>Next topic</h4>
|
|
<p class="topless"><a href="patch.html"
|
|
title="next chapter">Patch Decorators</a></p>
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="_sources/mock.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="patch.html" title="Patch Decorators"
|
|
>next</a> |</li>
|
|
<li class="right" >
|
|
<a href="index.html" title="Mock - Mocking and Testing Library"
|
|
>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> |