🦁
pytest のパラメーター化テストに日本語の ID を設定する方法
はじめに
この記事では pytest のパラメーター化テストで ID を指定する方法と、その ID に日本語を使うための設定について説明します。
ID を設定する方法
pytest のパラメーター化テストに ID を指定する方法は2つあります。
- pytest.mark.parametrize の ids を利用する
- pytest.param の id を利用する
import pytest
@pytest.mark.parametrize("num", [1, 2, 3], ids=["test1", "test2", "test3"])
def test_parametrize_ids(num):
assert isinstance(num, int)
@pytest.mark.parametrize(
"num",
[
pytest.param(1, id="test1"),
pytest.param(2, id="test2"),
pytest.param(3, id="test3"),
],
)
def test_param_id(num):
assert isinstance(num, int)
テストを実行する際に -v オプションを指定すると実行結果に ID が表示されるようになります。
$ pytest -v sample.py
==================================================================== test session starts ====================================================================
platform darwin -- Python 3.13.7, pytest-8.4.1, pluggy-1.6.0 -- /path/to/dir/.venv/bin/python
cachedir: .pytest_cache
rootdir: /path/to/dir
configfile: pyproject.toml
plugins: anyio-4.10.0, mock-3.14.1
collected 6 items
sample.py::test_parametrize_ids[test1] PASSED [ 16%]
sample.py::test_parametrize_ids[test2] PASSED [ 33%]
sample.py::test_parametrize_ids[test3] PASSED [ 50%]
sample.py::test_param_id[test1] PASSED [ 66%]
sample.py::test_param_id[test2] PASSED [ 83%]
sample.py::test_param_id[test3] PASSED [100%]
===================================================================== 6 passed in 0.01s =====================================================================
日本語の ID を指定する
単に日本語を指定するだけでは以下のように Unicode エスケープされてしまうので何が何だか分かりません。
sample.py::test_parametrize_ids[\u30c6\u30b9\u30c81] PASSED [ 16%]
sample.py::test_parametrize_ids[\u30c6\u30b9\u30c82] PASSED [ 33%]
sample.py::test_parametrize_ids[\u30c6\u30b9\u30c83] PASSED [ 50%]
sample.py::test_param_id[\u30c6\u30b9\u30c81] PASSED [ 66%]
sample.py::test_param_id[\u30c6\u30b9\u30c82] PASSED [ 83%]
sample.py::test_param_id[\u30c6\u30b9\u30c83] PASSED [100%]
そこで ID を日本語で表示させるために disable_test_id_escaping_and_forfeit_all_rights_to_community_support というオプションを設定ファイルで有効にします。
# pyproject.toml
[tool.pytest.ini_options]
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = true
この状態で再度 pytest を実行すると日本語の ID が表示されるようになります。
$ pytest -v sample.py
==================================================================== test session starts ====================================================================
platform darwin -- Python 3.13.7, pytest-8.4.1, pluggy-1.6.0 -- /path/to/dir/.venv/bin/python
cachedir: .pytest_cache
rootdir: /path/to/dir
configfile: pyproject.toml
plugins: anyio-4.10.0, mock-3.14.1
collected 6 items
sample.py::test_parametrize_ids[テスト1] PASSED [ 16%]
sample.py::test_parametrize_ids[テスト2] PASSED [ 33%]
sample.py::test_parametrize_ids[テスト3] PASSED [ 50%]
sample.py::test_param_id[テスト1] PASSED [ 66%]
sample.py::test_param_id[テスト2] PASSED [ 83%]
sample.py::test_param_id[テスト3] PASSED [100%]
===================================================================== 6 passed in 0.01s =====================================================================
pytest のバージョンに注意
ID を設定する方法を2つ紹介致しましたが、pytest のバージョンが 8.4.0 未満で pytest.param を利用する場合、先ほど紹介したオプションを有効にしても日本語が Unicode エスケープされてしまうので注意が必要です。
$ pytest -v sample.py
==================================================================== test session starts ====================================================================
platform darwin -- Python 3.13.7, pytest-8.3.5, pluggy-1.6.0 -- /path/to/dir/.venv/bin/python
cachedir: .pytest_cache
rootdir: /path/to/dir
configfile: pyproject.toml
plugins: anyio-4.10.0, mock-3.14.1
collected 6 items
sample.py::test_parametrize_ids[テスト1] PASSED [ 16%]
sample.py::test_parametrize_ids[テスト2] PASSED [ 33%]
sample.py::test_parametrize_ids[テスト3] PASSED [ 50%]
sample.py::test_param_id[\u30c6\u30b9\u30c81] PASSED [ 66%]
sample.py::test_param_id[\u30c6\u30b9\u30c82] PASSED [ 83%]
sample.py::test_param_id[\u30c6\u30b9\u30c83] PASSED [100%]
===================================================================== 6 passed in 0.01s =====================================================================
Discussion