领取资料,咨询答疑,请➕wei: June__Go
上一小节我们学习了pytest_sessionfinish钩子函数的使用方法,本小节我们讲解一下pytest_terminal_summary钩子函数的使用方法。
pytest_terminal_summary
钩子函数在 Pytest 测试运行结束后,但在退出之前调用。这个钩子可以用来在终端输出一些总结信息,比如测试执行的统计数据、自定义的报告等。
以下是一个具体的使用示例,我们将在测试结束后输出测试的通过率和失败的测试用例数量。首先,在你的项目根目录下创建 conftest.py
文件(如果还没有的话)。
然后,在 conftest.py
文件中添加 pytest_terminal_summary
钩子函数:
# conftest.py
def pytest_terminal_summary(terminalreporter):
# 获取测试结果统计
stats = terminalreporter.stats
# 输出测试执行的统计信息
print(f"\nTest Summary:")
print(f"Total tests run: {stats['total']}")
print(f"Passed: {stats['passed']}")
print(f"Failed: {stats['failed']}")
print(f"Skipped: {stats['skipp