🤖
pytest subtests
普段使ってるツールの使ってない機能を使ってみる Advent Calendar 2025です。
subtestsとは
pytestでテストケースの中で、色々なテストデータでテストを行う機能です。
元々プラグインでしたが、9.0で本体に追加されました。
parametrizedと何が違うの
pytestにはparametrizedという、テストデータをパラメータ化していろいろ実行する機能があります。
subtestsと役割が似ていますが、ドキュメントでも比較の記載があり、subtestsのメリットとして、動的にデータを作成できる的が上げられています。
(逆にデメリットとして、プラグインとの兼ね合い、実行時に決まるので事前の指定ができない等)
試してみる
subtests.testで囲んで、assertするだけです。
公式ドキュメントにある偶数でpass、奇数でfailするテストです。
(パラメータはメッセージと、追加でレポートに表示する任意数のパラメータ)
def test(subtests):
for i in range(5):
with subtests.test(msg="custom message", i=i):
assert i % 2 == 0
実行してみます
> pytest test_subtest.py
============================================================================================== test session starts ==============================================================================================
platform linux -- Python 3.11.11, pytest-9.0.1, pluggy-1.6.0
rootdir: /home/notrogue/project/pytest/pytest_sandbox
configfile: pyproject.toml
collected 1 item
test_subtest.py uuF [100%]
=================================================================================================== FAILURES ====================================================================================================
__________________________________________________________________________________________ test [custom message] (i=1) __________________________________________________________________________________________
subtests = <_pytest.subtests.Subtests object at 0x78df3dad2e50>
def test(subtests):
for i in range(5):
with subtests.test(msg="custom message", i=i):
> assert i % 2 == 0
E assert (1 % 2) == 0
test_subtest.py:5: AssertionError
__________________________________________________________________________________________ test [custom message] (i=3) __________________________________________________________________________________________
subtests = <_pytest.subtests.Subtests object at 0x78df3dad2e50>
def test(subtests):
for i in range(5):
with subtests.test(msg="custom message", i=i):
> assert i % 2 == 0
E assert (3 % 2) == 0
test_subtest.py:5: AssertionError
_____________________________________________________________________________________________________ test ______________________________________________________________________________________________________
contains 2 failed subtests
============================================================================================ short test summary info ============================================================================================
SUBFAILED[custom message] (i=1) test_subtest.py::test - assert (1 % 2) == 0
SUBFAILED[custom message] (i=3) test_subtest.py::test - assert (3 % 2) == 0
FAILED test_subtest.py::test - contains 2 failed subtests
=============================================================================================== 3 failed in 0.04s ===============================================================================
奇数(i=1,3)で失敗して、偶数(i=2)で成功していますね。
いろいろ
subtestsとsubtestsでないAssertを一つのテストで同時に使うこともできます。
def test(subtests):
for i in range(5):
with subtests.test(msg="custom message", i=i):
assert i % 2 == 0
assert 100 < 2
(省略)
============================================================================================ short test summary info ============================================================================================
SUBFAILED[custom message] (i=1) test_subtest.py::test - assert (1 % 2) == 0
SUBFAILED[custom message] (i=3) test_subtest.py::test - assert (3 % 2) == 0
FAILED test_subtest.py::test - assert 100 < 2
=============================================================================================== 3 failed in 0.05s ===============================================================================================
parametrizedと組み合わせることも出来ます
import pytest
@pytest.mark.parametrize("j", [1, 2])
def test(subtests, j):
for i in range(5):
with subtests.test(msg="custom message", i=i, j=j):
assert i >= j
============================================================================================ short test summary info ============================================================================================
SUBFAILED[custom message] (i=0, j=1) test_subtest.py::test[1] - assert 0 >= 1
FAILED test_subtest.py::test[1] - contains 1 failed subtest
SUBFAILED[custom message] (i=0, j=2) test_subtest.py::test[2] - assert 0 >= 2
SUBFAILED[custom message] (i=1, j=2) test_subtest.py::test[2] - assert 1 >= 2
FAILED test_subtest.py::test[2] - contains 2 failed subtests
=============================================================================================== 5 failed in 0.04s ===============================================================================================
Discussion