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

1.只有一个py文件

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

2.如果存在多个py文件

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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不要相同)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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文件中交叉执行,可能不符合我们预期。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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插件

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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顺序

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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执行顺序进行修改

1 2 3 4 5 6 7 8 9 10 11 12 13 # 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中即可,简单灵活

​现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。

如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受

可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛

分享他们的经验,还会分享很多直播讲座和技术沙龙

可以免费学习!划重点!开源的!!!

qq群号:485187702【暗号:csdn11】

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】

精彩文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: