34-pytest-Hooks函数之获取用例执行结果

本文详细介绍了如何使用pytest的hookimpl装饰器实现pytest_runtest_makereport函数,以捕获测试用例的setup、call和teardown执行结果。通过实例展示了在不同失败情况下(setup、call、teardown)的输出,并演示了如何仅获取call执行结果的方法。这对于理解和调试测试用例的执行流程非常有帮助。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

  • 本篇来学习获取用例执行结果的Hooks函数–pytest_runtest_makereport

基本使用

  • conftest.py文件中写入如下代码
# -*- coding: utf-8 -*-
# @Time    : 2022/4/7
# @Author  : 大海

import pytest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):	
	"""
	这里item是测试用例,call是测试步骤,具体执行过程如下:		
		先执行when='setup' 返回setup 的执行结果
		然后执行when='call' 返回call 的执行结果
		最后执行when='teardown'返回teardown 的执行结果
	"""
    print('-' * 20)

    # 获取钩子方法的调用结果
    out = yield
    print('用例执行结果', out)

    # 从钩子方法的调用结果中获取测试报告
    report = out.get_result()

    print('测试报告:%s' % report)
    print('步骤:%s' % report.when)
    print('nodeid:%s' % report.nodeid)
    print('description:%s' % str(item.function.__doc__))
    print(('运行结果: %s' % report.outcome))
    
  • test_66.py 写入如下代码
# -*- coding: utf-8 -*-
# @Time    : 2022/4/7
# @Author  : 大海

import os

def test_66():
    """这是测试用例描述-66"""
    print('大家好,我是测试小白!')


if __name__ == '__main__':
    os.system('pytest -s test_66.py')

  • 查看运行结果
    在这里插入图片描述

setup和teardown使用

  • conftest.py 添加内容如下
@pytest.fixture(scope="session", autouse=True)
def fix_a():
    print("setup 前置操作")
    yield 
    print("teardown 后置操作")
  • 运行test_66.py 查看结果
    在这里插入图片描述

setup失败

  • 修改conftest.py文件,代码如下
@pytest.fixture(scope="session", autouse=True)
def fix_a():
    print("setup 前置操作")
    raise Exception('前置操作失败!')
    yield
    print("teardown 后置操作")
  • 运行test_66.py ,查看结果为1error
    在这里插入图片描述

call失败情况

  • 恢复conftest.py代码,使setup 成功,修改test_66.py 文件代码,内容如下
# -*- coding: utf-8 -*-
# @Time    : 2022/4/7
# @Author  : 大海

import os

def test_66():
    """这是测试用例描述-66"""
    print('大家好,我是测试小白!')
    assert 1 != 1


if __name__ == '__main__':
    os.system('pytest -s test_66.py')

  • 运行test_66.py , 查看运行结果为1failed
    在这里插入图片描述

teardown失败情况

  • 修改conftest.py代码,内容如下
@pytest.fixture(scope="session", autouse=True)
def fix_a():
    print("setup 前置操作")
    yield
    print("teardown 后置操作")
    raise Exception("teardown 失败了!")
  • 恢复test_66.py代码并运行,查看结果为1passed,1error
    在这里插入图片描述

只获取call的结果

  • 修改conftest.py代码,内容如下
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('-' * 20)

    # 获取钩子方法的调用结果
    out = yield
    print('用例执行结果', out)

    # 从钩子方法的调用结果中获取测试报告
    report = out.get_result()
    if report.when == 'call':
        print('测试报告:%s' % report)
        print('步骤:%s' % report.when)
        print('nodeid:%s' % report.nodeid)
        print('description:%s' % str(item.function.__doc__))
        print(('运行结果: %s' % report.outcome))
  • 运行test_66.py,查看结果如下
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习de测试小白

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值