
pytestのmark.parametrizeでテストケースを識別しやすくする
はじめに
データアナリティクス事業本部のkobayashiです。
Pytestに関する小ネタです。
以前Pytestでmark.parametrizeを使ってサブテストを行う場合にテスをケースを区別するためにはmark.parametrizeでidを使ってテストケース名を区別することで視認性が良くなるという記事を書きました。
pytestのプラグインを漁っていたところpluginを使って簡単に識別できるものがあったので紹介します。
mark.parametrizeに名前をつける
以前書いたこちらの記事ではmark.parametrizeで行うサブテストに名前を付けるためにid
パラメータにテストケース名を指定して区別をする方法を紹介しました。
この方法ですと明示的にテストケース名をつける事ができテスト結果を確認するときにどのテストケースが成功・失敗したのかが非常にわかりやすくなっていました。
これを簡素化するためにpytest-verbose-parametrize
を使うとidをつけることなくテストケースが区別できるようになります。
pytest-verbose-parametrizeを使ってみる
それでは早速pytest-verbose-parametrizeを使って効果を確かめてみます。
はじめに以前の記事で使った以下のコードを使ってpytestを実行してみます。
def add_sub(x, y):
return [x + y, x - y]
import pytest
from main import add_sub
@pytest.mark.parametrize(
["data_in", "expected_data"],
[
pytest.param([1, 2], [3, -1]),
pytest.param([0, 0], [0, 0]),
pytest.param([-1, -2], [-3, 1]),
pytest.param([1, -2], [-1, 3]),
pytest.param([-1, 2], [1, -3]),
pytest.param([200, 800], [1000, -600]),
],
)
def test_add_sub(data_in, expected_data):
assert add_sub(data_in[0], data_in[1]) == expected_data
pytest-verbose-parametrizeを使わない状態では以下のように実行した順番でindex番号を付与された名前でテスト結果が返ってきます。
$ pytest -v
platform darwin -- Python 3.11.4, pytest-7.4.3, pluggy-1.3.0 -- /Users/kobayashi.masahiro/CMProject/developers.io/developers.io_22/venv/bin/python
cachedir: .pytest_cache
rootdir: /Users/kobayashi.masahiro/CMProject/developers.io/developers.io_22/devio_22/pytest-param
plugins: xdist-3.5.0, anyio-3.7.0
collected 6 items
test_main.py::test_add_sub[data_in0-expected_data0] PASSED [ 16%]
test_main.py::test_add_sub[data_in1-expected_data1] PASSED [ 33%]
test_main.py::test_add_sub[data_in2-expected_data2] PASSED [ 50%]
test_main.py::test_add_sub[data_in3-expected_data3] PASSED [ 66%]
test_main.py::test_add_sub[data_in4-expected_data4] PASSED [ 83%]
test_main.py::test_add_sub[data_in5-expected_data5] PASSED [100%]
それではpipでpytest-verbose-parametrizeをインストールしてからテストを実行してみます。
$ pip install pytest-verbose-parametrize
$ pytest -v
platform darwin -- Python 3.11.4, pytest-7.4.3, pluggy-1.3.0 -- /Users/kobayashi.masahiro/CMProject/developers.io/developers.io_22/venv/bin/python
cachedir: .pytest_cache
hypothesis profile 'default'
rootdir: /Users/kobayashi.masahiro/CMProject/developers.io/developers.io_22/devio_22/pytest-param
plugins: anyio-4.9.0, hypothesis-6.135.16, verbose-parametrize-1.8.1, xdist-3.5.0, base-url-2.1.0, mock-3.14.0, playwright-0.5.2
collected 6 items
test_main.py::test_add_sub[([1, 2], [3, -1])-()-None] PASSED [ 16%]
test_main.py::test_add_sub[([0, 0], [0, 0])-()-None] PASSED [ 33%]
test_main.py::test_add_sub[([-1, -2], [-3, 1])-()-None] PASSED [ 50%]
test_main.py::test_add_sub[([1, -2], [-1, 3])-()-None] PASSED [ 66%]
test_main.py::test_add_sub[([-1, 2], [1, -3])-()-None] PASSED [ 83%]
test_main.py::test_add_sub[([200, 800], [1000, -600])-()-None] PASSED [100%]
このようにparametrizeで指定したパラメータがテスト名として返ってくることがわかります。これによりIDを明示的に指定しなくても何のパラメータを指定したテストが失敗したのかが簡単にわかるようになります。
まとめ
pytestでmark.parametrizeにidを指定しなくても簡単にテスト結果から失敗したテストケースが判断できるようになるpytest-verbose-parametrizeのプラグインを使ってみました。
本来はテストケース毎にidをきちんと付けることが大切ですが、つけ忘れてしまった場合などの代替手段としてpytest-verbose-parametrizeを併用するのは効果的だと思いました。
最後まで読んで頂いてありがとうございました。