我已经知道stackoverflow答案描述了如何为所有测试设置一个带有一般消息的新部分,但我希望有一个特殊部分仅在某个模块的测试失败时显示。
这背后的故事是我为我进行的分析编写了一个测试。我仍然想进一步开发分析功能,但我很有信心结果是正确的。因此,我腌制了原始版本的所有结果,并编写了一个测试,将当前函数下的输出与腌制的原始输出 ( test_stability.py
) 进行比较。为了帮助我的同事和我自己,我现在想创建一条自定义消息,该消息会在测试test_stability.py
失败时出现。
所以,我有一个test_stability.py
包含两个测试的文件:
#test_stability.py
import pytest
def test_form_stable():
assert True
def test_content_stable():
assert False
另一个文件包含两个常规测试test_regular.py
:
#test_regular.py
import pytest
def test_first_reg():
assert True
def test_second_reg():
assert False
理想情况下,我的 pytest 报告如下所示:
collected 4 items
test_stability.py::test_form_stable PASSED
test_stability.py::test_content_stable FAILED
test_regular.py::test_first_reg PASSED
test_regular.py::test_second_reg Failed
============================================= FAILURES =============================================
____________________________________________ test_content_stability ____________________________________________
def test_content_stability():
> assert False
E assert False
test_spam.py:9: AssertionError
____________________________________________ test_second_reg ____________________________________________
def test_second_reg():
> assert False
E assert False
test_spam.py:9: AssertionError
---------------------------------------- My custom section -----------------------------------------
test_stability.py::test_content_stability says: "My custom message"
================================ 2 failed, 2 passed in 0.07 seconds ================================