I've got a pytest test that's parametrized with a @pytest.mark.parametrize decorator using a custom function load_test_cases() that loads the test cases from a yaml file.
class SelectTestCase:
def __init__(self, test_case):
self.select = test_case['select']
self.expect = test_case['expect']
def __str__(self): # also tried __repr__()
# Attempt to print the 'select' attribute in "pytest -v" output
return self.select
def load_test_cases(path):
with open(path, "rt") as f:
test_cases = yaml.safe_load_all(f)
return [ SelectTestCase(test_case) for test_case in test_cases ]
@pytest.mark.parametrize("test_case", load_test_cases("tests/select-test-cases.yaml"))
def test_select_prefixes(test_case):
# .. run the test
It works well except that the tests when run with pytest -v are displayed with test_case0, test_case1, etc parameters.
tests/test_resolver.py::test_select_prefixes[test_case0] PASSED [ 40%]
tests/test_resolver.py::test_select_prefixes[test_case1] PASSED [ 60%]
tests/test_resolver.py::test_select_prefixes[test_case2] PASSED [ 80%]
tests/test_resolver.py::test_select_prefixes[test_case3] PASSED [100%]
I would love to see the select attribute displayed instead, e.g.
tests/test_resolver.py::test_select_prefixes["some query"] PASSED [ 40%]
tests/test_resolver.py::test_select_prefixes["another query"] PASSED [ 60%]
I tried to add __str__() and __repr__() methods to the SelectTestCase class but it didn't make any difference.
Any idea how to do it?