pytest多文件执行顺序控制详解

 更新时间:2022年07月05日 09:49:31   作者:FatPuffer  
默认情况下pytest测试用例的执行顺序是先按照外层后内层(目录下的文件),同层级的包或文件、根据名称、按照ascii码升序执行,文件内的用例根据先后顺序执行,这篇文章主要给大家介绍了关于pytest多文件执行顺序控制的相关资料,需要的朋友可以参考下

1.只有一个py文件

1.使用pytest做接口测试,如果测试case只存在于单个.py文件,那么测试case默认从上到下执行,如果使用了pytest-order插件

2.如果存在多个py文件

1.使用pytest做接口测试,如果测试case存在于多个.py文件中,那么默认是按照文件名的ascii码顺序执行,进入文件后,默认按照从上到下顺序执行每个单元测试接口。

test_user.py  # 用户相关
    class TestUser:
        def test_user_create:
        def test_user_login:
        def test_user_delete
        
test_order.py  # 订单相关
    class TestOrder:
        def test_order_create:
        def test_order_list:
        def test_order_delete
        
test_stock.py  # 库存相关
    class TestStock:
        def test_stock_add:
        def test_stock_list:
        def test_stock_reduce

1.按照文件名ascii排序:test_order > test_stock > test_user

2.test_order_create > test_order_list > test_order_delete > test_stock_add > test_stock_list > …

2.如果单个.py测试文件中使用了pytest-order插件,那么该文件中添加了order的测试用例将会最先执行,没添加的将会按照1的顺序执行,这样就会出现单元测试的顺序在多文件中交叉执行的现象。(所以单个.py文件在使用pytest-order插件的情况下,建议每个case都带上order=x,且x不要相同)

test_user.py  # 用户相关
    class TestUser:
        @pytest.mark.run(order=1)
        def test_user_create:
        def test_user_login:
        @pytest.mark.run(order=2)
        def test_user_delete
        
test_order.py  # 订单相关
    class TestOrder:
        def test_order_create:
        def test_order_list:
        def test_order_delete
        
test_stock.py  # 库存相关
    class TestStock:
        def test_stock_add:
        def test_stock_list:
        def test_stock_reduce

1.由于 test_user 文件中的 case 使用了 pytest-order 插件,所以优先执行使用了order排序的 case

2.test_user_create > test_user_delete> test_order_create> … > test_stock_add > … > test_user_delete

3.如果多个.py文件使用了pytest-order插件,如果每个order指定的顺序不冲突,就按照order指定的顺序执行,如果有冲突,那就会出现在多个.py文件中交叉执行,可能不符合我们预期。

test_user.py  # 用户相关
    class TestUser:
        @pytest.mark.run(order=1)
        def test_user_create:
        def test_user_login:
        @pytest.mark.run(order=2)
        def test_user_delete
        
test_order.py  # 订单相关
    class TestOrder:
        def test_order_create:
        def test_order_list:
        def test_order_delete
        
test_stock.py  # 库存相关
    class TestStock:
        @pytest.mark.run(order=1)
        def test_stock_add:
        @pytest.mark.run(order=2)
        def test_stock_list:
        def test_stock_reduce

1.test_stock 和 test_user 存在 order 冲突,所以按照文件名ascii顺序排序

2.test_stock_add > test_user_create > test_stock_list > test_user_delete > order相关 > test_stock_reduce > test_user_login

4.多个py文件修改按照文件名ascii码排序方式

需求:不要再多个文件中来回执行case,保证测试用例顺序为:用户模块-->订单模块-->库存模块

方式一:通过修改文件名,使得文件名ascii码排序,和我们测试case执行顺序一致,确保case中没有pytest-order插件

test_1_user.py  # 用户相关
    class TestUser:
        def test_user_create:
        def test_user_login:
        def test_user_delete
        
test_2_order.py  # 订单相关
    class TestOrder:
        def test_order_create:
        def test_order_list:
        def test_order_delete
        
test_3_stock.py  # 库存相关
    class TestStock:
        def test_stock_add:
        def test_stock_list:
        def test_stock_reduce

但通常情况下,我们.py文件是根据模块去命名的,所以通过修改文件名实现我们预期的执行顺序,并不是很友好

