Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > 7f671eb35339cf812de52087b0d93519 > files > 402

python3-pytest-2.3.5-3.fc18.noarch.rpm



<!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>モンキーパッチ/モックのモジュールと環境</title>
    
    <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '2.2.4.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>
    <script type="text/javascript" src="_static/translations.js"></script>
    <link rel="top" title="None" href="index.html" />
    <link rel="up" title="py.test リファレンスドキュメント" href="apiref.html" />
    <link rel="next" title="xdist: pytest の分散テストプラグイン" href="xdist.html" />
    <link rel="prev" title="標準出力/標準エラーのキャプチャ" href="capture.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>ナビゲーション</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="py-modindex.html" title="Pythonモジュール索引"
             >モジュール</a></li>
        <li class="right" >
          <a href="xdist.html" title="xdist: pytest の分散テストプラグイン"
             accesskey="N">次へ</a> |</li>
        <li class="right" >
          <a href="capture.html" title="標準出力/標準エラーのキャプチャ"
             accesskey="P">前へ</a> |</li>
        <li><a href="contents.html">pytest-2.2.4.0</a> &raquo;</li>
          <li><a href="apiref.html" accesskey="U">py.test リファレンスドキュメント</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="contents.html">目次</a></h3>
  <ul>
<li><a class="reference internal" href="#">モンキーパッチ/モックのモジュールと環境</a><ul>
<li><a class="reference internal" href="#id2">簡単な例: モンキーパッチ機能</a></li>
<li><a class="reference internal" href="#id3">関数の引数 monkeypatch のメソッドリファレンス</a></li>
</ul>
</li>
</ul>

  <h4>前のトピックへ</h4>
  <p class="topless"><a href="capture.html"
                        title="前の章へ">標準出力/標準エラーのキャプチャ</a></p>
  <h4>次のトピックへ</h4>
  <p class="topless"><a href="xdist.html"
                        title="次の章へ">xdist: pytest の分散テストプラグイン</a></p>
<div id="searchbox" style="display: none">
  <h3>クイック検索</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="検索" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    モジュール、クラス、または関数名を入力してください
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="id1">
<h1>モンキーパッチ/モックのモジュールと環境<a class="headerlink" href="#id1" title="このヘッドラインへのパーマリンク">¶</a></h1>
<p>時々、グローバル設定に依存する機能のテストを実行する、またはネットワークアクセスを伴うような簡単にテストできないコードを実行する必要があります。 <tt class="docutils literal"><span class="pre">monkeypatch</span></tt> という関数の引数を使うことで、属性、ディクショナリの項目、環境変数、インポートのための <tt class="docutils literal"><span class="pre">sys.path</span></tt> の変更を安全に追加/削除するのを支援します。入門記事とその動機付けの議論は <a class="reference external" href="http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/">monkeypatch のブログ記事</a> を参照してください。</p>
<div class="section" id="id2">
<h2>簡単な例: モンキーパッチ機能<a class="headerlink" href="#id2" title="このヘッドラインへのパーマリンク">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">os.expanduser</span></tt> が特定のディレクトリを返すようにさせたい場合、関数内で <tt class="docutils literal"><span class="pre">os.expanduser</span></tt> が呼ばれる前にこの関数へパッチを当てるために <a class="reference internal" href="#_pytest.monkeypatch.monkeypatch.setattr" title="_pytest.monkeypatch.monkeypatch.setattr"><tt class="xref py py-meth docutils literal"><span class="pre">monkeypatch.setattr()</span></tt></a> メソッドが使えます:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># test_module.py の内容</span>
<span class="kn">import</span> <span class="nn">os.path</span>
<span class="k">def</span> <span class="nf">getssh</span><span class="p">():</span> <span class="c"># 疑似アプリケーションコード</span>
    <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="s">&quot;~admin&quot;</span><span class="p">),</span> <span class="s">&#39;.ssh&#39;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_mytest</span><span class="p">(</span><span class="n">monkeypatch</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">mockreturn</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;/abc&#39;</span>
    <span class="n">monkeypatch</span><span class="o">.</span><span class="n">setattr</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="p">,</span> <span class="s">&#39;expanduser&#39;</span><span class="p">,</span> <span class="n">mockreturn</span><span class="p">)</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">getssh</span><span class="p">()</span>
    <span class="k">assert</span> <span class="n">x</span> <span class="o">==</span> <span class="s">&#39;/abc/.ssh&#39;</span>
</pre></div>
</div>
<p>このテスト関数は <tt class="docutils literal"><span class="pre">os.path.expanduser</span></tt> にモンキーパッチを当てた後で、ある関数内からその関数が呼ばれます。このテスト関数が終了した後で <tt class="docutils literal"><span class="pre">os.path.expanduser</span></tt> に対する変更は元に戻ります。</p>
</div>
<div class="section" id="id3">
<h2>関数の引数 monkeypatch のメソッドリファレンス<a class="headerlink" href="#id3" title="このヘッドラインへのパーマリンク">¶</a></h2>
<dl class="class">
<dt id="_pytest.monkeypatch.monkeypatch">
<em class="property">class </em><tt class="descclassname">_pytest.monkeypatch.</tt><tt class="descname">monkeypatch</tt><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>object keeping a record of setattr/item/env/syspath changes.</p>
<dl class="method">
<dt id="_pytest.monkeypatch.monkeypatch.setattr">
<tt class="descname">setattr</tt><big>(</big><em>obj</em>, <em>name</em>, <em>value</em>, <em>raising=True</em><big>)</big><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch.setattr"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch.setattr" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>set attribute <tt class="docutils literal"><span class="pre">name</span></tt> on <tt class="docutils literal"><span class="pre">obj</span></tt> to <tt class="docutils literal"><span class="pre">value</span></tt>, by default
raise AttributeEror if the attribute did not exist.</p>
</dd></dl>

