Просто реализуйте пользовательский хук pytest_make_parametrize_id
. В вашем conftest.py
:
def pytest_make_parametrize_id(config, val, argname):
if isinstance(val, int):
return f'{argname}={val}'
if isinstance(val, str):
return f'text is {val}'
# return None to let pytest handle the formatting
return None
Пример тестов:
import pytest
@pytest.mark.parametrize('n', range(3))
def test_int(n):
assert True
@pytest.mark.parametrize('s', ('fizz', 'buzz'))
def test_str(s):
assert True
@pytest.mark.parametrize('c', (tuple(), list(), set()))
def test_unhandled(c):
assert True
Проверка параметров параметрирования:
$ pytest -q --collect-only
test_spam.py::test_int[n=0]
test_spam.py::test_int[n=1]
test_spam.py::test_int[n=2]
test_spam.py::test_str[text is fizz]
test_spam.py::test_str[text is buzz]
test_spam.py::test_unhandled[c0]
test_spam.py::test_unhandled[c1]
test_spam.py::test_unhandled[c2]
no tests ran in 0.06 seconds