【发布时间】:2021-10-20 22:28:35
【问题描述】:
我有一个参数化的 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
【问题讨论】:
-
也许看看这个:stackoverflow.com/a/37591040/8763097。此答案显示了如何自定义格式参数。您可能需要更改
item.name = '{func}[{params}]'.format(func=item.name.split('[')[0], params=format_string.format(**params))行以适应将元组输出为字符串。 -
是的,它有帮助,但代码看起来有点过于复杂,我认为 pytest 可以自行格式化