| |
| <!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>Patch Decorators — Mock 1.0.1 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.1', |
| 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.1 documentation" href="index.html" /> |
| <link rel="next" title="Helpers" href="helpers.html" /> |
| <link rel="prev" title="The Mock Class" href="mock.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="helpers.html" title="Helpers" |
| accesskey="N">next</a> |</li> |
| <li class="right" > |
| <a href="mock.html" title="The Mock Class" |
| accesskey="P">previous</a> |</li> |
| <li><a href="index.html">Mock 1.0.1 documentation</a> »</li> |
| </ul> |
| </div> |
| |
| <div class="document"> |
| <div class="documentwrapper"> |
| <div class="bodywrapper"> |
| <div class="body"> |
| |
| <div class="section" id="patch-decorators"> |
| <h1>Patch Decorators<a class="headerlink" href="#patch-decorators" title="Permalink to this headline">¶</a></h1> |
| <p>The patch decorators are used for patching objects only within the scope of |
| the function they decorate. They automatically handle the unpatching for you, |
| even if exceptions are raised. All of these functions can also be used in with |
| statements or as class decorators.</p> |
| <div class="section" id="patch"> |
| <h2>patch<a class="headerlink" href="#patch" title="Permalink to this headline">¶</a></h2> |
| <div class="admonition note"> |
| <p class="first admonition-title">Note</p> |
| <p class="last"><cite>patch</cite> is straightforward to use. The key is to do the patching in the |
| right namespace. See the section <a class="reference internal" href="#id1">where to patch</a>.</p> |
| </div> |
| <dl class="function"> |
| <dt id="mock.patch"> |
| <tt class="descname">patch</tt><big>(</big><em>target</em>, <em>new=DEFAULT</em>, <em>spec=None</em>, <em>create=False</em>, <em>spec_set=None</em>, <em>autospec=None</em>, <em>new_callable=None</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.patch" title="Permalink to this definition">¶</a></dt> |
| <dd><p><cite>patch</cite> acts as a function decorator, class decorator or a context |
| manager. Inside the body of the function or with statement, the <cite>target</cite> |
| is patched with a <cite>new</cite> object. When the function/with statement exits |
| the patch is undone.</p> |
| <p>If <cite>new</cite> is omitted, then the target is replaced with a |
| <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>. If <cite>patch</cite> is used as a decorator and <cite>new</cite> is |
| omitted, the created mock is passed in as an extra argument to the |
| decorated function. If <cite>patch</cite> is used as a context manager the created |
| mock is returned by the context manager.</p> |
| <p><cite>target</cite> should be a string in the form <cite>‘package.module.ClassName’</cite>. The |
| <cite>target</cite> is imported and the specified object replaced with the <cite>new</cite> |
| object, so the <cite>target</cite> must be importable from the environment you are |
| calling <cite>patch</cite> from. The target is imported when the decorated function |
| is executed, not at decoration time.</p> |
| <p>The <cite>spec</cite> and <cite>spec_set</cite> keyword arguments are passed to the <cite>MagicMock</cite> |
| if patch is creating one for you.</p> |
| <p>In addition you can pass <cite>spec=True</cite> or <cite>spec_set=True</cite>, which causes |
| patch to pass in the object being mocked as the spec/spec_set object.</p> |
| <p><cite>new_callable</cite> allows you to specify a different class, or callable object, |
| that will be called to create the <cite>new</cite> object. By default <cite>MagicMock</cite> is |
| used.</p> |
| <p>A more powerful form of <cite>spec</cite> is <cite>autospec</cite>. If you set <cite>autospec=True</cite> |
| then the mock with be created with a spec from the object being replaced. |
| All attributes of the mock will also have the spec of the corresponding |
| attribute of the object being replaced. Methods and functions being mocked |
| will have their arguments checked and will raise a <cite>TypeError</cite> if they are |
| called with the wrong signature. For mocks |
| replacing a class, their return value (the ‘instance’) will have the same |
| spec as the class. See the <a class="reference internal" href="helpers.html#mock.create_autospec" title="mock.create_autospec"><tt class="xref py py-func docutils literal"><span class="pre">create_autospec()</span></tt></a> function and |
| <a class="reference internal" href="helpers.html#auto-speccing"><em>Autospeccing</em></a>.</p> |
| <p>Instead of <cite>autospec=True</cite> you can pass <cite>autospec=some_object</cite> to use an |
| arbitrary object as the spec instead of the one being replaced.</p> |
| <p>By default <cite>patch</cite> will fail to replace attributes that don’t exist. If |
| you pass in <cite>create=True</cite>, and the attribute doesn’t exist, patch will |
| create the attribute for you when the patched function is called, and |
| delete it again afterwards. This is useful for writing tests against |
| attributes that your production code creates at runtime. It is off by by |
| default because it can be dangerous. With it switched on you can write |
| passing tests against APIs that don’t actually exist!</p> |
| <p>Patch can be used as a <cite>TestCase</cite> class decorator. It works by |
| decorating each test method in the class. This reduces the boilerplate |
| code when your test methods share a common patchings set. <cite>patch</cite> finds |
| tests by looking for method names that start with <cite>patch.TEST_PREFIX</cite>. |
| By default this is <cite>test</cite>, which matches the way <cite>unittest</cite> finds tests. |
| You can specify an alternative prefix by setting <cite>patch.TEST_PREFIX</cite>.</p> |
| <p>Patch can be used as a context manager, with the with statement. Here the |
| patching applies to the indented block after the with statement. If you |
| use “as” then the patched object will be bound to the name after the |
| “as”; very useful if <cite>patch</cite> is creating a mock object for you.</p> |
| <p><cite>patch</cite> takes arbitrary keyword arguments. These will be passed to |
| the <cite>Mock</cite> (or <cite>new_callable</cite>) on construction.</p> |
| <p><cite>patch.dict(...)</cite>, <cite>patch.multiple(...)</cite> and <cite>patch.object(...)</cite> are |
| available for alternate use-cases.</p> |
| </dd></dl> |
| |
| <p><cite>patch</cite> as function decorator, creating the mock for you and passing it into |
| the decorated function:</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">'__main__.SomeClass'</span><span class="p">)</span> |
| <span class="gp">... </span><span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">normal_argument</span><span class="p">,</span> <span class="n">mock_class</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">print</span> <span class="n">mock_class</span> <span class="ow">is</span> <span class="n">SomeClass</span> |
| <span class="gp">...</span> |
| <span class="gp">>>> </span><span class="n">function</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span> |
| <span class="go">True</span> |
| </pre></div> |
| </div> |
| <p>Patching a class replaces the class with a <cite>MagicMock</cite> <em>instance</em>. If the |
| class is instantiated in the code under test then it will be the |
| <a class="reference internal" href="mock.html#mock.Mock.return_value" title="mock.Mock.return_value"><tt class="xref py py-attr docutils literal"><span class="pre">return_value</span></tt></a> of the mock that will be used.</p> |
| <p>If the class is instantiated multiple times you could use |
| <a class="reference internal" href="mock.html#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> to return a new mock each time. Alternatively you |
| can set the <cite>return_value</cite> to be anything you want.</p> |
| <p>To configure return values on methods of <em>instances</em> on the patched class |
| you must do this on the <cite>return_value</cite>. For example:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Class</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="p">(</span><span class="s">'__main__.Class'</span><span class="p">)</span> <span class="k">as</span> <span class="n">MockClass</span><span class="p">:</span> |
| <span class="gp">... </span> <span class="n">instance</span> <span class="o">=</span> <span class="n">MockClass</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">'foo'</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">Class</span><span class="p">()</span> <span class="ow">is</span> <span class="n">instance</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">Class</span><span class="p">()</span><span class="o">.</span><span class="n">method</span><span class="p">()</span> <span class="o">==</span> <span class="s">'foo'</span> |
| <span class="gp">...</span> |
| </pre></div> |
| </div> |
| <p>If you use <cite>spec</cite> or <cite>spec_set</cite> and <cite>patch</cite> is replacing a <em>class</em>, then the |
| return value of the created mock will have the same spec.</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">Class</span> |
| <span class="gp">>>> </span><span class="n">patcher</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">'__main__.Class'</span><span class="p">,</span> <span class="n">spec</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> |
| <span class="gp">>>> </span><span class="n">MockClass</span> <span class="o">=</span> <span class="n">patcher</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="n">instance</span> <span class="o">=</span> <span class="n">MockClass</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">Original</span><span class="p">)</span> |
| <span class="gp">>>> </span><span class="n">patcher</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span> |
| </pre></div> |
| </div> |
| <p>The <cite>new_callable</cite> argument is useful where you want to use an alternative |
| class to the default <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> for the created mock. For example, if |
| you wanted a <a class="reference internal" href="mock.html#mock.NonCallableMock" title="mock.NonCallableMock"><tt class="xref py py-class docutils literal"><span class="pre">NonCallableMock</span></tt></a> to be used:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">thing</span> <span class="o">=</span> <span class="nb">object</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__.thing'</span><span class="p">,</span> <span class="n">new_callable</span><span class="o">=</span><span class="n">NonCallableMock</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_thing</span><span class="p">:</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">thing</span> <span class="ow">is</span> <span class="n">mock_thing</span> |
| <span class="gp">... </span> <span class="n">thing</span><span class="p">()</span> |
| <span class="gp">...</span> |
| <span class="gt">Traceback (most recent call last):</span> |
| <span class="c">...</span> |
| <span class="gr">TypeError</span>: <span class="n">'NonCallableMock' object is not callable</span> |
| </pre></div> |
| </div> |
| <p>Another use case might be to replace an object with a <cite>StringIO</cite> instance:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">StringIO</span> <span class="kn">import</span> <span class="n">StringIO</span> |
| <span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span> |
| <span class="gp">... </span> <span class="k">print</span> <span class="s">'Something'</span> |
| <span class="gp">...</span> |
| <span class="gp">>>> </span><span class="nd">@patch</span><span class="p">(</span><span class="s">'sys.stdout'</span><span class="p">,</span> <span class="n">new_callable</span><span class="o">=</span><span class="n">StringIO</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="n">mock_stdout</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="n">foo</span><span class="p">()</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">mock_stdout</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span> <span class="o">==</span> <span class="s">'Something</span><span class="se">\n</span><span class="s">'</span> |
| <span class="gp">...</span> |
| <span class="gp">>>> </span><span class="n">test</span><span class="p">()</span> |
| </pre></div> |
| </div> |
| <p>When <cite>patch</cite> is creating a mock for you, it is common that the first thing |
| you need to do is to configure the mock. Some of that configuration can be done |
| in the call to patch. Any arbitrary keywords you pass into the call will be |
| used to set attributes on the created mock:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">patcher</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">'__main__.thing'</span><span class="p">,</span> <span class="n">first</span><span class="o">=</span><span class="s">'one'</span><span class="p">,</span> <span class="n">second</span><span class="o">=</span><span class="s">'two'</span><span class="p">)</span> |
| <span class="gp">>>> </span><span class="n">mock_thing</span> <span class="o">=</span> <span class="n">patcher</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="n">mock_thing</span><span class="o">.</span><span class="n">first</span> |
| <span class="go">'one'</span> |
| <span class="gp">>>> </span><span class="n">mock_thing</span><span class="o">.</span><span class="n">second</span> |
| <span class="go">'two'</span> |
| </pre></div> |
| </div> |
| <p>As well as attributes on the created mock attributes, like the |
| <a class="reference internal" href="mock.html#mock.Mock.return_value" title="mock.Mock.return_value"><tt class="xref py py-attr docutils literal"><span class="pre">return_value</span></tt></a> and <a class="reference internal" href="mock.html#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>, of child mocks can |
| also be configured. These aren’t syntactically valid to pass in directly as |
| keyword arguments, but a dictionary with these as keys can still be expanded |
| into a <cite>patch</cite> call using <cite>**</cite>:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">config</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">patcher</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">'__main__.thing'</span><span class="p">,</span> <span class="o">**</span><span class="n">config</span><span class="p">)</span> |
| <span class="gp">>>> </span><span class="n">mock_thing</span> <span class="o">=</span> <span class="n">patcher</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="n">mock_thing</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_thing</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> |
| </div> |
| <div class="section" id="patch-object"> |
| <h2>patch.object<a class="headerlink" href="#patch-object" title="Permalink to this headline">¶</a></h2> |
| <dl class="function"> |
| <dt id="mock.patch.object"> |
| <tt class="descclassname">patch.</tt><tt class="descname">object</tt><big>(</big><em>target</em>, <em>attribute</em>, <em>new=DEFAULT</em>, <em>spec=None</em>, <em>create=False</em>, <em>spec_set=None</em>, <em>autospec=None</em>, <em>new_callable=None</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.patch.object" title="Permalink to this definition">¶</a></dt> |
| <dd><p>patch the named member (<cite>attribute</cite>) on an object (<cite>target</cite>) with a mock |
| object.</p> |
| <p><cite>patch.object</cite> can be used as a decorator, class decorator or a context |
| manager. Arguments <cite>new</cite>, <cite>spec</cite>, <cite>create</cite>, <cite>spec_set</cite>, <cite>autospec</cite> and |
| <cite>new_callable</cite> have the same meaning as for <cite>patch</cite>. Like <cite>patch</cite>, |
| <cite>patch.object</cite> takes arbitrary keyword arguments for configuring the mock |
| object it creates.</p> |
| <p>When used as a class decorator <cite>patch.object</cite> honours <cite>patch.TEST_PREFIX</cite> |
| for choosing which methods to wrap.</p> |
| </dd></dl> |
| |
| <p>You can either call <cite>patch.object</cite> with three arguments or two arguments. The |
| three argument form takes the object to be patched, the attribute name and the |
| object to replace the attribute with.</p> |
| <p>When calling with the two argument form you omit the replacement object, and a |
| mock is created for you and passed in as an extra argument to the decorated |
| function:</p> |
| <div class="highlight-python"><div class="highlight"><pre><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">'class_method'</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="n">mock_method</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="n">SomeClass</span><span class="o">.</span><span class="n">class_method</span><span class="p">(</span><span class="mi">3</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="mi">3</span><span class="p">)</span> |
| <span class="gp">...</span> |
| <span class="gp">>>> </span><span class="n">test</span><span class="p">()</span> |
| </pre></div> |
| </div> |
| <p><cite>spec</cite>, <cite>create</cite> and the other arguments to <cite>patch.object</cite> have the same |
| meaning as they do for <cite>patch</cite>.</p> |
| </div> |
| <div class="section" id="patch-dict"> |
| <h2>patch.dict<a class="headerlink" href="#patch-dict" title="Permalink to this headline">¶</a></h2> |
| <dl class="function"> |
| <dt id="mock.patch.dict"> |
| <tt class="descclassname">patch.</tt><tt class="descname">dict</tt><big>(</big><em>in_dict</em>, <em>values=()</em>, <em>clear=False</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.patch.dict" title="Permalink to this definition">¶</a></dt> |
| <dd><p>Patch a dictionary, or dictionary like object, and restore the dictionary |
| to its original state after the test.</p> |
| <p><cite>in_dict</cite> can be a dictionary or a mapping like container. If it is a |
| mapping then it must at least support getting, setting and deleting items |
| plus iterating over keys.</p> |
| <p><cite>in_dict</cite> can also be a string specifying the name of the dictionary, which |
| will then be fetched by importing it.</p> |
| <p><cite>values</cite> can be a dictionary of values to set in the dictionary. <cite>values</cite> |
| can also be an iterable of <cite>(key, value)</cite> pairs.</p> |
| <p>If <cite>clear</cite> is True then the dictionary will be cleared before the new |
| values are set.</p> |
| <p><cite>patch.dict</cite> can also be called with arbitrary keyword arguments to set |
| values in the dictionary.</p> |
| <p><cite>patch.dict</cite> can be used as a context manager, decorator or class |
| decorator. When used as a class decorator <cite>patch.dict</cite> honours |
| <cite>patch.TEST_PREFIX</cite> for choosing which methods to wrap.</p> |
| </dd></dl> |
| |
| <p><cite>patch.dict</cite> can be used to add members to a dictionary, or simply let a test |
| change a dictionary, and ensure the dictionary is restored when the test |
| ends.</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">patch</span> |
| <span class="gp">>>> </span><span class="n">foo</span> <span class="o">=</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="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="p">{}</span> |
| |
| <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">os</span> |
| <span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">dict</span><span class="p">(</span><span class="s">'os.environ'</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="gp">... </span> <span class="k">print</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">'newkey'</span><span class="p">]</span> |
| <span class="gp">...</span> |
| <span class="go">newvalue</span> |
| <span class="gp">>>> </span><span class="k">assert</span> <span class="s">'newkey'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span> |
| </pre></div> |
| </div> |
| <p>Keywords can be used in the <cite>patch.dict</cite> call to set values in the dictionary:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mymodule</span> <span class="o">=</span> <span class="n">MagicMock</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="n">mymodule</span><span class="o">.</span><span class="n">function</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="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">dict</span><span class="p">(</span><span class="s">'sys.modules'</span><span class="p">,</span> <span class="n">mymodule</span><span class="o">=</span><span class="n">mymodule</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="kn">import</span> <span class="nn">mymodule</span> |
| <span class="gp">... </span> <span class="n">mymodule</span><span class="o">.</span><span class="n">function</span><span class="p">(</span><span class="s">'some'</span><span class="p">,</span> <span class="s">'args'</span><span class="p">)</span> |
| <span class="gp">...</span> |
| <span class="go">'fish'</span> |
| </pre></div> |
| </div> |
| <p><cite>patch.dict</cite> can be used with dictionary like objects that aren’t actually |
| dictionaries. At the very minimum they must support item getting, setting, |
| deleting and either iteration or membership test. This corresponds to the |
| magic methods <cite>__getitem__</cite>, <cite>__setitem__</cite>, <cite>__delitem__</cite> and either |
| <cite>__iter__</cite> or <cite>__contains__</cite>.</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Container</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="p">{}</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">__delitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">)</span> |
| <span class="gp">...</span> |
| <span class="gp">>>> </span><span class="n">thing</span> <span class="o">=</span> <span class="n">Container</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="n">thing</span><span class="p">[</span><span class="s">'one'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</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">thing</span><span class="p">,</span> <span class="n">one</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">two</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">thing</span><span class="p">[</span><span class="s">'one'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">thing</span><span class="p">[</span><span class="s">'two'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">3</span> |
| <span class="gp">...</span> |
| <span class="gp">>>> </span><span class="k">assert</span> <span class="n">thing</span><span class="p">[</span><span class="s">'one'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span> |
| <span class="gp">>>> </span><span class="k">assert</span> <span class="nb">list</span><span class="p">(</span><span class="n">thing</span><span class="p">)</span> <span class="o">==</span> <span class="p">[</span><span class="s">'one'</span><span class="p">]</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="patch-multiple"> |
| <h2>patch.multiple<a class="headerlink" href="#patch-multiple" title="Permalink to this headline">¶</a></h2> |
| <dl class="function"> |
| <dt id="mock.patch.multiple"> |
| <tt class="descclassname">patch.</tt><tt class="descname">multiple</tt><big>(</big><em>target</em>, <em>spec=None</em>, <em>create=False</em>, <em>spec_set=None</em>, <em>autospec=None</em>, <em>new_callable=None</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#mock.patch.multiple" title="Permalink to this definition">¶</a></dt> |
| <dd><p>Perform multiple patches in a single call. It takes the object to be |
| patched (either as an object or a string to fetch the object by importing) |
| and keyword arguments for the patches:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">multiple</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="n">FIRST_PATCH</span><span class="o">=</span><span class="s">'one'</span><span class="p">,</span> <span class="n">SECOND_PATCH</span><span class="o">=</span><span class="s">'two'</span><span class="p">):</span> |
| <span class="o">...</span> |
| </pre></div> |
| </div> |
| <p>Use <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> as the value if you want <cite>patch.multiple</cite> to create |
| mocks for you. In this case the created mocks are passed into a decorated |
| function by keyword, and a dictionary is returned when <cite>patch.multiple</cite> is |
| used as a context manager.</p> |
| <p><cite>patch.multiple</cite> can be used as a decorator, class decorator or a context |
| manager. The arguments <cite>spec</cite>, <cite>spec_set</cite>, <cite>create</cite>, <cite>autospec</cite> and |
| <cite>new_callable</cite> have the same meaning as for <cite>patch</cite>. These arguments will |
| be applied to <em>all</em> patches done by <cite>patch.multiple</cite>.</p> |
| <p>When used as a class decorator <cite>patch.multiple</cite> honours <cite>patch.TEST_PREFIX</cite> |
| for choosing which methods to wrap.</p> |
| </dd></dl> |
| |
| <p>If you want <cite>patch.multiple</cite> to create mocks for you, then you can use |
| <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> as the value. If you use <cite>patch.multiple</cite> as a decorator |
| then the created mocks are passed into the decorated function by keyword.</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">thing</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="n">other</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span> |
| |
| <span class="gp">>>> </span><span class="nd">@patch.multiple</span><span class="p">(</span><span class="s">'__main__'</span><span class="p">,</span> <span class="n">thing</span><span class="o">=</span><span class="n">DEFAULT</span><span class="p">,</span> <span class="n">other</span><span class="o">=</span><span class="n">DEFAULT</span><span class="p">)</span> |
| <span class="gp">... </span><span class="k">def</span> <span class="nf">test_function</span><span class="p">(</span><span class="n">thing</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">thing</span><span class="p">,</span> <span class="n">MagicMock</span><span class="p">)</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">MagicMock</span><span class="p">)</span> |
| <span class="gp">...</span> |
| <span class="gp">>>> </span><span class="n">test_function</span><span class="p">()</span> |
| </pre></div> |
| </div> |
| <p><cite>patch.multiple</cite> can be nested with other <cite>patch</cite> decorators, but put arguments |
| passed by keyword <em>after</em> any of the standard arguments created by <cite>patch</cite>:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nd">@patch</span><span class="p">(</span><span class="s">'sys.exit'</span><span class="p">)</span> |
| <span class="gp">... </span><span class="nd">@patch.multiple</span><span class="p">(</span><span class="s">'__main__'</span><span class="p">,</span> <span class="n">thing</span><span class="o">=</span><span class="n">DEFAULT</span><span class="p">,</span> <span class="n">other</span><span class="o">=</span><span class="n">DEFAULT</span><span class="p">)</span> |
| <span class="gp">... </span><span class="k">def</span> <span class="nf">test_function</span><span class="p">(</span><span class="n">mock_exit</span><span class="p">,</span> <span class="n">other</span><span class="p">,</span> <span class="n">thing</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="s">'other'</span> <span class="ow">in</span> <span class="nb">repr</span><span class="p">(</span><span class="n">other</span><span class="p">)</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="s">'thing'</span> <span class="ow">in</span> <span class="nb">repr</span><span class="p">(</span><span class="n">thing</span><span class="p">)</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="s">'exit'</span> <span class="ow">in</span> <span class="nb">repr</span><span class="p">(</span><span class="n">mock_exit</span><span class="p">)</span> |
| <span class="gp">...</span> |
| <span class="gp">>>> </span><span class="n">test_function</span><span class="p">()</span> |
| </pre></div> |
| </div> |
| <p>If <cite>patch.multiple</cite> is used as a context manager, the value returned by the |
| context manger is a dictionary where created mocks are keyed by name:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">multiple</span><span class="p">(</span><span class="s">'__main__'</span><span class="p">,</span> <span class="n">thing</span><span class="o">=</span><span class="n">DEFAULT</span><span class="p">,</span> <span class="n">other</span><span class="o">=</span><span class="n">DEFAULT</span><span class="p">)</span> <span class="k">as</span> <span class="n">values</span><span class="p">:</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="s">'other'</span> <span class="ow">in</span> <span class="nb">repr</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="s">'other'</span><span class="p">])</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="s">'thing'</span> <span class="ow">in</span> <span class="nb">repr</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="s">'thing'</span><span class="p">])</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">values</span><span class="p">[</span><span class="s">'thing'</span><span class="p">]</span> <span class="ow">is</span> <span class="n">thing</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">values</span><span class="p">[</span><span class="s">'other'</span><span class="p">]</span> <span class="ow">is</span> <span class="n">other</span> |
| <span class="gp">...</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="patch-methods-start-and-stop"> |
| <span id="start-and-stop"></span><h2>patch methods: start and stop<a class="headerlink" href="#patch-methods-start-and-stop" title="Permalink to this headline">¶</a></h2> |
| <p>All the patchers have <cite>start</cite> and <cite>stop</cite> methods. These make it simpler to do |
| patching in <cite>setUp</cite> methods or where you want to do multiple patches without |
| nesting decorators or with statements.</p> |
| <p>To use them call <cite>patch</cite>, <cite>patch.object</cite> or <cite>patch.dict</cite> as normal and keep a |
| reference to the returned <cite>patcher</cite> object. You can then call <cite>start</cite> to put |
| the patch in place and <cite>stop</cite> to undo it.</p> |
| <p>If you are using <cite>patch</cite> to create a mock for you then it will be returned by |
| the call to <cite>patcher.start</cite>.</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">patcher</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">'package.module.ClassName'</span><span class="p">)</span> |
| <span class="gp">>>> </span><span class="kn">from</span> <span class="nn">package</span> <span class="kn">import</span> <span class="n">module</span> |
| <span class="gp">>>> </span><span class="n">original</span> <span class="o">=</span> <span class="n">module</span><span class="o">.</span><span class="n">ClassName</span> |
| <span class="gp">>>> </span><span class="n">new_mock</span> <span class="o">=</span> <span class="n">patcher</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="k">assert</span> <span class="n">module</span><span class="o">.</span><span class="n">ClassName</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">original</span> |
| <span class="gp">>>> </span><span class="k">assert</span> <span class="n">module</span><span class="o">.</span><span class="n">ClassName</span> <span class="ow">is</span> <span class="n">new_mock</span> |
| <span class="gp">>>> </span><span class="n">patcher</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="k">assert</span> <span class="n">module</span><span class="o">.</span><span class="n">ClassName</span> <span class="ow">is</span> <span class="n">original</span> |
| <span class="gp">>>> </span><span class="k">assert</span> <span class="n">module</span><span class="o">.</span><span class="n">ClassName</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">new_mock</span> |
| </pre></div> |
| </div> |
| <p>A typical use case for this might be for doing multiple patches in the <cite>setUp</cite> |
| method of a <cite>TestCase</cite>:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyTest</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">patcher1</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">'package.module.Class1'</span><span class="p">)</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">patcher2</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">'package.module.Class2'</span><span class="p">)</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">MockClass1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">patcher1</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">MockClass2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">patcher2</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> |
| <span class="gp">...</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">patcher1</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">patcher2</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span> |
| <span class="gp">...</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="k">assert</span> <span class="n">package</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Class1</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">MockClass1</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">package</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Class2</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">MockClass2</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">run</span><span class="p">()</span> |
| </pre></div> |
| </div> |
| <div class="admonition caution"> |
| <p class="first admonition-title">Caution</p> |
| <p>If you use this technique you must ensure that the patching is “undone” by |
| calling <cite>stop</cite>. This can be fiddlier than you might think, because if an |
| exception is raised in the setUp then tearDown is not called. <a class="reference external" href="http://pypi.python.org/pypi/unittest2">unittest2</a> cleanup functions make this |
| easier.</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyTest</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="n">patcher</span> <span class="o">=</span> <span class="n">patch</span><span class="p">(</span><span class="s">'package.module.Class'</span><span class="p">)</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">MockClass</span> <span class="o">=</span> <span class="n">patcher</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> |
| <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">addCleanup</span><span class="p">(</span><span class="n">patcher</span><span class="o">.</span><span class="n">stop</span><span class="p">)</span> |
| <span class="gp">...</span> |
| <span class="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="k">assert</span> <span class="n">package</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">Class</span> <span class="ow">is</span> <span class="bp">self</span><span class="o">.</span><span class="n">MockClass</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">run</span><span class="p">()</span> |
| </pre></div> |
| </div> |
| <p class="last">As an added bonus you no longer need to keep a reference to the <cite>patcher</cite> |
| object.</p> |
| </div> |
| <p>It is also possible to stop all patches which have been started by using |
| <cite>patch.stopall</cite>.</p> |
| <dl class="function"> |
| <dt id="mock.patch.stopall"> |
| <tt class="descclassname">patch.</tt><tt class="descname">stopall</tt><big>(</big><big>)</big><a class="headerlink" href="#mock.patch.stopall" title="Permalink to this definition">¶</a></dt> |
| <dd><p>Stop all active patches. Only stops patches started with <cite>start</cite>.</p> |
| </dd></dl> |
| |
| </div> |
| <div class="section" id="test-prefix"> |
| <h2>TEST_PREFIX<a class="headerlink" href="#test-prefix" title="Permalink to this headline">¶</a></h2> |
| <p>All of the patchers can be used as class decorators. When used in this way |
| they wrap every test method on the class. The patchers recognise methods that |
| start with <cite>test</cite> as being test methods. This is the same way that the |
| <cite>unittest.TestLoader</cite> finds test methods by default.</p> |
| <p>It is possible that you want to use a different prefix for your tests. You can |
| inform the patchers of the different prefix by setting <cite>patch.TEST_PREFIX</cite>:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">patch</span><span class="o">.</span><span class="n">TEST_PREFIX</span> <span class="o">=</span> <span class="s">'foo'</span> |
| <span class="gp">>>> </span><span class="n">value</span> <span class="o">=</span> <span class="mi">3</span> |
| <span class="go">>>></span> |
| <span class="gp">>>> </span><span class="nd">@patch</span><span class="p">(</span><span class="s">'__main__.value'</span><span class="p">,</span> <span class="s">'not three'</span><span class="p">)</span> |
| <span class="gp">... </span><span class="k">class</span> <span class="nc">Thing</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">foo_one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">print</span> <span class="n">value</span> |
| <span class="gp">... </span> <span class="k">def</span> <span class="nf">foo_two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">print</span> <span class="n">value</span> |
| <span class="gp">...</span> |
| <span class="go">>>></span> |
| <span class="gp">>>> </span><span class="n">Thing</span><span class="p">()</span><span class="o">.</span><span class="n">foo_one</span><span class="p">()</span> |
| <span class="go">not three</span> |
| <span class="gp">>>> </span><span class="n">Thing</span><span class="p">()</span><span class="o">.</span><span class="n">foo_two</span><span class="p">()</span> |
| <span class="go">not three</span> |
| <span class="gp">>>> </span><span class="n">value</span> |
| <span class="go">3</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="nesting-patch-decorators"> |
| <h2>Nesting Patch Decorators<a class="headerlink" href="#nesting-patch-decorators" title="Permalink to this headline">¶</a></h2> |
| <p>If you want to perform multiple patches then you can simply stack up the |
| decorators.</p> |
| <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="nd">@patch.object</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="s">'class_method'</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</span><span class="p">(</span><span class="n">mock1</span><span class="p">,</span> <span class="n">mock2</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">static_method</span> <span class="ow">is</span> <span class="n">mock1</span> |
| <span class="gp">... </span> <span class="k">assert</span> <span class="n">SomeClass</span><span class="o">.</span><span class="n">class_method</span> <span class="ow">is</span> <span class="n">mock2</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="s">'foo'</span><span class="p">)</span> |
| <span class="gp">... </span> <span class="n">SomeClass</span><span class="o">.</span><span class="n">class_method</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> |
| <span class="gp">... </span> <span class="k">return</span> <span class="n">mock1</span><span class="p">,</span> <span class="n">mock2</span> |
| <span class="gp">...</span> |
| <span class="gp">>>> </span><span class="n">mock1</span><span class="p">,</span> <span class="n">mock2</span> <span class="o">=</span> <span class="n">test</span><span class="p">()</span> |
| <span class="gp">>>> </span><span class="n">mock1</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="gp">>>> </span><span class="n">mock2</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s">'bar'</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <p>Note that the decorators are applied from the bottom upwards. This is the |
| standard way that Python applies decorators. The order of the created mocks |
| passed into your test function matches this order.</p> |
| <p>Like all context-managers patches can be nested using contextlib’s nested |
| function; <em>every</em> patching will appear in the tuple after “as”:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">nested</span> |
| <span class="gp">>>> </span><span class="k">with</span> <span class="n">nested</span><span class="p">(</span> |
| <span class="gp">... </span> <span class="n">patch</span><span class="p">(</span><span class="s">'package.module.ClassName1'</span><span class="p">),</span> |
| <span class="gp">... </span> <span class="n">patch</span><span class="p">(</span><span class="s">'package.module.ClassName2'</span><span class="p">)</span> |
| <span class="gp">... </span> <span class="p">)</span> <span class="k">as</span> <span class="p">(</span><span class="n">MockClass1</span><span class="p">,</span> <span class="n">MockClass2</span><span class="p">):</span> |
| <span class="gp">... </span> <span class="k">assert</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="gp">... </span> <span class="k">assert</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="gp">...</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="where-to-patch"> |
| <span id="id1"></span><h2>Where to patch<a class="headerlink" href="#where-to-patch" title="Permalink to this headline">¶</a></h2> |
| <p><cite>patch</cite> works by (temporarily) changing the object that a <em>name</em> points to with |
| another one. There can be many names pointing to any individual object, so |
| for patching to work you must ensure that you patch the name used by the system |
| under test.</p> |
| <p>The basic principle is that you patch where an object is <em>looked up</em>, which |
| is not necessarily the same place as where it is defined. A couple of |
| examples will help to clarify this.</p> |
| <p>Imagine we have a project that we want to test with the following structure:</p> |
| <div class="highlight-python"><pre>a.py |
| -> Defines SomeClass |
| |
| b.py |
| -> from a import SomeClass |
| -> some_function instantiates SomeClass</pre> |
| </div> |
| <p>Now we want to test <cite>some_function</cite> but we want to mock out <cite>SomeClass</cite> using |
| <cite>patch</cite>. The problem is that when we import module b, which we will have to |
| do then it imports <cite>SomeClass</cite> from module a. If we use <cite>patch</cite> to mock out |
| <cite>a.SomeClass</cite> then it will have no effect on our test; module b already has a |
| reference to the <em>real</em> <cite>SomeClass</cite> and it looks like our patching had no |
| effect.</p> |
| <p>The key is to patch out <cite>SomeClass</cite> where it is used (or where it is looked up |
| ). In this case <cite>some_function</cite> will actually look up <cite>SomeClass</cite> in module b, |
| where we have imported it. The patching should look like:</p> |
| <blockquote> |
| <div><cite>@patch(‘b.SomeClass’)</cite></div></blockquote> |
| <p>However, consider the alternative scenario where instead of <cite>from a import |
| SomeClass</cite> module b does <cite>import a</cite> and <cite>some_function</cite> uses <cite>a.SomeClass</cite>. Both |
| of these import forms are common. In this case the class we want to patch is |
| being looked up on the a module and so we have to patch <cite>a.SomeClass</cite> instead:</p> |
| <blockquote> |
| <div><cite>@patch(‘a.SomeClass’)</cite></div></blockquote> |
| </div> |
| <div class="section" id="patching-descriptors-and-proxy-objects"> |
| <h2>Patching Descriptors and Proxy Objects<a class="headerlink" href="#patching-descriptors-and-proxy-objects" title="Permalink to this headline">¶</a></h2> |
| <p>Since version 0.6.0 both <a class="reference internal" href="#patch">patch</a> and <a class="reference internal" href="#patch-object">patch.object</a> have been able to correctly |
| patch and restore descriptors: class methods, static methods and properties. |
| You should patch these on the <em>class</em> rather than an instance.</p> |
| <p>Since version 0.7.0 <a class="reference internal" href="#patch">patch</a> and <a class="reference internal" href="#patch-object">patch.object</a> work correctly with some objects |
| that proxy attribute access, like the <a class="reference external" href="http://www.voidspace.org.uk/python/weblog/arch_d7_2010_12_04.shtml#e1198">django setttings object</a>.</p> |
| <div class="admonition note"> |
| <p class="first admonition-title">Note</p> |
| <p class="last">In django <cite>import settings</cite> and <cite>from django.conf import settings</cite> |
| return different objects. If you are using libraries / apps that do both you |
| may have to patch both. Grrr...</p> |
| </div> |
| </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="#">Patch Decorators</a><ul> |
| <li><a class="reference internal" href="#patch">patch</a></li> |
| <li><a class="reference internal" href="#patch-object">patch.object</a></li> |
| <li><a class="reference internal" href="#patch-dict">patch.dict</a></li> |
| <li><a class="reference internal" href="#patch-multiple">patch.multiple</a></li> |
| <li><a class="reference internal" href="#patch-methods-start-and-stop">patch methods: start and stop</a></li> |
| <li><a class="reference internal" href="#test-prefix">TEST_PREFIX</a></li> |
| <li><a class="reference internal" href="#nesting-patch-decorators">Nesting Patch Decorators</a></li> |
| <li><a class="reference internal" href="#where-to-patch">Where to patch</a></li> |
| <li><a class="reference internal" href="#patching-descriptors-and-proxy-objects">Patching Descriptors and Proxy Objects</a></li> |
| </ul> |
| </li> |
| </ul> |
| |
| <h4>Previous topic</h4> |
| <p class="topless"><a href="mock.html" |
| title="previous chapter">The Mock Class</a></p> |
| <h4>Next topic</h4> |
| <p class="topless"><a href="helpers.html" |
| title="next chapter">Helpers</a></p> |
| <h3>This Page</h3> |
| <ul class="this-page-menu"> |
| <li><a href="_sources/patch.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="helpers.html" title="Helpers" |
| >next</a> |</li> |
| <li class="right" > |
| <a href="mock.html" title="The Mock Class" |
| >previous</a> |</li> |
| <li><a href="index.html">Mock 1.0.1 documentation</a> »</li> |
| </ul> |
| </div> |
| <div class="footer"> |
| © Copyright 2007-2012, Michael Foord & the mock team. |
| Last updated on Nov 05, 2012. |
| Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. |
| </div> |
| </body> |
| </html> |