pytest教程-45-钩子函数-pytest_report_testitemFinished

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

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

pytest_report_testitemFinished 钩子函数在每个测试项(测试函数或测试方法)执行完成后被调用。这个钩子可以用来获取测试结果、执行自定义的报告逻辑,或者在测试结束后进行一些清理工作。以下是一个具体的代码示例,展示了如何在 conftest.py 文件中使用这个钩子函数:

# conftest.py

import pytest

# 全局变量用于存储测试结果
test_results = {}

def pytest_report_testitemFinished(item, report):
    # 获取测试项的名称和结果
    test_name = item.name
    test_result = report.outcome

    # 将测试结果存储在全局字典中
    test_results[test_name] = test_result

    # 根据测试结果执行不同的操作
    if test_result == "passed":
        print(f"{test_name} passed.")
    elif test_result == "failed":
        print(f"{test_name} failed.")
        print(f"Failure details: {report.longrepr}")
    e
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值