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中即可,简单灵活

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值