gpt4 book ai didi

python - pytest 报告摘要显示错误信息

转载 作者:太空狗 更新时间:2023-10-29 20:37:55 26 4
gpt4 key购买 nike

我对 pytest Hook 和插件比较陌生,我不知道如何让我的 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 插件,但似乎无法正常工作。任何人有任何其他想法?

谢谢

最佳答案

试试 -tb 标志:

pytest --tb=line

这为每个测试提供一行输出。参见 the docs .

关于python - pytest 报告摘要显示错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30994622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com