How to take action on a test failure with pytest?(如何对PYTEST测试失败采取措施?)
问题描述
我正在使用pytest。
我想收集/保存一些数据,以便对测试失败进行事后分析。我可以编写teardown_method
,但我看不到在该上下文中获取测试状态的方法。
是否可以对任何测试(或assertion
)失败采取操作?
推荐答案
在conftest.py
文件中实现pytest_exception_interact
,根据documentation:
在引发可能可交互处理的异常时调用。
def pytest_exception_interact(node, call, report):
if report.failed:
# call.excinfo contains an ExceptionInfo instance
从您的问题中不清楚您希望从错误中收集什么信息,但可能有权访问ExceptionInfo
实例就足以满足您的需求。
这篇关于如何对PYTEST测试失败采取措施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!