Pytest报告摘要显示错误信息

时间:2015-06-23 05:36:22

标签: python pytest

我对pytest钩子和插件比较新,我无法弄清楚如何让我的pytest代码给出测试执行摘要以及失败的原因。

考虑代码:

class Foo:
    def __init__(self, val):
        self.val = val

    def test_compare12():
        f1 = Foo(1)
        f2 = Foo(2)
        assert f1 == f2, "F2 does not match F1"

    def test_compare34():
        f3 = Foo(3)
        f4 = Foo(4)
        assert f3 == f4, "F4 does not match F3"

当我使用-v选项运行pytest脚本时,它在控制台上给出了以下结果:

========================= test session starts=================================
platform darwin -- Python 2.7.5 -- py-1.4.26 -- pytest-2.7.0 --    /Users/nehau/src/QA/bin/python
rootdir: /Users/nehau/src/QA/test, inifile: 
plugins: capturelog
collected 2 items 

test_foocompare.py::test_compare12 FAILED
test_foocompare.py::test_compare34 FAILED

================================ FAILURES ===============================
_______________________________ test_compare12 _________________________

def test_compare12():
    f1 = Foo(1)
    f2 = Foo(2)
>       assert f1 == f2, "F2 does not match F1"
E       AssertionError: F2 does not match F1
E       assert <test.test_foocompare.Foo instance at 0x107640368> == <test.test_foocompare.Foo instance at 0x107640488>

test_foocompare.py:11: AssertionError
_____________________________ test_compare34______________________________

def test_compare34():
    f3 = Foo(3)
    f4 = Foo(4)
>       assert f3 == f4, "F4 does not match F3"
E       AssertionError: F4 does not match F3
E       assert <test.test_foocompare.Foo instance at 0x107640248> == <test.test_foocompare.Foo instance at 0x10761fe60>

test_foocompare.py:16: AssertionError

=============================== 2 failed in 0.01 seconds ==========================

我正在运行接近2000个测试用例,所以如果我可以使用以下格式的pytest显示输出真的很有用:

::
test_foocompare.py::test_compare12 FAILED AssertionError:F2 does not match F1
test_foocompare.py::test_compare34 FAILED AssertionError:F2 does not match F1
::

我查看了pytest_runtest_makereport插件,但似乎无法使其正常工作。任何人有任何其他想法?

由于

3 个答案:

答案 0 :(得分:12)

尝试使用-tb标志:

pytest --tb=line

每次测试给出一行输出。 请参阅the docs

答案 1 :(得分:6)

同时尝试pytest -v --tb=no显示所有通过/未通过结果。

答案 2 :(得分:2)

尝试py.test的-ra选项。它将在日志末尾提供一个摘要,显示所有(失败,跳过等等并通过)。

请参阅https://docs.pytest.org/en/latest/usage.html#detailed-summary-report