全网最牛自动化测试框架系列之pytest(13)-多线程、多进程执行用例

【文章末尾给大家留下了大量的福利】

有些项目的测试用例较多,测试用例时需要分布式执行,缩短运行时间。

pytest框架中提供可用于分布式执行测试用例的插件:pytest-parallel、pytest-xdist,接下来我们来学习这两个插件的使用方法。

pytest-parallel

pytest-parallel 同时支持多线程、多进程两种方式执行测试用例。

安装

安装命令:pip install pytest-parallel==0.0.10

注意,虽然最新的版本为 0.1.1,但在windows系统中需要指定安装 0.0.10 版本,否则使用 pytest-parallel 参数执行用例时会报如下错误,其他系统暂未尝试。

AttributeError: Can't pickle local object 'pytest_addoption.<locals>.label_type.<locals>.a_label_type'

参数说明

pytest-parallel 提供参数执行测试用例,示例如下:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py', '--workers=1', '--tests-per-worker=3'])

参数说明:

  1. --workers=n指定运行的进程数为 n,默认为1,windows系统中只能为1
  2. --tests-per-worker=m 指定运行的线程数为 m
  3. 若两个参数都指定,则表示启动n个进程,每个进程最多启动m线程执行,总线程数=进程数*线程数
  4. windows系统中不支持 --workers 取其他值,即只能为1,mac或linux系统中可取其他值

使用

接下来举例进行说明。

测试用例模块 test_case.py:

import pytest
import time

def test_01():
    print("执行test_01")
    time.sleep(3)

def test_02():
    print("执行test_02")
    time.sleep(4)

def test_03():
    print("执行test_03")
    time.sleep(5)

def test_04():
    print("执行test_04")
    time.sleep(6)

不使用 pytest-parallel 执行用例:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py'])
    

# 执行结果如下:
collected 4 items

testcase\test_case.py
.执行test_01
.执行test_02
.执行test_03
.执行test_04

============================= 4 passed in 18.05s ==============================

使用 pytest-parallel 分布式执行用例:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py', '--workers=1', '--tests-per-worker=3'])


# 执行结果如下:  
collected 4 items
pytest-parallel: 1 worker (process), 3 tests per worker (threads)

执行test_01
执行test_03执行test_02

.执行test_04
...
============================== 4 passed in 9.04s ==============================

从以上结果可以看出来:

  1. 不使用 pytest-parallel 执行 test_case.py 中的测试用例所用时间为18.05s

  2. 使用 pytest-parallel 执行 test_case.py 中的测试用例,当 --workers=1、--tests-per-worker=3 时所用时间为9.04s

pytest-xdist

pytest-xdist 只支持多进程执行测试用例,不支持多线程执行。

安装

安装命令:pip install pytest-xdist

参数说明

pytest-xdist 提供参数执行测试用例,示例如下:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py', '-n=4'])

参数说明:

  1. -n= 指定进程数,如 -n=4 表示开启4个cpu进行执行测试用例。
  2. pytest-xdist支持windows系统使用,同样也支持mac、linux。

使用

使用 pytest-xdist 分布式执行用例:

if __name__ == '__main__':
    pytest.main(['-s', 'testcase/test_case.py', '-n=4'])

    
# 执行结果如下:
plugins: allure-pytest-2.9.45, forked-1.4.0, html-2.1.1, metadata-1.10.0, ordering-0.6, parallel-0.0.10, rerunfailures-9.1.1, xdist-2.5.0
gw0 I / gw1 I / gw2 I / gw3 I
gw0 [4] / gw1 [4] / gw2 [4] / gw3 [4]

....
============================== 4 passed in 7.19s ==============================

从结果可以看出来,使用 pytest-xdist 执行 test_case.py 中的测试用例,当 -n=4 时所用时间为7.19s

总结

  1. pytest-parallel 支持多线程执行用例,但在windows系统中只支持单个进程执行,即windows中只能--workers=1

  2. pytest-xdist 只支持多进程执行用例,但可以在windows系统中进行参数设置。

  3. 推荐使用 pytest-parallel,因为支持多线程执行,且自动化测试项目一般会搭建在mac或linux系统中运行,--workers 可以取别的值。

在使用过程中可能会遇到其他一些问题,欢迎评论探讨。

 重点:学习资料学习当然离不开资料,这里当然也给你们准备了600G的学习资料

【需要的可以扫描文章末尾的qq群二维码自助拿走】

【记得(备注“csdn000”)】

【或私信000】

群里的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。

项目实战:

大型电商平台:

全套软件测试自动化测试教学视频

300G教程资料下载【视频教程+PPT+项目源码】

全套软件测试自动化测试大厂面经

python自动化测试++全套模板+性能测试

听说关注我并三连的铁汁都已经升职加薪暴富了哦!!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值