Pythons: Python 2.4-3.2, Jython, PyPy
Platforms : Unix/Posix と Windows
PyPI パッケージ名 : pytest
PDF ドキュメント : 最新をダウンロード
インストールオプション:
pip install -U pytest # or
easy_install -U pytest
インストール後に適切なバージョンかを確認するには、次のように実行します:
$ 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
エラーが発生したら インストールに関する既知の問題 を確認してください。
簡単なテスト関数を含む最初のテストファイルを作りましょう:
# test_sample.py の内容
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
こんな感じです。さあ、テスト関数を実行しましょう:
$ 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():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:5: AssertionError
========================= 1 failed in 0.01 seconds =========================
py.test は 標準的なテスト探索ルール に従い test_answer 関数を検出します。基本的には test_ の接頭辞をもつファイルや関数です。先ほど作成した func(3) 呼び出しが 5 を返さなかったという失敗レポートを受け取りました。
注釈
テストの期待値をアサートするには単純に assert 文を使います。pytest の 高度なアサートイントロスペクション は assert 評価時の中間値を賢くレポートします。これにより、多くの JUnit レガシーメソッド の名前を覚える必要がなくなります。
例外を発生させるコードをテストしたいなら raises ヘルパー関数を使います:
# test_sysexit.py の内容
import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
f()
このコードを “quiet” モードで実行します:
$ py.test -q test_sysexit.py
collecting ... collected 1 items
.
1 passed in 0.00 seconds
テストを書き始めて何個か作成したら、クラスやモジュール内にそういったテストをグループ化すると分かりやすくなります。2つのテストを含むクラスを作成しましょう:
# test_class.py の内容
class TestClass:
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
標準的なテスト探索ルール により、2つのテストが検出されました。サブクラス化する必要はありません。単純にそのファイル名を与えることで、対象のモジュールを実行できます:
$ py.test -q test_class.py
collecting ... collected 2 items
.F
================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________
self = <test_class.TestClass instance at 0x1a956c8>
def test_two(self):
x = "hello"
> assert hasattr(x, 'check')
E assert hasattr('hello', 'check')
test_class.py:8: AssertionError
1 failed, 1 passed in 0.01 seconds
最初のテストは成功し、2番目のテストは失敗しました。また、失敗した原因を理解しやすいよう、このアサーションの中間値がぱっと見て分かります。
機能テストでは、ファイルを作成して、アプリケーションのオブジェクトをそのファイルに書き込むようなことがよくあります。py.test は、1つだけ存在する一時ディレクトリといった、任意のリソース要求を扱う万能の 関数の引数を使った依存性の注入 を提供します:
# test_tmpdir.py の内容
def test_needsfiles(tmpdir):
print tmpdir
assert 0
テスト関数のシグネチャに tmpdir という名前を含めます。py.test はその名前を見つけ、テスト関数が呼び出される前にリソースを作成するファクトリー関数を呼び出します。では、実行してみましょう:
$ 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
> assert 0
E assert 0
test_tmpdir.py:3: AssertionError
----------------------------- Captured stdout ------------------------------
/tmp/pytest-22/test_needsfiles0
1 failed in 0.01 seconds
テストを実行する毎に、そのテスト関数の実行前に一時ディレクトリが作成されました。さらに詳細は 一時ディレクトリとファイル を参照してください。
組み込みの 関数の引数を使った依存性の注入 を把握するには、次のコマンドを実行します:
py.test --fixtures # 組み込み/カスタムの関数の引数を表示する
次のドキュメントを見てましょう:
最先端の Python パッケージインストーラーである pip をインストール してください。
もしくは easy_install ツールをインストールするために distribute docs を読んでください。
旧来の setuptools プロジェクトも使えますが、それはバグ修正が行われてなく Python 3 でも動作しません。
複雑な例は 使用方法と例 を参照してください。