遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(31):pytest常用插件 - 并发执行(pytest-xdist)

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

应用条件

无依赖:用例间没有关系

无顺序:用例可以不按顺序随机执行

此时,就可以并发执行,节约测试时间

注意:并发执行会打乱执行顺序,与pytest-ordering插件是冲突的

 

插件安装

pip install pytest-xdist

 

使用方式

加参数-n x,x表示进程数(pytest-xdist是进程级并发)

也可以:-n auto,可以根据当前系统的CPU核数自动设置进程数

执行原理过程类似jmeter分布式

 

基础示例

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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
 
import pytest
 
 
class Test01:
 
    def test_d(self):
        print("---test_d")
 
    @pytest.mark.run(order=-3)
    def test_c(self):
        print("---test_c")
 
    @pytest.mark.run(order=0)
    def test_b(self):
        print("---test_b")
 
    @pytest.mark.run(order=1)
    def test_a(self):
        print("---test_a")

 

非并发执行

 

2个进程并发执行:gw0、gw1

 

3个进程并发执行:gw0、gw1、gw2

 

自动进程并发执行:gw0、gw1、gw2、gw3

 

分组执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
 
 
class Test01:
    def test_d(self):
        print("---test_d")
 
    def test_c(self):
        print("---test_c")
 
class Test02:
    def test_b(self):
        print("---test_b")
 
    def test_a(self):
        print("---test_a")

  

不分组:test_a和test_d分别属于不同的测试类,但是都是被gw0执行

 

分组:哪怕指定了3个进程,但是实际是2个,分别执行不同测试类下的两个方法

 

更多实践应用

https://www.cnblogs.com/uncleyong/p/18032317

 

【bak】

 

posted @   全栈测试笔记  阅读(208)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示