Sophie

Sophie

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

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="基本から始めよう" href="overview.html" />
    <link rel="next" title="使用方法とテスト実行" href="usage.html" />
    <link rel="prev" title="Pytest へようこそ!" href="index.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="usage.html" title="使用方法とテスト実行"
             accesskey="N">次へ</a> |</li>
        <li class="right" >
          <a href="index.html" title="Pytest へようこそ!"
             accesskey="P">前へ</a> |</li>
        <li><a href="contents.html">pytest-2.2.4.0</a> &raquo;</li>
          <li><a href="overview.html" accesskey="U">基本から始めよう</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="#getstarted">インストール</a></li>
<li><a class="reference internal" href="#simpletest">初めてのテスト実行</a></li>
<li><a class="reference internal" href="#id5">特定の例外が発生したことをアサートする</a></li>
<li><a class="reference internal" href="#id6">1つのクラスで複数のテストをグループ化する</a></li>
<li><a class="reference internal" href="#id7">機能テスト: 一時ディレクトリの要求</a></li>
<li><a class="reference internal" href="#id8">次に学ぶこと</a></li>
<li><a class="reference internal" href="#installation-issues">インストールに関する既知の問題</a><ul>
<li><a class="reference internal" href="#easy-install-pip">easy_install や pip が見つかりません</a></li>
<li><a class="reference internal" href="#windows-py-test">インストールしたのに Windows 上で py.test が見つかりません</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>前のトピックへ</h4>
  <p class="topless"><a href="index.html"
                        title="前の章へ">Pytest へようこそ!</a></p>
  <h4>次のトピックへ</h4>
  <p class="topless"><a href="usage.html"
                        title="次の章へ">使用方法とテスト実行</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><strong>Pythons</strong>: Python 2.4-3.2, Jython, PyPy</p>
<p><strong>Platforms</strong> : Unix/Posix と Windows</p>
<p><strong>PyPI パッケージ名</strong> : <a class="reference external" href="http://pypi.python.org/pypi/pytest">pytest</a></p>
<p><strong>PDF ドキュメント</strong> : <a class="reference external" href="http://pytest.org/latest/pytest.pdf">最新をダウンロード</a></p>
<div class="section" id="getstarted">
<span id="id3"></span><h2>インストール<a class="headerlink" href="#getstarted" title="このヘッドラインへのパーマリンク">¶</a></h2>
<p>インストールオプション:</p>
<div class="highlight-python"><pre>pip install -U pytest # or
easy_install -U pytest</pre>
</div>
<p>インストール後に適切なバージョンかを確認するには、次のように実行します:</p>
<div class="highlight-python"><pre>$ py.test --version
This is py.test version 2.2.4, imported from /home/hpk/p/pytest/pytest.py
setuptools registered plugins:
  pytest-xdist-1.8 at /home/hpk/p/pytest-xdist/xdist/plugin.pyc</pre>
</div>
<p>エラーが発生したら <a class="reference internal" href="#installation-issues"><em>インストールに関する既知の問題</em></a> を確認してください。</p>
</div>
<div class="section" id="simpletest">
<span id="id4"></span><h2>初めてのテスト実行<a class="headerlink" href="#simpletest" title="このヘッドラインへのパーマリンク">¶</a></h2>
<p>簡単なテスト関数を含む最初のテストファイルを作りましょう:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># test_sample.py の内容</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>

<span class="k">def</span> <span class="nf">test_answer</span><span class="p">():</span>
    <span class="k">assert</span> <span class="n">func</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="mi">5</span>
</pre></div>
</div>
<p>こんな感じです。さあ、テスト関数を実行しましょう:</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

test_sample.py F

================================= FAILURES =================================
_______________________________ test_answer ________________________________

    def test_answer():
