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编写一个GUI倒计时器

    python编写一个GUI倒计时器

    这篇文章主要为大家详细介绍了python编写一个GUI倒计时器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • tensorflow 恢复指定层与不同层指定不同学习率的方法

    tensorflow 恢复指定层与不同层指定不同学习率的方法

    今天小编就为大家分享一篇tensorflow 恢复指定层与不同层指定不同学习率的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python tkinter库实现气泡屏保和锁屏

    python tkinter库实现气泡屏保和锁屏

    这篇文章主要为大家详细介绍了python tkinter库实现气泡屏保和锁屏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • 写一个Python脚本下载哔哩哔哩舞蹈区的所有视频

    写一个Python脚本下载哔哩哔哩舞蹈区的所有视频

    B 站大家都熟悉,尤其是它的舞蹈区.有 100W+ 的舞蹈视频.在没有 wifi 的情况下,就欣赏不了这些视频了.作为一个 python 程序员,小编就写一个 Python 脚本在晚上下载舞蹈区的所有视频,需要的朋友可以参考下
    2021-05-05
  • Python里字典的基本用法(包括嵌套字典)

    Python里字典的基本用法(包括嵌套字典)

    今天小编就为大家分享一篇关于Python里字典的基本用法(包括嵌套字典),小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • PyOD进行异常值检测使用实例

    PyOD进行异常值检测使用实例

    异常值检测各个领域的关键任务之一,PyOD是Python Outlier Detection的缩写,可以简化多变量数据集中识别异常值的过程,在本文中,我们将介绍PyOD包,并通过实际给出详细的代码示例
    2024-02-02
  • python Dtale库交互式数据探索分析和可视化界面

    python Dtale库交互式数据探索分析和可视化界面

    这篇文章主要为大家介绍了python Dtale库交互式数据探索分析和可视化界面实现功能详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • 利用python判断字母大小写的几种方法小结

    利用python判断字母大小写的几种方法小结

    在开发过程中有时候我们需要判断一个字符串是否是小写形式,下面这篇文章主要给大家介绍了关于利用python判断字母大小写的几种方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • python基础教程之Hello World!

    python基础教程之Hello World!

    这篇文章主要介绍了python基础教程之Hello World!,本文讲解了命令行中、文件中、脚本文件中输出Hello World的例子,需要的朋友可以参考下
    2014-08-08
  • python关于os.walk函数查找windows文件方式

    python关于os.walk函数查找windows文件方式

    这篇文章主要介绍了python关于os.walk函数查找windows文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08

最新评论