0
我有一个参数化测试,它需要str
和dict
作为参数,所以如果我允许pytest生成id,名称看起来很奇怪。pytest参数化测试用自定义id函数
所以我虽然使用函数生成自定义ID,但它看起来没有按预期工作。
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
我想它会返回这样的事情
test_file.py::test_demo[2017.01] PASSED
test_file.py::test_demo[2017.02] PASSED
但它返回这一点。
test_file.py::test_demo[2017.01-category_value0] PASSED
test_file.py::test_demo[2017.02-category_value1] PASSED
有人可以告诉我这有什么问题,或者有什么办法可以实现吗?
更新: 我意识到有什么问题,if_func将要求每个参数,如果我不会为任何参数的默认功能回到str
将被调用。我已修好,但那也很难看。
def id_func(param):
if isinstance(param, str):
return param
return " "
现在它返回这样的事情,
test_file.py::test_demo[2017.01- ] PASSED
test_file.py::test_demo[2017.02- ] PASSED
的问题是,即使我返回空字符串(即return ""
)所花费的默认表示。有人能告诉我为什么吗?