rerunfailures插件实现失败重运行

2021-04-29  本文已影响0人  阿登20

unittest是标准库,他没有失败重运行机制。后续我会放一个自己封装的失败重运行的装饰器,实现对unittest单元测试框架的冲运行。这里先不讨论unittest.

一、安装

重运行机制使用到了pytest的插件,插件名称为:rerunfailures,要使用它,需要先安装此插件

pip install pytest-rerunfailures

二、使用方法

1.命令行参数形式

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'])
image.png

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
image.png

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/
image.png
上一篇 下一篇

猜你喜欢

热点阅读