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分布式
基础示例
#!/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

分组执行
#!/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
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!

浙公网安备 33010602011771号