pytest教程-42-钩子函数-pytest_runtest_makereport

领取资料,咨询答疑,请➕wei:  June__Go

上一小节我们学习了pytest_runtest_teardown钩子函数的使用方法,本小节我们讲解一下pytest_runtest_makereport钩子函数的使用方法。

pytest_runtest_makereport 钩子函数在 pytest 为每个测试生成报告时调用。这个钩子可以用来自定义测试结果的报告,例如添加额外的信息到报告中,或者修改报告的格式。以下是如何使用这个钩子函数的具体方法和代码示例:

首先,确保你的项目中有一个 conftest.py 文件。然后,在 conftest.py 文件中定义 pytest_runtest_makereport 钩子函数:

# conftest.py

import pytest

def pytest_runtest_makereport(item, call):
    # 获取测试结果报告
    report = item.get_reportinfo()

    # 检查测试结果
    if report.when == "call":
        if report.passed:
            # 如果测试通过,添加自定义信息
            report.extra_info = "Test passed without any issues."
        elif report.failed:
            # 如果测试失败,添加自定义错误信息
            report.extra_info = "Test failed due to an error."
        elif report.skipped:
            # 如果测试被跳过,添加自定义跳过原因
            report.extra_info = "Test was skipped as per the skip condition."

    # 如果需要修改报告的格式,可以在这里进行
    # 例如,你可以修改 report.nodeid, report.duration 等属性

    # 返回修改后的报告信息
    return report

在这个示例中,我们首先获取了当前测试的报告信息。然后,我们根据测试的结果(通过、失败或跳过)添加了自定义的信息到报告中。这些自定义信息将在 pytest 的报告中显示。

请注意,pytest_runtest_makereport 钩子函数的参数 item 是当前测试的 Item 对象,它包含了测试的相关信息。call 是一个函数,它代表了要执行的测试函数。item.get_reportinfo() 方法用于获取当前测试的报告信息。

这个示例展示了如何使用 pytest_runtest_makereport 钩子函数来自定义测试报告。在实际应用中,你可能需要根据具体的测试需求来调整这些操作,例如添加更多的自定义信息或者修改报告的其他属性。

在这个更复杂的示例中,我们将使用 pytest_runtest_makereport 钩子函数来增强 pytest 的测试报告。我们将添加自定义的测试结果信息,包括测试用例的执行时间、是否使用了特定的 fixture,以及在测试执行期间捕获的任何异常或警告。我们还将展示如何根据测试结果修改报告的输出。

首先,确保你的项目中有一个 conftest.py 文件。然后,在 conftest.py 文件中定义 pytest_runtest_makereport 钩子函数:

# conftest.py

import pytest
import time
from _pytest.terminalwriter import TerminalWriter

def pytest_runtest_makereport(item, call):
    # 获取测试结果报告
    report = item.get_reportinfo()

    # 获取测试用例的执行时间
    execution_time = call.duration

    # 检查测试结果
    if report.when == "call":
        if report.passed:
            # 如果测试通过,添加执行时间和是否使用了特定 fixture 的信息
            report.extra_info = f"Test passed. Execution time: {execution_time:.2f}s. Used fixtures: {', '.join(item.fixturenames)}"
        elif report.failed:
            # 如果测试失败,添加执行时间、错误信息和是否使用了特定 fixture 的信息
            report.extra_info = f"Test failed. Execution time: {execution_time:.2f}s. Error: {report.longrepr}. Used fixtures: {', '.join(item.fixturenames)}"
        elif report.skipped:
            # 如果测试被跳过,添加跳过原因和是否使用了特定 fixture 的信息
            report.extra_info = f"Test skipped. Reason: {report.reason}. Used fixtures: {', '.join(item.fixturenames)}"

    # 如果需要修改报告的格式,可以在这里进行
    # 例如,我们可以自定义报告的输出
    def pytest_report_teststatus(report):
        if report.passed:
            TerminalWriter().write(f"{report.nodeid}: passed (time={execution_time:.2f}s)\n")
        elif report.failed:
            TerminalWriter().write(f"{report.nodeid}: failed (time={execution_time:.2f}s)\n")
        elif report.skipped:
            TerminalWriter().write(f"{report.nodeid}: skipped ({report.reason})\n")

    # 返回修改后的报告信息
    return report

# 这个钩子函数用于自定义 pytest 的终端报告输出
def pytest_terminal_summary(terminalreporter):
    # 在这里可以添加自定义的总结信息,例如测试覆盖率、性能指标等
    pass

在这个示例中,我们首先获取了当前测试的报告信息和执行时间。然后,我们根据测试的结果(通过、失败或跳过)添加了详细的自定义信息到报告中。这些自定义信息包括执行时间、错误信息(如果测试失败)、跳过原因(如果测试被跳过)以及使用的 fixture 名称。

我们还定义了一个 pytest_report_teststatus 钩子函数来自定义 pytest 的终端报告输出。这个钩子函数允许我们为每个测试用例的报告添加自定义的文本输出。

最后,我们定义了一个 pytest_terminal_summary 钩子函数,它可以在测试完成后的终端报告中添加自定义的总结信息。在这个示例中,我们没有添加具体的输出内容,但你可以根据需要添加测试覆盖率、性能指标等信息。

请注意,这个示例中的代码仅用于演示如何使用 pytest_runtest_makereport 钩子函数来增强测试报告。在实际应用中,你可能需要根据具体的测试需求来调整这些操作。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走,希望可以帮助到大家!领取资料,咨询答疑,请➕wei:  June__Go

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值