unittest是标准库,他没有失败重运行机制。后续我会放一个自己封装的失败重运行的装饰器,实现对unittest单元测试框架的冲运行。这里先不讨论unittest.
一、安装
重运行机制使用到了pytest的插件,插件名称为:rerunfailures,要使用它,需要先安装此插件
pip install pytest-rerunfailures
二、使用方法
1.命令行参数形式
-
命令:pytest --reruns 重试次数
- 比如:pytest --reruns 2 表示:运行失败的用例可以重新运行2次 -
命令:pytest --reruns 重试次数 --reruns-delay 次数之间的延时设置(单位:秒)
- 比如:pytest --reruns 2 --reruns-delay 5 表示:运行失败的用例可以重新运行2次,第一次和第二次的间隔时间为5秒钟
import pytest
def test_demo_01():
b = 1 + 2
assert 3 == b
def test_demo_02():
b = 1 + 2
assert 2 == b
if __name__ == '__main__':
pytest.main(['--reruns', '3', '--reruns-delay', '5'])

2.使用装饰器
@pytest.mark.flaky(reruns=重试次数, reruns_delay=次数之间的延时设置(单位:秒))
import pytest
def test_demo_01():
b = 1 + 2
assert 3 == b
@pytest.mark.flaky(reruns=3, reruns_delay=5)
def test_demo_02():
b = 1 + 2
assert 2 == b

pytest.ini里面配置
[pytest]
markers =
smoke:marks tests as smoke.
demo:marks tests asa demo.
testpaths=./cases
addopts=-vsq --reruns 2
base_url=https://www.baidu.com/

网友评论