&gt;       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:5: AssertionError
========================= 1 failed in 0.01 seconds =========================</pre>
</div>
<p>py.test は <a class="reference internal" href="goodpractises.html#test-discovery"><em>標準的なテスト探索ルール</em></a> に従い <tt class="docutils literal"><span class="pre">test_answer</span></tt> 関数を検出します。基本的には <tt class="docutils literal"><span class="pre">test_</span></tt> の接頭辞をもつファイルや関数です。先ほど作成した <tt class="docutils literal"><span class="pre">func(3)</span></tt> 呼び出しが <tt class="docutils literal"><span class="pre">5</span></tt> を返さなかったという失敗レポートを受け取りました。</p>
<div class="admonition note">
<p class="first admonition-title">ノート</p>
<p class="last">テストの期待値をアサートするには単純に <tt class="docutils literal"><span class="pre">assert</span></tt> 文を使います。pytest の <a class="reference internal" href="assert.html#assert-introspection"><em>高度なアサートイントロスペクション</em></a> は assert 評価時の中間値を賢くレポートします。これにより、多くの <a class="reference external" href="http://docs.python.org/library/unittest.html#test-cases">JUnit レガシーメソッド</a> の名前を覚える必要がなくなります。</p>
</div>
</div>
<div class="section" id="id5">
<h2>特定の例外が発生したことをアサートする<a class="headerlink" href="#id5" title="このヘッドラインへのパーマリンク">¶</a></h2>
<p>例外を発生させるコードをテストしたいなら <tt class="docutils literal"><span class="pre">raises</span></tt> ヘルパー関数を使います:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># test_sysexit.py の内容</span>
<span class="kn">import</span> <span class="nn">pytest</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
    <span class="k">raise</span> <span class="ne">SystemExit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">test_mytest</span><span class="p">():</span>
    <span class="k">with</span> <span class="n">pytest</span><span class="o">.</span><span class="n">raises</span><span class="p">(</span><span class="ne">SystemExit</span><span class="p">):</span>
        <span class="n">f</span><span class="p">()</span>
</pre></div>
</div>
<p>このコードを &#8220;quiet&#8221; モードで実行します:</p>
<div class="highlight-python"><pre>$ py.test -q test_sysexit.py
collecting ... collected 1 items
.
1 passed in 0.00 seconds</pre>
</div>
</div>
<div class="section" id="id6">
<h2>1つのクラスで複数のテストをグループ化する<a class="headerlink" href="#id6" title="このヘッドラインへのパーマリンク">¶</a></h2>
<p>テストを書き始めて何個か作成したら、クラスやモジュール内にそういったテストをグループ化すると分かりやすくなります。2つのテストを含むクラスを作成しましょう:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># test_class.py の内容</span>
<span class="k">class</span> <span class="nc">TestClass</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">test_one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="s">&quot;this&quot;</span>
        <span class="k">assert</span> <span class="s">&#39;h&#39;</span> <span class="ow">in</span> <span class="n">x</span>

    <span class="k">def</span> <span class="nf">test_two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="s">&quot;hello&quot;</span>
        <span class="k">assert</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s">&#39;check&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p><a class="reference internal" href="goodpractises.html#test-discovery"><em>標準的なテスト探索ルール</em></a> により、2つのテストが検出されました。サブクラス化する必要はありません。単純にそのファイル名を与えることで、対象のモジュールを実行できます:</p>
<div class="highlight-python"><pre>$ py.test -q test_class.py
collecting ... collected 2 items
.F
================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________

self = &lt;test_class.TestClass instance at 0x1a956c8&gt;

    def test_two(self):
        x = "hello"
&gt;       assert hasattr(x, 'check')
E       assert hasattr('hello', 'check')

test_class.py:8: AssertionError
1 failed, 1 passed in 0.01 seconds</pre>
</div>
<p>最初のテストは成功し、2番目のテストは失敗しました。また、失敗した原因を理解しやすいよう、このアサーションの中間値がぱっと見て分かります。</p>
</div>
<div class="section" id="id7">
<h2>機能テスト: 一時ディレクトリの要求<a class="headerlink" href="#id7" title="このヘッドラインへのパーマリンク">¶</a></h2>
<p>機能テストでは、ファイルを作成して、アプリケーションのオブジェクトをそのファイルに書き込むようなことがよくあります。py.test は、1つだけ存在する一時ディレクトリといった、任意のリソース要求を扱う万能の <a class="reference internal" href="funcargs.html#funcarg-mechanism"><em>関数の引数を使った依存性の注入</em></a> を提供します:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># test_tmpdir.py の内容</span>
<span class="k">def</span> <span class="nf">test_needsfiles</span><span class="p">(</span><span class="n">tmpdir</span><span class="p">):</span>
    <span class="k">print</span> <span class="n">tmpdir</span>
    <span class="k">assert</span> <span class="mi">0</span>
</pre></div>
</div>
<p>テスト関数のシグネチャに <tt class="docutils literal"><span class="pre">tmpdir</span></tt> という名前を含めます。py.test はその名前を見つけ、テスト関数が呼び出される前にリソースを作成するファクトリー関数を呼び出します。では、実行してみましょう:</p>
<div class="highlight-python"><pre>$ py.test -q test_tmpdir.py
collecting ... collected 1 items
F
================================= FAILURES =================================
_____________________________ test_needsfiles ______________________________

tmpdir = local('/tmp/pytest-22/test_needsfiles0')

    def test_needsfiles(tmpdir):
        print tmpdir
&gt;       assert 0
E       assert 0

