pytest并发执行中设置用例执行顺序

场景

使用pytest-xdist实现测试用例的并发执行, 但是执行中部分用例需要设置执行顺序

需要的依赖包

pytest-order , pytest-xdist, pytest-dependency

实现方法

  1. 对执行顺序有要求的用例设置, 设置在同一个组中并且设置执行顺序,用例上设置@pytest.mark.xdist_group(name="group_name"), 并且执行时设置参数 -dist loadgroup , 就能保证group名字相同的用例在同一个worker中运行

  2. 然后对同一个group中的用例设置顺序,即@pytest.mark.order(1)

  3. 如果用例间有依赖的话,可以对被依赖和依赖的用例设置依赖关系

    python 复制代码
    @pytest.mark.dependency(name="test_name")
    @pytest.mark.dependency(depends=["test_name"])

验证

  1. 有一个文件test_1.py, 内容如下

    python 复制代码
    def test_one():
    	pass
    
    def test_two():
        pass

    执行pytest test_1.py -v -n 2

    输出结果: test_one和test_two分别属于gw0 和gw1

    shell 复制代码
    test_case/test_1.py::test_one
    test_case/test_1.py::test_two
    [gw0] [ 50%] PASSED test_case/test_1.py::test_one
    [gw1] [100%] PASSED test_case/test_1.py::test_two
  2. 添加group后test_1.py文件内容

    python 复制代码
    @pytest.mark.xdist_group(name="group_name")
    def test_one():
       pass
    
    @pytest.mark.xdist_group(name="group_name")
    def test_two():
        pass

    执行pytest test_1.py -v --dist loadgroup -n 2

    输出结果:test_one和test_two属于同一个worker gw0

    shell 复制代码
    test_case/test_1.py::test_one@group_name
    [gw0] [ 50%] PASSED test_case/test_1.py::test_one@group_name
    test_case/test_1.py::test_two@group_name
    [gw0] [100%] PASSED test_case/test_1.py::test_two@group_name
  3. 添加执行顺序后test_1.py文件内容

    python 复制代码
    @pytest.mark.xdist_group(name="group_name")
    @pytest.mark.order(2)
    def test_one():
      pass
    
    @pytest.mark.xdist_group(name="group_name")
    @pytest.mark.order(1)
    def test_two():
       pass

    执行pytest test_1.py -v --dist loadgroup -n 2

    输出结果: 用例在gw0中并且按顺序执行

    shell 复制代码
    test_case/test_1.py::test_two@group_name
    [gw0] [ 50%] PASSED test_case/test_1.py::test_two@group_name
    test_case/test_1.py::test_one@group_name
    [gw0] [100%] PASSED test_case/test_1.py::test_one@group_name
  4. 新建文件test_2.py文件内容

    python 复制代码
    import pytest
    
    @pytest.mark.dependency(depends=["test_name"])
    def test_three():
    	pass

    修改test_1.py文件内容

    python 复制代码
    import pytest
    
    @pytest.mark.dependency(name="test name")
    @pytest.mark.xdist_group(name="group_name")
    @pytest.mark.order(2)
    def test_one():
    	pass
    
    @pytest.mark.xdist_group(name="group_name")
    @pytest.mark.order(1)
    def test_two():
    	pass

    执行结果如下, test_three是skipped状态

    shell 复制代码
    test_case/test_1.py::test_two@group_name
    test_case/test_2.py::test_three
    [gw1] [ 33%] SKIPPED test_case/test_2.py::test_three
    [gw0] [ 66%] PASSED test_case/test_1.py::test_two@group_name
    test_case/test_1.py::test_one@group_name
    [gw0] [100%] PASSED test_case/test_1.py::test_one@group_name

    修改一下test_2.py

    python 复制代码
     import pytest
    
     @pytest.mark.xdist_group(name="group_name")
     @pytest.mark.dependency(depends=["test name"],scope='session')
     @pytest.mark.order(3)
     def test_three():
     	pass

    再次执行结果, 按指定顺序执行

    shell 复制代码
    test_case/test_1.py::test_two@group_name
    [gw0] [ 33%] PASSED test_case/test_1.py::test_two@group_name
    test_case/test_1.py::test_one@group_name
    [gw0] [ 66%] PASSED test_case/test_1.py::test_one@group_name
    test_case/test_2.py::test_three@group_name
    [gw0] [100%] PASSED test_case/test_2.py::test_three@group_name

经验之谈: 对于dependency不建议多用,用例设计的时候就需要考虑用例之间解耦,特别是对于UI自动化,为了保证通过率,需要设置失败重新运行,如果设置的依赖,很有可能出现被依赖的用例执行成功,但是依赖的用例是skipped状态

相关推荐
5233AI开发笔记28 分钟前
API 返回 429:先区分限速、并发和配额,再决定是否重试
python
databook29 分钟前
经常听到的透视表是干什么的?
python·数据分析·pandas
特立独行的猫A1 小时前
我用仓颉写了个多平台发文工具:一份 Markdown,一条命令发六个平台
java·后端·python
陆陆陆六1 小时前
# 五个AI引擎的答案采集:反爬、去重与时间窗对齐的三个工程细节
python·net·ai答案采集
小小张说故事4 小时前
Python 的 a[1:5:2] 到底切出了什么?拆开 slice 对象、__getitem__ 与负步长那次反向
python
wuyk5554 小时前
PyQt6-第15讲-数据可视化QChart
python·ui·信息可视化·pyqt
天天被压力4 小时前
【A股分钟级高频数据完全手册 #04】指数分钟级行情数据接口:沪深指数Pro版最新分时取数
java·人工智能·python
日日大王学编程4 小时前
2026主流AI Agent产品分类、核心差异与技术特性全解析
人工智能·后端·python
天天被压力4 小时前
【A股分钟级高频数据完全手册 #03】股票历史1分钟K线数据接口怎么用:Pro版批量拉取与本地缓存
java·人工智能·python
武天旭20 小时前
AI安全--AI大模型的毒性偏见与安全对齐实践
人工智能·python·深度学习