[英]Pytest does not display tuple values in test report
我有一个参数化的 pytest 测试并使用元组作为预期值。
如果我运行测试,这些值不会显示,自动生成的值 (expected1...expected) 会显示在报告中。
是否可以在输出中显示元组值?
测试样本:
@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
如果要查看元组,可以将元组定义为间接参数字符串。 这会调用conftest.py
中的夹具,该夹具可以在将字符串传递给测试函数之前将它们eval
回元组。
它并不完美,但它相当非侵入性且易于理解。
@pytest.mark.parametrize(
'test_input, expected',
[
('12:00 AM', '(0, 0)'),
('12:01 AM', '(0, 1)'),
('11:59 AM', '(11, 58)')
],
indirect=["expected"])
def test_params(test_input, expected):
assert expected != (11, 58)
conftest.py:
@pytest.fixture
def expected(request):
return eval(request.param)
输出:
test_print_tuples.py::test_params[12:00 AM-(0, 0)] PASSED
test_print_tuples.py::test_params[12:01 AM-(0, 1)] PASSED
test_print_tuples.py::test_params[11:59 AM-(11, 58)] FAILED
一些纯粹主义者可能会争辩说使用eval
是不好的做法。 如果需要,您可以改用literal_eval
。 见这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.