<!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>標準的な (Python) テスト探索の変更</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="使用方法と例" href="index.html" /> <link rel="next" title="Python 以外のテストを扱う" href="nonpython.html" /> <link rel="prev" title="カスタムマーカーを使う" href="markers.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="nonpython.html" title="Python 以外のテストを扱う" accesskey="N">次へ</a> |</li> <li class="right" > <a href="markers.html" title="カスタムマーカーを使う" accesskey="P">前へ</a> |</li> <li><a href="../contents.html">pytest-2.2.4.0</a> »</li> <li><a href="index.html" accesskey="U">使用方法と例</a> »</li> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">目次</a></h3> <ul> <li><a class="reference internal" href="#">標準的な (Python) テスト探索の変更</a><ul> <li><a class="reference internal" href="#id1">ディレクトリの再帰探索の変更</a></li> <li><a class="reference internal" href="#change-naming-conventions">命名規則の変更</a></li> <li><a class="reference internal" href="#id3">Python パッケージとしてコマンドライン引数を解釈</a></li> <li><a class="reference internal" href="#id4">コレクションの探索</a></li> </ul> </li> </ul> <h4>前のトピックへ</h4> <p class="topless"><a href="markers.html" title="前の章へ">カスタムマーカーを使う</a></p> <h4>次のトピックへ</h4> <p class="topless"><a href="nonpython.html" title="次の章へ">Python 以外のテストを扱う</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="python"> <h1>標準的な (Python) テスト探索の変更<a class="headerlink" href="#python" title="このヘッドラインへのパーマリンク">¶</a></h1> <div class="section" id="id1"> <h2>ディレクトリの再帰探索の変更<a class="headerlink" href="#id1" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>ini ファイルで <a class="reference internal" href="../customize.html#confval-norecursedirs"><tt class="xref std std-confval docutils literal"><span class="pre">norecursedirs</span></tt></a> オプションを設定できます。例えば、プロジェクトのルートディレクトリにある <tt class="docutils literal"><span class="pre">setup.cfg</span></tt> に設定します:</p> <div class="highlight-python"><pre># setup.cfg の内容 [pytest] norecursedirs = .svn _build tmp*</pre> </div> <p>これは典型的な subversion と sphinx の build ディレクトリと <tt class="docutils literal"><span class="pre">tmp</span></tt> という接頭辞をもつディレクトリを再帰探索しない設定です。</p> </div> <div class="section" id="change-naming-conventions"> <span id="id2"></span><h2>命名規則の変更<a class="headerlink" href="#change-naming-conventions" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><a class="reference internal" href="../customize.html#confval-python_files"><tt class="xref std std-confval docutils literal"><span class="pre">python_files</span></tt></a>, <a class="reference internal" href="../customize.html#confval-python_classes"><tt class="xref std std-confval docutils literal"><span class="pre">python_classes</span></tt></a>, <a class="reference internal" href="../customize.html#confval-python_functions"><tt class="xref std std-confval docutils literal"><span class="pre">python_functions</span></tt></a> オプションを設定することで別の命名規則を使うこともできます。サンプルを紹介します:</p> <div class="highlight-python"><pre># setup.cfg の内容 # tox.ini または pytest.init ファイルでも定義できる [pytest] python_files=check_*.py python_classes=Check python_functions=check</pre> </div> <p>この設定は Python ファイル名に <tt class="docutils literal"><span class="pre">check_</span></tt> 、 クラス名に <tt class="docutils literal"><span class="pre">Check</span></tt> 、関数名に <tt class="docutils literal"><span class="pre">check</span></tt> という接頭辞を py.test が探すようにします。例えば、次のようなファイルです:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># check_myapp.py の内容</span> <span class="k">class</span> <span class="nc">CheckMyApp</span><span class="p">:</span> <span class="k">def</span> <span class="nf">check_simple</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span> <span class="k">def</span> <span class="nf">check_complex</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span> </pre></div> </div> <p>テストコレクションは次のようになります:</p> <div class="highlight-python"><pre>$ py.test --collectonly =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 2 items <Module 'check_myapp.py'> <Class 'CheckMyApp'> <Instance '()'> <Function 'check_simple'> <Function 'check_complex'> ============================= in 0.00 seconds =============================</pre> </div> </div> <div class="section" id="id3"> <h2>Python パッケージとしてコマンドライン引数を解釈<a class="headerlink" href="#id3" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>py.test がファイルシステムのパスから Python パッケージ名として引数を解釈するように <tt class="docutils literal"><span class="pre">--pyargs</span></tt> オプションを使えます。例えば、unittest2 をインストール済みなら、次のように指定できます:</p> <div class="highlight-python"><pre>py.test --pyargs unittest2.test.test_skipping -q</pre> </div> <p>それぞれのテストモジュールを実行します。その他のオプションと同様に ini ファイルと <a class="reference internal" href="../customize.html#confval-addopts"><tt class="xref std std-confval docutils literal"><span class="pre">addopts</span></tt></a> オプションにより、この変更を永続化できます:</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">pyargs</span> </pre></div> </div> <p>単純に <tt class="docutils literal"><span class="pre">py.test</span> <span class="pre">NAME</span></tt> を実行すると、NAME がインポート可能なパッケージ/モジュールとして存在しているかどうかをチェックします。存在しない場合、ファイルシステム上のパスとして NAME を扱います。</p> </div> <div class="section" id="id4"> <h2>コレクションの探索<a class="headerlink" href="#id4" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>次のようにテストを実行せずにコレクションツリーをピークできます:</p> <div class="highlight-python"><pre>. $ py.test --collectonly pythoncollection.py =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 3 items <Module 'pythoncollection.py'> <Function 'test_function'> <Class 'TestClass'> <Instance '()'> <Function 'test_method'> <Function 'test_anothermethod'> ============================= in 0.00 seconds =============================</pre> </div> </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="nonpython.html" title="Python 以外のテストを扱う" >次へ</a> |</li> <li class="right" > <a href="markers.html" title="カスタムマーカーを使う" >前へ</a> |</li> <li><a href="../contents.html">pytest-2.2.4.0</a> »</li> <li><a href="index.html" >使用方法と例</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>