【pytest学习】pytest 用例执行失败后其他不再执行

本文介绍了如何利用pytest的pytest_runtest_setup和pytest_runtest_makereport钩子函数,实现测试用例之间的依赖管理。当某个用例如登录失败后,后续依赖该用例的用例(如我的列表、我的订单)将被标记为xfail并跳过执行。通过全局变量记录失败用例,并在执行新用例前检查是否需要跳过。

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

1.hook函数介绍

在执行用例时,遇到用例之前存在有关联,用例执行失败后,其余用例也没有必要再去执行(比如:登录失败后,我的列表、我的订单等用例就没有比必要执行了)。

完成这个功能我们只要学习pytest给我们提供的两个Hook函数:

  • pytest_runtest_setup :在执行测试之前执行,类似unittest中的setup方法。
  • pytest_runtest_makereport:测试报告钩子函数,打印有关测试运行的信息。

2.pytest_runtest_setup

方法只有一个参数item,不需要手动传递,pytest会自动的将所需内容传入给这个方法。
item:主要记录了用例的名称、所在模块、所在类名、文件路径等信息。
在这里插入图片描述
介绍一下item中不太明确的参数:

  • keywords:当前用例被mark标记的名称。
    - 例如@pytest.mark.somke:keywords == somke
  • cls:当前用例的类名称。

3.pytest_runtest_makereport

方法有两个参数itemcall,item参数与上面一样的。
参数call :用于了记录执行过程、测试报告等信息。
在这里插入图片描述
call 中的参数介绍:

  • when:记录测试用例运行的状态,总共三个状态:
    - setup:用例“执行前”。
    - call:用例"执行中"。
    - teardown:用例"执行完成"。
  • excinfo:如果用例执行失败,会记录在这里。

4.解决思路

通过上面的分析,可以得到一个简单的思路,就是:在每个测试用例执行之前,判断当前用例所在的类是否有失败的用例,如果有就使用pytest.xfail标记为失败的用例,不再执行。

步骤:
1. 定义一个全局变量。
2. 自定一个mark标记,将用例之间有耦合性的用例标记出来(可以通过这个来控制是否需要)。
3. 添加失败用例:用pytest_runtest_makereport hook函数中使用call中的excinfo信息判断用例是否失败,如果失败就将用例添加到全局变量中。
4. 在用例执行前判断是否有失败用例:用pytest_runtest_setuphook函数中实现判断当前用例所在的类是否有失败的用例。

实现代码:
当前代码最好在conftest.py文件中实现。

# conftest.py

from typing import Dict, Tuple
import pytest

# 全局变量,记录失败的用例
_test_failed_incremental: Dict[str, Dict[Tuple[int, ...], str]] = {}


def pytest_runtest_makereport(item, call):
    # 判断用例执行失败后是否需要跳过
    if "incremental" in item.keywords:
    	# 如果用例失败,添加到全局变量中
        if call.excinfo is not None:
            cls_name = str(item.cls)
            parametrize_index = (
                tuple(item.callspec.indices.values())
                if hasattr(item, "callspec")
                else ()
            )
            test_name = item.originalname or item.name
            _test_failed_incremental.setdefault(cls_name, {}).setdefault(
                parametrize_index, test_name
            )


def pytest_runtest_setup(item):
	# 判断用例执行失败后是否需要跳过
    if "incremental" in item.keywords:
        cls_name = str(item.cls)
        # 判断当前用例的类是否在全局变量中
        if cls_name in _test_failed_incremental:
            parametrize_index = (
                tuple(item.callspec.indices.values())
                if hasattr(item, "callspec")
                else ()
            )
            test_name = _test_failed_incremental[cls_name].get(parametrize_index, None)
            # 如果当前类中的用例存在失败用例,就跳过
            if test_name is not None:
                pytest.xfail("previous test failed ({})".format(test_name))

测试代码:

@pytest.mark.incremental
class TestAdd:

    def test_01(self):
        print('test_01 用例执行中...')

    
    def test_02(self):
        pytest.xfail('以后的用例都失败了0')

    def test_03(self):
        print('test_03 用例执行中...')
        
if __name__ == "__main__":
    pytest.main()

结果:
test_01用例执行成功,
test_02失败,
test_03跳过。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值