上期回顾
Pytest的配置:AutoTest-Pytest环境安装配置
Pytest运行参数说明:Pytest常用参数剖析 | 案例演示
本期介绍
今天给大家再进行讲解下Pytest运行时另外几个份量级参数 -n、-reruns
-n NUM: pytest-xdist多线程运行(需要先安装pyest-xdist)
--reruns NUM:重试运行测试用例(需要先安装pytest-rerunfailures)
快速安装模块包
在Pycharm新建requirements.txt文件,在文件里录入你这边需要安装的模块信息
pytest
pytest-html
pytest-xdist
pytest-ordering
pytest-rerunfailures
allure-pytest
在pycahrm | Termianl命令行输入pip install -r requirements.txt进行快速安装
安装完毕即可正常使用文本里的模块
进入主题运行参数
-n参数
// FileName: HelloWorld.python
# -*- coding:utf-8 -*-
# auth:shichao
# Email:695214599@qq.com
import pytest
import time
class TestLogin:
def test_01_shichao(self):
time.sleep(2)
print('这是第1条测试用例')
assert 1 < 2
def test_02_shichao(self):
time.sleep(2)
print('这是第2条测试用例')
a = "测试"
assert "测" in a
def test_03_shichao(self):
time.sleep(2)
print('这是第3条测试用例')
def test_04_shichao(self):
time.sleep(2)
print('这是第4条测试用例')
def test_05_shichao(self):
time.sleep(2)
print('这是第5条测试用例')
def test_06_shichao(self):
time.sleep(2)
print('这是第6条测试用例')
if __name__ == '__main__':
pytest.main()
pytest -vs test_one_case.py 运行参数正常运行,未加多线程执行参数测试用例运行总耗时12s+,
所以当这种方式面临着几百条测试用例的时候那么我们的耗时是不是更长,此时就需要 -n 参数来解决问题
pytest -vs test_one_case.py -n 2 运行方式加-n参数进行运行,n=2意味着代表2个线程,看看效果
发现没有当我们加了-n参数后 n=2 启动2个线程时,耗时减半了,大家还可以试试n=3 n=4的时候效果
-reruns参数
这里我们将第三条测试用例写一个错误的断言,先进行运行看是否报错,再看看我们运用重试参数-reruns的效果
// FileName: HelloWorld.python
# -*- coding:utf-8 -*-
# auth:shichao
# Email:695214599@qq.com
import pytest
import time
class TestLogin:
def test_01_shichao(self):
time.sleep(2)
print('这是第1条测试用例')
assert 1 < 2
def test_02_shichao(self):
time.sleep(2)
print('这是第2条测试用例')
a = "测试"
assert "测" in a
def test_03_shichao(self):
time.sleep(2)
print('这是第3条测试用例')
assert 1 == 2
print('这里有个错误的断言,来试试我们的重试机制')
def test_04_shichao(self):
time.sleep(2)
print('这是第4条测试用例')
def test_05_shichao(self):
time.sleep(2)
print('这是第5条测试用例')
def test_06_shichao(self):
time.sleep(2)
print('这是第6条测试用例')
if __name__ == '__main__':
pytest.main()
pytest -vs test_one_case.py 运行参数正常运行,未加-reruns重试参数,正常运行到第三条测试用例进行报错了
我们来试试进行加上--reruns的效果,注意哈当我们在实际命令编写时,是使用的--reruns 2 后面接上重新运行的次数,后面接2就代表重新运行2次
pytest -vs test_one_case.py --reruns 2 当我们加了--reruns 2 参数后我们发现第三条错误的用例,按照预期进行重试了2次
划重点:--reruns参数的作用,
做过UI自动化的同学都知道,我们很多测试用例都是基于前端页面元素加载完毕后,使用selenium的内置方法模拟人工进行UI自动化测试
如果当某次执行时页面元素因某些原因未成功加载完毕,此时我们的测试用例运行时捕捉不到页面元素,则会进行报错
所以如果我们运用到--reruns参数进行重试的目的,就是为了重试这类运行错误的测试用例二次校验是不是真的失败。
以上就是针对于-n、-reruns 运行参数的剖析,后期大家如果使用到pytest框架则会明白其中的好处,希望对大家带来帮助
支持小编持续输出好文,请点赞、分享朋友圈一下吧!
加小编微信:695214599,一起共事,进步,沉淀!
上一篇:接口测试系列 | 实战演练
点击阅读原文,查看更多精彩文章