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
运行结果如下
在这里插入图片描述
html报告
在这里插入图片描述

第二种方式

需要在重跑测试用例脚本前面加上@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 =================================================


在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值