pytest -v ./pytest_scripts/test_script_001.py
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.12.8/x64/bin/pytest", line 5, in <module>
from pytest import console_main
ModuleNotFoundError: No module named 'pytest'
Pythonが提供するユニットテストフレームワークとして, unittest
がありますが,unittest
と比べpytest
は以下のようなメリットがあります.
assert
文がシンプルassert
文pytest
を用いたunit testの基本構文を理解するために以下のコードを見てみます.
def test_passing():
assert (1, 2, 3) == (1, 2, 3)
assert
文はテストの成否を判断するPythonの組み込みコマンドです.assert
以下がFalse
と評価されると,AssertionError
を発生させます.
test_passing
という関数の名前はtest_
から始まってますが,pytest
はtest_
で始まる関数をテスト関数として検出しテストを実行します.
Example 1 : test_script_001.py
def test_passing():
assert (1, 2, 3) == (1, 2, 3)
def test_passing2():
assert (2, 3, 4) == (2, 3, 4)
def nottest_passing():
assert (1, 2, 3) == (1, 2, 3)
というpytest用のファイルを用意します.これを実行すると
pytest -v ./pytest_scripts/test_script_001.py
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.12.8/x64/bin/pytest", line 5, in <module>
from pytest import console_main
ModuleNotFoundError: No module named 'pytest'
上記の例では,
collecting ... collected 2 items
よりテスト関数が2つ検出されたtest_passing
, test_passing2
が問題なくテストパスしたという情報が表示されています.
▶ テストが失敗する場合
def test_failing():
assert (1, 2) == (1, 1)
という関数を準備し,あえて失敗させてみます.
pytest -v ./pytest_scripts/test_script_002.py
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.12.8/x64/bin/pytest", line 5, in <module>
from pytest import console_main
ModuleNotFoundError: No module named 'pytest'
At index 1 diff: 2 != 1
とあるように index 1が一致していないと教えてくれます.pytestが表示するテストが失敗した場所とその周囲のコードのセクションのことをトレースバック(traceback)と呼びます.これの表示をoffにしたい場合は
pytest --tb=no ./pytest_scripts/
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.12.8/x64/bin/pytest", line 5, in <module>
from pytest import console_main
ModuleNotFoundError: No module named 'pytest'
pytest
が利用するassert
はPythonの組み込みstatementと同じですが,unittest
などの他のフレームワークのassert
ヘルパー関数と比べシンプルに利用することができます. 例として,
pytest | unittest |
---|---|
assert something |
assertTrue(something) |
assert a == b |
assertEqual(a, b) |
assert a != b |
assertNotEqual(a, b) |
assert a <= b |
assertLessEual(a, b) |
assert a is None |
assertIsNone(a) |
assert a is not None |
assertIsNotNone(a) |
pytest_scripts/test_script_001.py .. [ 66%]
pytest_scripts/test_script_002.py F [100%]
に着目すると ..
, F
といった文字列が確認できます.これはpytest
のテスト結果を表しています.
結果 | 表示 | 説明 |
---|---|---|
PASSED | . |
正常に実行された個数を. の個数で示す |
FAILED | F |
正常に実行されなかった個数をF の個数で示す |
SKIPPED | s |
スキップされたテストを示す |
XFAIL | x |
想定通り失敗したテストを示す.@pytest.mark.xfail() でpytest に教える |
XPASS | X |
xfail マーカーを設定したのに成功したテストを示す |
ERROR | E |
例外が想定通り発生したことを示す |