Pytest05-多進程運行用例、失敗用例重跑
高清視頻B站鏈接](https://www.bilibili.com/video/BV1ce411i7Yg/)
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自動化測試學習路線圖/簡歷優化】
視頻鏈接:
課程服務介紹
加微信(備注博客園扶搖)即可免費領取下面的自動化測試資料和一份軟件測試面試寶典


本文來自博客園,作者:測試老宅男扶搖,轉載請注明原文鏈接:http://www.rzrgm.cn/cekailsf/p/17945396
浙公網安備 33010602011771號