test_tmpdir.py:3: AssertionError
----------------------------- Captured stdout ------------------------------
/tmp/pytest-22/test_needsfiles0
1 failed in 0.01 seconds</pre>
</div>
<p>テストを実行する毎に、そのテスト関数の実行前に一時ディレクトリが作成されました。さらに詳細は <a class="reference internal" href="tmpdir.html#tmpdir-handling"><em>一時ディレクトリとファイル</em></a> を参照してください。</p>
<p>組み込みの <a class="reference internal" href="funcargs.html#id1"><em>関数の引数を使った依存性の注入</em></a> を把握するには、次のコマンドを実行します:</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">fixtures</span>   <span class="c"># 組み込み/カスタムの関数の引数を表示する</span>
</pre></div>
</div>
</div>
<div class="section" id="id8">
<h2>次に学ぶこと<a class="headerlink" href="#id8" title="このヘッドラインへのパーマリンク">¶</a></h2>
<p>次のドキュメントを見てましょう:</p>
<ul class="simple">
<li><a class="reference internal" href="usage.html#cmdline"><em>python -m pytest による pytest 呼び出し</em></a>: コマンドラインの実行方法のサンプル</li>
<li><a class="reference internal" href="goodpractises.html#goodpractises"><em>優れたプラクティス</em></a>: virtualenv、テストレイアウト、genscript の対応</li>
<li><a class="reference internal" href="apiref.html#apiref"><em>py.test リファレンスドキュメント</em></a>: ドキュメントと py.test を使う上でのサンプル</li>
<li><a class="reference internal" href="plugins.html#plugins"><em>プラグインと conftest ファイルの連携</em></a>: プラグインの管理と作成</li>
</ul>
</div>
<div class="section" id="installation-issues">
<span id="id9"></span><h2>インストールに関する既知の問題<a class="headerlink" href="#installation-issues" title="このヘッドラインへのパーマリンク">¶</a></h2>
<div class="section" id="easy-install-pip">
<h3>easy_install や pip が見つかりません<a class="headerlink" href="#easy-install-pip" title="このヘッドラインへのパーマリンク">¶</a></h3>
<p>最先端の Python パッケージインストーラーである <a class="reference external" href="http://www.pip-installer.org/en/latest/index.html">pip をインストール</a> してください。</p>
<p>もしくは <tt class="docutils literal"><span class="pre">easy_install</span></tt> ツールをインストールするために <a class="reference external" href="http://pypi.python.org/pypi/distribute">distribute docs</a> を読んでください。</p>
<p>旧来の <a class="reference external" href="http://pypi.python.org/pypi/setuptools">setuptools</a> プロジェクトも使えますが、それはバグ修正が行われてなく Python 3 でも動作しません。</p>
</div>
<div class="section" id="windows-py-test">
<h3>インストールしたのに Windows 上で py.test が見つかりません<a class="headerlink" href="#windows-py-test" title="このヘッドラインへのパーマリンク">¶</a></h3>
<ul class="simple">
<li><strong>Windows</strong>: &#8220;easy_install&#8221; または &#8220;py.test&#8221; が見つからないなら、 <tt class="docutils literal"><span class="pre">PATH</span></tt> にそれらの Python スクリプトを追加する必要があります。 <a class="reference external" href="http://www.imladris.com/Scripts/PythonForWindows.html">Python for Windows</a> を参照してください。別の方法として、自動的にパス設定を行ってくれる <a class="reference external" href="http://www.activestate.com/activepython/downloads">ActivePython install</a> を使うこともできます。</li>
</ul>
<ul class="simple">
<li><strong>Windows XP 上の Jython2.5.1</strong>: <a class="reference external" href="http://bugs.jython.org/issue1491">Jython はコマンドラインランチャーを作らない</a> ので <tt class="docutils literal"><span class="pre">py.test</span></tt> は正常に動作しません。CPython 上に py.test をインストールして <tt class="docutils literal"><span class="pre">py.test</span> <span class="pre">--genscript=mytest</span></tt> を実行すると、Jython で行うテストを py.test で実行する <tt class="docutils literal"><span class="pre">jython</span> <span class="pre">mytest</span></tt> が使えます。</li>
</ul>
<blockquote>
<div>複雑な例は <a class="reference internal" href="example/index.html#examples"><em>使用方法と例</em></a> を参照してください。</div></blockquote>
</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="usage.html" title="使用方法とテスト実行"
             >次へ</a> |</li>
        <li class="right" >
          <a href="index.html" title="Pytest へようこそ!"
             >前へ</a> |</li>
        <li><a href="contents.html">pytest-2.2.4.0</a> &raquo;</li>
          <li><a href="overview.html" >基本から始めよう</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>