I have a parameterized test which takes str
, and dict
as an argument and so the name look pretty weird if I allow pytest to generate ids.
I want to generate custom ids using a function, however it seems it's not working as intended.
def id_func(param):
if isinstance(param, str):
return param
@pytest.mark.parametrize(argnames=('date', 'category_value'),
argvalues=[("2017.01", {"bills": "0,10", "shopping": "100,90", "Summe": "101,00"}),
("2017.02", {"bills": "20,00", "shopping": "10,00", "Summe": "30,00"})],
ids=id_func)
def test_demo(date, category_value):
pass
I was thinking it would return something like this
test_file.py::test_demo[2017.01] PASSED
test_file.py::test_demo[2017.02] PASSED
but it's returning this.
test_file.py::test_demo[2017.01-category_value0] PASSED
test_file.py::test_demo[2017.02-category_value1] PASSED
Could someone tell me what's wrong with this, or is there any way to achieve this?
Update:
I realize what's the issue, if_func will be called for each parameter and if I won't return str
for any parameter default function will be called. I have fix but that's also ugly.
def id_func(param):
if isinstance(param, str):
return param
return " "
Now it returns something like this,
test_file.py::test_demo[2017.01- ] PASSED
test_file.py::test_demo[2017.02- ] PASSED
The problem is even If I return empty string (i.e. return ""
)it takes the default representation. Could someone let me know why?