[英]Pytest does not display tuple values in test report
I have a parametrized pytest test and use tuples as expected values.我有一个参数化的 pytest 测试并使用元组作为预期值。
If I run the tests, these values are not displayed and autogenerated ones (expected1...expected) are shown in the report.如果我运行测试,这些值不会显示,自动生成的值 (expected1...expected) 会显示在报告中。
Is it possible to have tuple values displayed in the output?是否可以在输出中显示元组值?
Test sample:测试样本:
@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
Output:输出:
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
If you want to see the tuples, you can define the tuples as indirect parameter strings.如果要查看元组,可以将元组定义为间接参数字符串。 This calls a fixture in
conftest.py
that can eval
the strings back into tuples before they are passed to the test function.这会调用
conftest.py
中的夹具,该夹具可以在将字符串传递给测试函数之前将它们eval
回元组。
It's not perfect, but it's fairly non-invasive and easy to understand.它并不完美,但它相当非侵入性且易于理解。
@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: conftest.py:
@pytest.fixture
def expected(request):
return eval(request.param)
Output:输出:
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
Some purists may argue that using eval
is bad practice.一些纯粹主义者可能会争辩说使用
eval
是不好的做法。 If you want, you can use literal_eval
instead.如果需要,您可以改用
literal_eval
。 See here .见这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.