领取资料,咨询答疑,请➕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['skipped']}")
# 如果有失败的测试,输出失败的测试用例数量
if stats['failed'] > 0:
print(f"\n{stats['failed']} tests failed. Please check the output for details.")
else:
print("\nAll tests passed successfully!")
# 可以在这里添加更多的自定义输出,例如输出失败的测试用例列表
if stats['failed'] > 0:
failed_tests = [item.fspath.basename for item in stats.get('failed', [])]
print("Failed tests:")
for test in failed_tests:
print(test)
在这个示例中,我们首先获取了 terminalreporter
对象,它包含了测试的统计信息。然后,我们使用这些统计信息来输出测试的总览,包括通过的测试数量、失败的测试数量和跳过的测试数量。如果有任何测试失败,我们还会输出失败的测试用例数量,并提示用户查看输出的详细信息。最后,我们可以选择性地输出失败的测试用例列表。
现在,当你运行 Pytest 测试时,pytest_terminal_summary
钩子将在测试运行结束后被调用,输出测试的总结信息。这样,你就可以在终端快速了解测试的整体结果。
好的,让我们创建一个更复杂的 pytest_terminal_summary
钩子示例。在这个示例中,我们将执行以下操作:
- 输出测试执行的详细统计信息。
- 根据测试结果(通过或失败)输出不同的总结信息。
- 对于失败的测试,输出失败的原因和堆栈跟踪。
- 如果有跳过的测试,输出跳过的测试数量和原因。
首先,确保你的 conftest.py
文件中已经定义了必要的钩子函数和测试数据。
然后,在 conftest.py
文件中添加 pytest_terminal_summary
钩子函数:
# conftest.py
def pytest_terminal_summary(terminalreporter):
# 获取测试结果统计
stats = terminalreporter.stats
# 输出测试执行的详细统计信息
print("\nTest Execution Summary:")
print(f"Total tests run: {stats['total']}")
print(f"Passed: {stats['passed']}")
print(f"Failed: {stats['failed']}")
print(f"Skipped: {stats['skipped']}")
print(f"XFailed (expected failures): {stats['xfailed']}")
print(f"XPassed (unexpected passes): {stats['xpassed']}")
# 根据测试结果输出不同的总结信息
if stats['failed'] > 0:
print("\nThere were failures!")
for item in stats.get('failed', []):
# 获取失败的测试用例的详细信息
test_item = terminalreporter.getitemnodeid(item)
print(f"Failed test: {test_item.name}")
print(f"Location: {test_item.location}")
print(f"Reason: {test_item.longrepr}")
print("\n")
elif stats['skipped'] > 0:
print("\nThere were skipped tests.")
for item in stats.get('skipped', []):
# 获取跳过的测试用例的详细信息
test_item = terminalreporter.getitemnodeid(item)
print(f"Skipped test: {test_item.name}")
print(f"Location: {test_item.location}")
print(f"Reason: {test_item.longrepr}")
print("\n")
else:
print("\nAll tests passed successfully!")
# 输出其他自定义信息,例如测试执行时间等
total_time = terminalreporter.stats.get('totaltime', 0)
print(f"Total time taken: {total_time:.2f} seconds")
在这个示例中,我们首先获取了 terminalreporter
对象,它包含了测试的统计信息。然后,我们输出了测试的详细统计信息,包括通过、失败、跳过、预期失败和意外通过的测试数量。接下来,我们根据测试结果输出不同的总结信息。对于失败的测试,我们输出了失败的原因和堆栈跟踪。对于跳过的测试,我们输出了跳过的原因。最后,我们还输出了测试执行的总时间。
请注意,这个示例中的 terminalreporter.getitemnodeid
方法用于获取失败或跳过测试的详细信息。这个方法返回一个包含测试用例名称、位置和长描述(堆栈跟踪)的对象。
现在,当你运行 Pytest 测试时,pytest_terminal_summary
钩子将在测试运行结束后被调用,输出测试的详细总结信息。这样,你就可以在终端快速了解测试的整体结果和失败的原因。
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走,希望可以帮助到大家!领取资料,咨询答疑,请➕wei: June__Go