<dl class="method">
<dt id="_pytest.monkeypatch.monkeypatch.delattr">
<tt class="descname">delattr</tt><big>(</big><em>obj</em>, <em>name</em>, <em>raising=True</em><big>)</big><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch.delattr"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch.delattr" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>delete attribute <tt class="docutils literal"><span class="pre">name</span></tt> from <tt class="docutils literal"><span class="pre">obj</span></tt>, by default raise
AttributeError it the attribute did not previously exist.</p>
</dd></dl>

<dl class="method">
<dt id="_pytest.monkeypatch.monkeypatch.setitem">
<tt class="descname">setitem</tt><big>(</big><em>dic</em>, <em>name</em>, <em>value</em><big>)</big><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch.setitem"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch.setitem" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>set dictionary entry <tt class="docutils literal"><span class="pre">name</span></tt> to value.</p>
</dd></dl>

<dl class="method">
<dt id="_pytest.monkeypatch.monkeypatch.delitem">
<tt class="descname">delitem</tt><big>(</big><em>dic</em>, <em>name</em>, <em>raising=True</em><big>)</big><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch.delitem"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch.delitem" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>delete <tt class="docutils literal"><span class="pre">name</span></tt> from dict, raise KeyError if it doesn&#8217;t exist.</p>
</dd></dl>

<dl class="method">
<dt id="_pytest.monkeypatch.monkeypatch.setenv">
<tt class="descname">setenv</tt><big>(</big><em>name</em>, <em>value</em>, <em>prepend=None</em><big>)</big><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch.setenv"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch.setenv" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>set environment variable <tt class="docutils literal"><span class="pre">name</span></tt> to <tt class="docutils literal"><span class="pre">value</span></tt>.  if <tt class="docutils literal"><span class="pre">prepend</span></tt>
is a character, read the current environment variable value
and prepend the <tt class="docutils literal"><span class="pre">value</span></tt> adjoined with the <tt class="docutils literal"><span class="pre">prepend</span></tt> character.</p>
</dd></dl>

<dl class="method">
<dt id="_pytest.monkeypatch.monkeypatch.delenv">
<tt class="descname">delenv</tt><big>(</big><em>name</em>, <em>raising=True</em><big>)</big><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch.delenv"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch.delenv" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>delete <tt class="docutils literal"><span class="pre">name</span></tt> from environment, raise KeyError it not exists.</p>
</dd></dl>

<dl class="method">
<dt id="_pytest.monkeypatch.monkeypatch.syspath_prepend">
<tt class="descname">syspath_prepend</tt><big>(</big><em>path</em><big>)</big><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch.syspath_prepend"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch.syspath_prepend" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>prepend <tt class="docutils literal"><span class="pre">path</span></tt> to <tt class="docutils literal"><span class="pre">sys.path</span></tt> list of import locations.</p>
</dd></dl>

<dl class="method">
<dt id="_pytest.monkeypatch.monkeypatch.chdir">
<tt class="descname">chdir</tt><big>(</big><em>path</em><big>)</big><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch.chdir"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch.chdir" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>change the current working directory to the specified path
path can be a string or a py.path.local object</p>
</dd></dl>

<dl class="method">
<dt id="_pytest.monkeypatch.monkeypatch.undo">
<tt class="descname">undo</tt><big>(</big><big>)</big><a class="reference internal" href="_modules/_pytest/monkeypatch.html#monkeypatch.undo"><span class="viewcode-link">[ソース]</span></a><a class="headerlink" href="#_pytest.monkeypatch.monkeypatch.undo" title="この定義へのパーマリンク">¶</a></dt>
<dd><p>undo previous changes.  This call consumes the
undo stack.  Calling it a second time has no effect unless
you  do more monkeypatching after the undo call.</p>
</dd></dl>

</dd></dl>

<p><tt class="docutils literal"><span class="pre">monkeypatch.setattr/delattr/delitem/delenv()</span></tt> の全ての関数において、変更対象が存在しない場合にデフォルトで例外を発生させます。このチェック処理をスキップしたいなら <tt class="docutils literal"><span class="pre">raising=False</span></tt> を渡してください。</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>ナビゲーション</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="py-modindex.html" title="Pythonモジュール索引"
             >モジュール</a></li>
        <li class="right" >
          <a href="xdist.html" title="xdist: pytest の分散テストプラグイン"
             >次へ</a> |</li>
        <li class="right" >
          <a href="capture.html" title="標準出力/標準エラーのキャプチャ"
             >前へ</a> |</li>
        <li><a href="contents.html">pytest-2.2.4.0</a> &raquo;</li>
          <li><a href="apiref.html" >py.test リファレンスドキュメント</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2011, holger krekel et alii.
      このドキュメントは <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3 で生成しました。
    </div>
  </body>
</html>