<!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>モジュールやテストファイルの doctest</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="プラグインと conftest ファイルの連携" href="plugins.html" /> <link rel="prev" title="nose 向けに書かれたテストの実行" href="nose.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="plugins.html" title="プラグインと conftest ファイルの連携" accesskey="N">次へ</a> |</li> <li class="right" > <a href="nose.html" title="nose 向けに書かれたテストの実行" accesskey="P">前へ</a> |</li> <li><a href="contents.html">pytest-2.2.4.0</a> »</li> <li><a href="apiref.html" accesskey="U">py.test リファレンスドキュメント</a> »</li> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>前のトピックへ</h4> <p class="topless"><a href="nose.html" title="前の章へ">nose 向けに書かれたテストの実行</a></p> <h4>次のトピックへ</h4> <p class="topless"><a href="plugins.html" title="次の章へ">プラグインと conftest ファイルの連携</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="doctest"> <h1>モジュールやテストファイルの doctest<a class="headerlink" href="#doctest" title="このヘッドラインへのパーマリンク">¶</a></h1> <p>デフォルトで <tt class="docutils literal"><span class="pre">test*.txt</span></tt> のパターンに一致する全てのファイルは、Python 標準の <tt class="docutils literal"><span class="pre">doctest</span></tt> モジュールで実行されます。次のようにコマンドラインでこのパターンを変更できます:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">doctest</span><span class="o">-</span><span class="n">glob</span><span class="o">=</span><span class="s">'*.rst'</span> </pre></div> </div> <p>Python モジュール (通常 python テストモジュールを含む) の docstring からも doctest を実行できます:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">doctest</span><span class="o">-</span><span class="n">modules</span> </pre></div> </div> <p>次のように pytest.ini にその設定を追加することで、自分のプロジェクトでそういった変更を永続化できます:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># pytest.ini の内容</span> <span class="p">[</span><span class="n">pytest</span><span class="p">]</span> <span class="n">addopts</span> <span class="o">=</span> <span class="o">--</span><span class="n">doctest</span><span class="o">-</span><span class="n">modules</span> </pre></div> </div> <p>次のようなテキストファイルが存在して:</p> <div class="highlight-python"><pre># example.rst の内容 hello this is a doctest >>> x = 3 >>> x 3</pre> </div> <p>他にも次のようなファイルも存在するとします:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># mymodule.py の内容</span> <span class="k">def</span> <span class="nf">something</span><span class="p">():</span> <span class="sd">""" a doctest in a docstring</span> <span class="sd"> >>> something()</span> <span class="sd"> 42</span> <span class="sd"> """</span> <span class="k">return</span> <span class="mi">42</span> </pre></div> </div> <p>コマンドラインオプションを指定せず <tt class="docutils literal"><span class="pre">py.test</span></tt> を実行するだけです:</p> <div class="highlight-python"><pre>$ py.test =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 1 items mymodule.py . ========================= 1 passed in 0.02 seconds =========================</pre> </div> <p>それは <tt class="docutils literal"><span class="pre">getfixture</span></tt> ヘルパーを使ってフィクスチャを使用することが可能である:</p> <div class="highlight-python"><pre># content of example.rst >>> tmp = getfixture('tmpdir') >>> ...</pre> </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="plugins.html" title="プラグインと conftest ファイルの連携" >次へ</a> |</li> <li class="right" > <a href="nose.html" title="nose 向けに書かれたテストの実行" >前へ</a> |</li> <li><a href="contents.html">pytest-2.2.4.0</a> »</li> <li><a href="apiref.html" >py.test リファレンスドキュメント</a> »</li> </ul> </div> <div class="footer"> © Copyright 2011, holger krekel et alii. このドキュメントは <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3 で生成しました。 </div> </body> </html>