pythont中失败用例重新执行

python自动化中,失败用例重跑,需要用到pytest的中库rerunsfailurs,需要安装
安装步骤:
1.在dos窗口执行:pip install pytest-rerunsfailures
2.如果需要html的测试报告,需要安装 pip instal pytest-html

第一种方式

在python自动化项目根目录下新建pytest.ini,在配置文件中写入以下内容:

[pytest]
addpots=-vs --reruns 2 --reruns-delay 1

reruns为失败用例重跑的次数,reruns-delay为间隔时间,单位为s
测试用例函数

def login(username,pwd):
    if username=='whh' and pwd=='123456':
        return "登录成功"
    return "登录失败"

测试用例脚本

from login import login
import pytest
class TestCase:
    def test_01(self):
        result=login("whh",'123456')
        assert result=="登录成功"
    def test_03(self):
        result=login("hhh",'123')
        assert result=="登录成功"
    def test_04(self):
        result=login('whh','123456')
        assert result=="登录失败"

在控制台执行 pytest --html=./report/report.html test_01.py
运行结果如下
pythont中失败用例重新执行_第1张图片
html报告
pythont中失败用例重新执行_第2张图片

第二种方式

需要在重跑测试用例脚本前面加上@pytest.mark.flaky(reruns=2,reruns-delay=2)
如下代码

from login import login
import pytest
class TestCase:
    def test_01(self):
        result=login("whh",'123456')
        assert result=="登录成功"
    @pytest.mark.flaky(reruns=2,reruns_delay=2)
    def test_03(self):
        result=login("hhh",'123')
        assert result=="登录成功"
    @pytest.mark.flaky(reruns=1,reruns_delay=2)
    def test_04(self):
        result=login('whh','123456')
        assert result=="登录失败"

运行方式

E:\web_ketangpai>pytest --html=report/report.html test_01.py
========================================================= test session starts ==========================================================
platform win32 -- Python 3.7.3, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: E:\web_ketangpai
plugins: allure-pytest-2.9.43, Faker-8.5.1, html-3.1.1, metadata-1.11.0, rerunfailures-10.0
collected 3 items                                                                                                                       

test_01.py .RRFR                                                                                                                  [100%]F
 [100%]

=============================================================== FAILURES ===============================================================
___________________________________________________________ TestCase.test_03 ___________________________________________________________

self = <test_01.TestCase object at 0x0000018513329E10>

    @pytest.mark.flaky(reruns=2,reruns_delay=2)
    def test_03(self):
        result=login("hhh",'123')
>       assert result=="登录成功"
E       AssertionError: assert '登录失败' == '登录成功'
E         - 登录成功
E         + 登录失败

test_01.py:10: AssertionError
___________________________________________________________ TestCase.test_04 ___________________________________________________________

self = <test_01.TestCase object at 0x0000018513346470>

    @pytest.mark.flaky(reruns=1,reruns_delay=2)
    def test_04(self):
        result=login('whh','123456')
>       assert result=="登录失败"
E       AssertionError: assert '登录成功' == '登录失败'
E         - 登录失败
E         + 登录成功

test_01.py:14: AssertionError
----------------------------------- generated html file: file://E:\web_ketangpai\report\report.html ------------------------------------
======================================================= short test summary info ========================================================
FAILED test_01.py::TestCase::test_03 - AssertionError: assert '登录失败' == '登录成功'
FAILED test_01.py::TestCase::test_04 - AssertionError: assert '登录成功' == '登录失败'
================================================= 2 failed, 1 passed, 3 rerun in 6.27s =================================================


pythont中失败用例重新执行_第3张图片

你可能感兴趣的:(python)