我有一个参数化的pytest测试,并使用元组作为期望值
如果我运行测试,这些值将不显示,而自动生成的值(expected0…expectedN)将显示在报告中
是否可以在输出中显示元组值
测试样本:
@pytest.mark.parametrize('test_input, expected',
[
('12:00 AM', (0, 0)),
('12:01 AM', (0, 1)),
('11:59 AM', (11, 58))
])
def test_params(test_input, expected):
assert time_calculator.convert_12h_to_24(test_input) == expected
输出:
test_time_converter.py::test_params[12:00 AM-expected0] PASSED test_time_converter.py::test_params[12:01 AM-expected1] PASSED test_time_converter.py::test_params[11:59 AM-expected2] FAILED
应用此answer,可以自定义
ids
属性:另一种选择,虽然不是很优雅,但是将元组定义为字符串,并使它们indirect parameters。这将在
conftest.py
中调用一个简单的fixture,该fixture可以eval
将字符串返回元组,然后再将它们传递给测试函数conftest.py:
输出(两种解决方案):
相关问题 更多 >
编程相关推荐