Chapter 1 Installation and Getting Started

3728 ワード

pytestのインストール

  • コマンドラインでは、pip install -U pytest
  • コマンドを実行します.
  • 現在インストールされているバージョンを確認します:pytest --version
  • 最初のテストを作成


    単純な4行のみのテストメソッドを作成
    # content of test_sample.py
    def func(x):
      return x + 1
    
    def test_answer():
      assert func(3) == 5
    

    そうですか.このテスト方法を実行できます.
    $ pytest
    =========================== test session starts ============================
    platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
    rootdir: $REGENDOC_TMPDIR, inifile:
    collected 1 item
    test_sample.py F [100%]
    ================================= FAILURES =================================
    _______________________________ test_answer ________________________________
    def test_answer():
    > assert func(3) == 5
    E assert 4 == 5
    E + where 4 = func(3)
    test_sample.py:5: AssertionError
    ========================= 1 failed in 0.12 seconds =========================
    

    ここでテストは、func(3)が5を返すことができないため、エラーレポートを返します.
    Note:      `assert`        . pytest         
    

    assert式の中間値をインテリジェントにレポートすることで、JUnitに残された多くの名前メソッドを回避できます.

    複数のテストの実行


    pytestは、現在のフォルダとそのサブフォルダ内のすべてのtest_を実行します.py*および*test.py形式のテストより一般的には、standard test discovery rulesを参照してください.

    ある異常を引き起こしたと断言する

    raiseというアシスタントを使用することによって、いくつかのコードが異常を引き起こすと断言します.
    # content of test_sysexit.py
    import pytest
    
    def f():
      raise SystemExit(1)
    
    def test_mytest():
      with pytest.raises(SystemExit):
        f()
    

    サイレントレポートモードによるテスト機能の実行
    $ pytest -q test_sysexit.py
    .                                                                           [100%]
    1 passed in 0.12 seconds
    

    1つのクラスで複数のテスト・インスタンスを実行


    一度に開発した複数のテスト例を1つのクラスに入れたいかもしれません.pytestは、1つ以上のテスト・インスタンスを制御するためにクラスを作成することを簡単にします.
    # content of test_class.py
    class TestClass(object):
      def test_one(self):
        x = 'this'
        assert 'h' in x
    
      def test_two(self):
        x = 'hello'
        assert hasattr(x, 'check')
    

    pyttestはPythonのテスト発見規則に従ってすべてのテスト用例を発見するので、test_の冒頭の方法をすべて見つけることができる.ここではサブクラス化する必要はありません.モジュールファイル名を渡すことで、モジュールを簡単に実行できます.
    $ pytest -q test_class.py
    .F [100%]
    ================================= FAILURES =================================
    ____________________________ TestClass.test_two ____________________________
    self = 
    def test_two(self):
    x = "hello"
    > assert hasattr(x, 'check')
    E AssertionError: assert False
    E + where False = hasattr('hello', 'check')
    test_class.py:8: AssertionError
    1 failed, 1 passed in 0.12 seconds
    

    1つ目の例は「Passed」で、2つ目は「Failed」です.失敗の原因を理解するのに役立つ断言の中間値を簡単に見ることができます.

    機能テストに一意の一時ディレクトリを要求

    # content of test_tmpdir.py
    def test_needsfiles(tmpdir):
      print(tmpdir)
      assert 0
    

    テスト関数署名にtmpdirという名前がリストされ、pytestはfixtureファクトリを検索して呼び出し、テスト関数呼び出しを実行する前のリソースを作成します.Pytestは、テスト呼び出しごとに一意の一時ディレクトリを作成します.
    $ pytest -q test_tmpdir.py
    F [100%]
    ================================= FAILURES =================================
    _____________________________ test_needsfiles ______________________________
    tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
    def test_needsfiles(tmpdir):
    print (tmpdir)
    > assert 0
    E assert 0
    test_tmpdir.py:3: AssertionError
    --------------------------- Captured stdout call ---------------------------
    PYTEST_TMPDIR/test_needsfiles0
    1 failed in 0.12 seconds
    

    テンポラリ・ファイルの処理については、Temporary directories and filesを参照してください.
    pytest --fixtures #