方式二:如果使用pytest-order插件来控制,必须保证每个文件的order值是不能重复的,后一个.py文件order最小值必须大于前一个.py文件最大值,这样就可以确保文件执行顺序

这样在增加测试用例后,就可能需要修改很多order顺序

test_user.py  # 用户相关
    class TestUser:
        @pytest.mark.run(order=1)
        def test_user_create:
        @pytest.mark.run(order=3)
        def test_user_login:
        @pytest.mark.run(order=2)
        def test_user_delete
        
test_order.py  # 订单相关
    class TestOrder:
        @pytest.mark.run(order=4)
        def test_order_create:
        @pytest.mark.run(order=5)
        def test_order_list:
        @pytest.mark.run(order=6)
        def test_order_delete
        
test_stock.py  # 库存相关
    class TestStock:
        @pytest.mark.run(order=7)
        def test_stock_add:
        @pytest.mark.run(order=8)
        def test_stock_list:
        @pytest.mark.run(order=9)
        def test_stock_reduce

方式三:通过pytest提供的勾子方法pytest_collection_modifyitems,对case执行顺序进行修改

# conftest.py

def pytest_collection_modifyitems(config, items)
    # 期望用例顺序按照.py文件执行
    appoint_classes = {"TestUser": [], "TestOrder": [], "TestStock": []}

    for item in items:
        for cls_name in appoint_classes:
            if item.parent.name == cls_name:
                appoint_classes[cls_name].append(item)
    items.clear()
    for cases in appoint_classes.values():
        items.extend(cases)

用户只需要将其新增的测试模块class按照预期的顺序添加到appoint_classes中即可,简单灵活

总结

到此这篇关于pytest多文件执行顺序控制的文章就介绍到这了,更多相关pytest多文件执行顺序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • Python中DataFrame转列表的最全指南

    Python中DataFrame转列表的最全指南

    在Python数据分析中,Pandas的DataFrame是最常用的数据结构之一,本文将为你详解5种主流DataFrame转换为列表的方法,大家可以根据需求进行选择
    2025-03-03
  • Python面向对象之反射/自省机制实例分析

    Python面向对象之反射/自省机制实例分析

    这篇文章主要介绍了Python面向对象之反射/自省机制,结合实例形式分析了Python面向对象程序设计中的反射/自省机制概念、原理及相关属性操作技巧,需要的朋友可以参考下
    2018-08-08
  • Pyecharts之特殊图表的实现示例

    Pyecharts之特殊图表的实现示例

    本文主要介绍了Pyecharts之特殊图表的实现示例,包括象形图、水球图和日历图的定制方法,具有一定的参考价值,感兴趣的可以了解一下
    2025-01-01
  • 详解python常用命令行选项与环境变量

    详解python常用命令行选项与环境变量

    这篇文章主要介绍了python常用命令行选项与环境变量,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • Python3中正则模块re.compile、re.match及re.search函数用法详解

    Python3中正则模块re.compile、re.match及re.search函数用法详解

    这篇文章主要介绍了Python3中正则模块re.compile、re.match及re.search函数用法,结合实例形式较为详细的分析了re模块 中re.compile、re.match及re.search函数的功能、参数、具体使用技巧与注意事项,需要的朋友可以参考下
    2018-06-06
  • python实现图像拼接

    python实现图像拼接

    这篇文章主要为大家详细介绍了python实现图像拼接,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • python for循环输入一个矩阵的实例

    python for循环输入一个矩阵的实例

    今天小编就为大家分享一篇python for循环输入一个矩阵的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-11-11
  • Python实现PDF转Word的多种方式总结

    Python实现PDF转Word的多种方式总结

    这篇文章主要为大家详细介绍了三种Python实现PDF文件转Word文档的方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-11-11
  • Python实现一键自动分类管理文件

    Python实现一键自动分类管理文件

    经常杂乱无章的文件夹会让我们找不到所想要的文件,所以本文小编特意为大家介绍了如何制作一个可视化GUI界面,通过输入路径一键点击实现文件分门别类的归档,希望对大家有所帮助<BR>
    2024-01-01
  • Python全栈之正则表达式

    Python全栈之正则表达式

    这篇文章主要为大家介绍了Python正则表达式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-11-11

最新评论