<!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="skip と xfail: 成功しないテストを扱う" href="skipping.html" /> <link rel="prev" title="xdist: pytest の分散テストプラグイン" href="xdist.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="skipping.html" title="skip と xfail: 成功しないテストを扱う" accesskey="N">次へ</a> |</li> <li class="right" > <a href="xdist.html" title="xdist: pytest の分散テストプラグイン" 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"> <h3><a href="contents.html">目次</a></h3> <ul> <li><a class="reference internal" href="#">一時ディレクトリとファイル</a><ul> <li><a class="reference internal" href="#tmpdir">テスト関数の引数 ‘tmpdir’</a></li> <li><a class="reference internal" href="#base-temporary-directory">デフォルトの一時ディレクトリ</a></li> </ul> </li> </ul> <h4>前のトピックへ</h4> <p class="topless"><a href="xdist.html" title="前の章へ">xdist: pytest の分散テストプラグイン</a></p> <h4>次のトピックへ</h4> <p class="topless"><a href="skipping.html" title="次の章へ">skip と xfail: 成功しないテストを扱う</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="tmpdir-handling"> <span id="id1"></span><h1>一時ディレクトリとファイル<a class="headerlink" href="#tmpdir-handling" title="このヘッドラインへのパーマリンク">¶</a></h1> <div class="section" id="tmpdir"> <h2>テスト関数の引数 ‘tmpdir’<a class="headerlink" href="#tmpdir" title="このヘッドラインへのパーマリンク">¶</a></h2> <p><tt class="docutils literal"><span class="pre">tmpdir</span></tt> という関数の引数を一意な一時ディレクトリを提供するのに使えます。それは <a class="reference internal" href="#base-temporary-directory"><em>デフォルトの一時ディレクトリ</em></a> に作成されます。</p> <p><tt class="docutils literal"><span class="pre">tmpdir</span></tt> は <tt class="docutils literal"><span class="pre">os.path</span></tt> メソッドやさらに他のメソッドを提供する <a class="reference external" href="http://py.rtfd.org/path.html">py.path.local</a> オブジェクトです。次にテストでの使用例を紹介します:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># test_tmpdir.py の内容</span> <span class="kn">import</span> <span class="nn">os</span> <span class="k">def</span> <span class="nf">test_create_file</span><span class="p">(</span><span class="n">tmpdir</span><span class="p">):</span> <span class="n">p</span> <span class="o">=</span> <span class="n">tmpdir</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="s">"sub"</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">"hello.txt"</span><span class="p">)</span> <span class="n">p</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"content"</span><span class="p">)</span> <span class="k">assert</span> <span class="n">p</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="o">==</span> <span class="s">"content"</span> <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">tmpdir</span><span class="o">.</span><span class="n">listdir</span><span class="p">())</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">assert</span> <span class="mi">0</span> </pre></div> </div> <p>このテストを実行すると、最終行の <tt class="docutils literal"><span class="pre">assert</span> <span class="pre">0</span></tt> が失敗して <tt class="docutils literal"><span class="pre">tmpdir</span></tt> の値が見えます:</p> <div class="highlight-python"><pre>$ py.test test_tmpdir.py =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 1 items test_tmpdir.py F ================================= FAILURES ================================= _____________________________ test_create_file _____________________________ tmpdir = local('/tmp/pytest-23/test_create_file0') def test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") p.write("content") assert p.read() == "content" assert len(tmpdir.listdir()) == 1 > assert 0 E assert 0 test_tmpdir.py:7: AssertionError ========================= 1 failed in 0.02 seconds =========================</pre> </div> </div> <div class="section" id="base-temporary-directory"> <span id="id2"></span><h2>デフォルトの一時ディレクトリ<a class="headerlink" href="#base-temporary-directory" title="このヘッドラインへのパーマリンク">¶</a></h2> <p>デフォルトでは、テスト向けの一時ディレクトリは、システムの一時ディレクトリのサブディレクトリとして作成されます。基本となる名前は <tt class="docutils literal"><span class="pre">pytest-NUM</span></tt> となり <tt class="docutils literal"><span class="pre">NUM</span></tt> はテストが実行される度に数字が増えます。また、3世代より古い一時ディレクトリは削除されます。</p> <p>デフォルトの一時ディレクトリの設定は次のように書き換えられます:</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">basetemp</span><span class="o">=</span><span class="n">mydir</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">py.test</span></tt> は、ローカルマシン上で分散テストを行うとき、全ての一時データが basetemp ディレクトリの配下で実行されてテスト毎に一意になるよう、サブプロセスに対しても basetemp ディレクトリをちゃんと設定します。</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="skipping.html" title="skip と xfail: 成功しないテストを扱う" >次へ</a> |</li> <li class="right" > <a href="xdist.html" title="xdist: pytest の分散テストプラグイン" >前へ</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>