使用pytest_runtest_logreport方法进行测试日志记录的实践

前言

在pytest-rerunfailures:优化测试稳定性的失败重试工具这篇文章中,我们查看源码时,看到了pytest_runtest_logreport,这是一个钩子函数,那这个钩子函数是干嘛用的?有啥作用?运行机制是怎样的?带着这些疑问,我们一起往下看。

pytest_runtest_logreport 钩子函数介绍

pytest_runtest_logreport 是 pytest 框架中的一个钩子函数,用于处理测试期间发生的各种事件。它会在每个测试项(例如测试用例或测试函数)执行期间被调用,提供了丰富的信息和操作选项。

该方法接收一个名为 "report" 的参数,该参数是一个包含测试报告相关信息的对象。我们可以通过分析 report 对象的属性来获取有关测试的重要信息,例如测试结果、日志输出等。

以下是 report 对象常用的一些属性:

  • report.when: 测试发生的时间点,例如:"call" 表示测试函数调用前,"teardown" 表示测试函数执行后。
  • report.outcome: 测试结果,例如:"passed" 表示测试通过,"failed" 表示测试失败。
  • report.longrepr: 详细的测试结果信息,例如失败时的堆栈跟踪信息。

使用场景

在编写测试用例时,我们希望能够详细了解每个测试的执行情况,包括测试开始、测试结束、测试失败等。pytest_runtest_logreport 方法为我们提供了一种机制,可以捕获并记录这些关键事件和相关日志信息。

下面通过一个案例来说明如何使用 pytest_runtest_logreport 方法记录测试日志。

conftest.py

python 复制代码
def pytest_runtest_logreport(report):
    print(f"======={report}")
    outcome = report.outcome
    if report.when == "call":
        print("开始执行测试:", report.nodeid)
        if outcome == "passed":
            print("测试通过:", report.nodeid)
        elif outcome == "failed":
            print("测试失败:", report.nodeid)
            print("错误信息:", report.longrepr)

test_demo.py

arduino 复制代码
def test_addition():
    assert 2 + 2 == 4

在上面的示例中,我们编写了一个简单的测试用例 test_demo,它断言 2 + 2 是否等于 4。同时,我们定义了一个 pytest_runtest_logreport 函数,并根据 report 的属性来输出不同的日志信息。

当我们运行该测试用例时,pytest_runtest_logreport 方法会被调用三次:when=="setup",when=="call",when=="teardown"。我们看输出结果:

ini 复制代码
=======<TestReport 'test_dir/test_demo.py::test_addition' when='setup' outcome='passed'>
PASSED=======<TestReport 'test_dir/test_demo.py::test_addition' when='call' outcome='passed'>
开始执行测试: test_dir/test_demo.py::test_addition
测试通过: test_dir/test_demo.py::test_addition
=======<TestReport 'test_dir/test_demo.py::test_addition' when='teardown' outcome='passed'>

运行机制

pytest_runtest_logreport钩子函数的运行机制如下:

  1. 在 pytest 执行过程中,当一个测试项(例如测试函数或测试方法)开始设置、调用、拆除时,pytest 会触发 pytest_runtest_logreport 钩子函数。

  2. 钩子函数接收一个参数 report,它是一个 TestReport 对象,包含了当前测试项的详细信息,如测试项的名称、结果状态等。

  3. 钩子函数根据 report 对象的 when 属性,可以判断当前的测试阶段。when 属性可取的值有:

    • 'setup':表示当前处于测试项的设置阶段,即在执行测试项之前进行的准备工作。
    • 'call':表示当前处于测试项的调用阶段,即执行测试项本身的阶段。
    • 'teardown':表示当前处于测试项的拆除阶段,即在执行测试项之后进行的清理工作。
  4. 根据不同的测试阶段,钩子函数可以执行相关的处理操作,例如打印日志、记录结果、生成报告等。

总结一下,pytest_runtest_logreport 钩子函数可以在测试项的设置、调用和拆除阶段捕获并处理相应的日志报告。开发人员可以根据需要编写自定义的钩子函数,实现对测试过程的监控、记录和分析。

最后

通过使用 pytest_runtest_logreport 方法,我们可以方便地记录测试执行期间的各种事件和日志信息。这为测试过程的分析和调试提供了强大的工具。我们可以根据具体需求自定义 pytest_runtest_logreport 函数,以满足不同的日志记录需求。

相关推荐
子兮曰5 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰5 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
默_笙5 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
爱勇宝5 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
qq_426003965 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫5 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
胡写代码5 天前
别再前后端各写一套表单校验了
java·后端
长沙三为智能科技5 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读5 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
大勇前进5 天前
原生 PHP 还是 Laravel?小项目到底要不要上框架
后端