pytest 用例执行失败后其他不再执行

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

pytest 用例执行失败后其他不再执行

qq_37668510   2023-03-20 我要评论

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跳过。

 到此这篇关于pytest 用例执行失败后其他不再执行的文章就介绍到这了,更多相关pytest 用例执行失败内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们