Pytest05-多进程运行用例、失败用例重跑

发布时间 2024-01-04 15:41:57作者: 测试老宅男扶摇

1.多进程运行用例

安装pytest-xdist:
pip install pytest-xdist

# 将测试执行发送到多个cpu
# pytest.main(['-n','2','test_many.py'])
# 使用与计算机具有cpu内核一样多的进程
pytest.main(['-n', 'auto', 'test_many.py'])

PS:最新版本的pytest和插件发现跑不起来
已验证可运行版本:
pytest:7.3.1 pytest-xdist 1.31.0

2.重新运行失败用例

pytest-rerunfailures是一个可以使pytest重新运行测试的插件,以消除间歇性故障。
安装要求:
您需要具备以下先决条件才能使用pytest-rerunfailures:
安装
pip install pytest-rerunfailures

Python 3.5,最高3.8或PyPy3
pytest 5.0或更高版本
目前已针对最近的5个次要pytest版本测试了该软件包。如果您使用的是较早版本的pytest,则应考虑更新或使用此软件包的较早版本之一。

重新运行所有测试失败用例

pytest.main(['--reruns', '3', 'test_rerun.py'])

在每次重跑之间,增加一次延迟时间

pytest.main(['--reruns', '3','--reruns-delay','2','test_rerun.py'])

3. 代码实战案例

代码文件1:test_many.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 作者:扶摇
import pytest
from selenium import webdriver
from time import sleep

def test01():
    driver = webdriver.Chrome()
    sleep(3)
    driver.get("http://www.baidu.com")
    sleep(10)
    assert 1 == 1

def test02():
    driver = webdriver.Chrome()
    sleep(3)
    driver.get("http://www.taobao.com")
    sleep(10)
    assert 1 == 2

def test03():
    assert 1 == 3

def test04():
    assert 1 == 4
def test05():
    assert 1 == 5
def test06():
    assert 1 == 6

def test07():
    assert 1 == 7

def test08():
    assert 1 == 8

if __name__ == '__main__':
    # 并发2个,将测试任务分给2个CPU去同时执行
    # pytest.main(['-n','2','test_many.py'])
    # 使用与计算机CPU内核一样多的线程,进行并发
    pytest.main(['-n','auto','test_many.py'])

代码文件2:test_rerun.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 作者:扶摇
import pytest
from time import sleep

def test01():
    assert 1 == 1

def test02():
    assert 1 == 2

def test03():
    assert 1 == 3

def test04():
    assert 1 == 4
def test05():
    assert 1 == 5
def test06():
    assert 1 == 6

def test07():
    assert 1 == 7

def test08():
    assert 1 == 8

if __name__ == '__main__':
    # pytest.main(['--reruns','3','test_rerun.py'])
    # 在每次失败重跑之前,增加一个延时时间
    pytest.main(['--reruns','3','--reruns-delay','1','test_rerun.py'])

测试技术交流请联系我

备注博客园扶摇

【学习软件测试/Python自动化测试技术/领取Python自动化测试学习路线图/简历优化】
视频链接:
课程服务介绍

自动化全栈学习路线图讲解

软件测试面试合集

Python编程刷题合集

Pytest入门到实战

Python接口自动化合集

PythonWeb自动化合集

性能测试合集

Jmeter接口自动化测试实战全集

2023GPT探索发现合集

加微信(备注博客园扶摇)即可免费领取下面的自动化测试资料和一份软件测试面试宝典