在 pytest 中显示通过测试的详尽信息

Udu*_*use 9 python testing pytest

当测试失败时,会有一个输出指示测试的上下文,例如

=================================== FAILURES ===================================
______________________________ Test.test_sum_even ______________________________

numbers = [2, 4, 6]

    @staticmethod
    def test_sum_even(numbers):
        assert sum(numbers) % 2 == 0
>       assert False
E       assert False

test_preprocessing.py:52: AssertionError
Run Code Online (Sandbox Code Playgroud)

如果我也希望通过测试得到同样的结果怎么办?这样我就可以快速检查传递给测试的参数是否正确?

我试着命令行选项行--full-trace-l--tb long,和-rpP,但他们没有工作。

任何的想法?

Ian*_*nce 6

使用--verbose标志执行 pytest将导致它在执行时列出每个测试的完全限定名称,例如:

tests/dsl/test_ancestor.py::TestAncestor::test_finds_ancestor_nodes
tests/dsl/test_and.py::TestAnd::test_aliased_as_ampersand
tests/dsl/test_and.py::TestAnd::test_finds_all_nodes_in_both_expressions
tests/dsl/test_anywhere.py::TestAnywhere::test_finds_all_nodes_when_no_arguments_given_regardless_of_the_context
tests/dsl/test_anywhere.py::TestAnywhere::test_finds_multiple_kinds_of_nodes_regardless_of_the_context
tests/dsl/test_anywhere.py::TestAnywhere::test_finds_nodes_regardless_of_the_context
tests/dsl/test_axis.py::TestAxis::test_finds_nodes_given_the_xpath_axis
tests/dsl/test_axis.py::TestAxis::test_finds_nodes_given_the_xpath_axis_without_a_specific_tag
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您只是要求从通过的测试用例中获取标准输出,那么您需要传递该-s选项pytest以防止捕获标准输出。有关标准输出抑制的更多信息可在pytest 文档中找到。