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状态

相关推荐
喵手3 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934733 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy3 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威4 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ4 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha5 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy5 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
喵手5 小时前
Python爬虫实战:构建各地统计局数据发布板块的自动化索引爬虫(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集数据csv导出·采集各地统计局数据发布数据·统计局数据采集
天天爱吃肉82186 小时前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车
m0_715575346 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python