Pytest----失败N次后停止执行

简介: Pytest----失败N次后停止执行

【原文链接】

1 默认情况下,所有用例都会执行完成

测试脚本代码如下:

def test_1():
    print("in test_1")
    assert 1==1

def test_2():
    print("in test_2")
    assert 1==2

def test_3():
    print("in test_3")
    assert 1==1

def test_4():
    print("in test_4")
    assert 1==2

使用pytest 命令执行结果如下:即,四个用例全部执行

G:\redrose2100\src\demo>pytest
========================================================================== test session starts ===========================================================================
platform win32 -- Python 3.9.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: G:\redrose2100\src\demo
plugins: rerunfailures-10.0
collected 4 items                                                                                                                                                         

test_example.py .F.F                                                                                                                                                [100%]

================================================================================ FAILURES ================================================================================
_________________________________________________________________________________ test_2 _________________________________________________________________________________

    def test_2():
        print("in test_2")
>       assert 1==2
E       assert 1 == 2

test_example.py:8: AssertionError
-------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------
in test_2
_________________________________________________________________________________ test_4 _________________________________________________________________________________

    def test_4():
        print("in test_4")
>       assert 1==2
E       assert 1 == 2

test_example.py:16: AssertionError
-------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------
in test_4
======================================================================== short test summary info =========================================================================
FAILED test_example.py::test_2 - assert 1 == 2
FAILED test_example.py::test_4 - assert 1 == 2
====================================================================== 2 failed, 2 passed in 0.13s =======================================================================

G:\redrose2100\src\demo>
  • 当希望在有用例失败的情况下,即停止执行后续其他用例,使用 pytest -x即可

如下,使用 pytest -x 执行的结果,即执行到第二个用例是断言错误,因此停止不在执行后续的两个用例

G:\redrose2100\src\demo>pytest -x
========================================================================== test session starts ===========================================================================
platform win32 -- Python 3.9.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: G:\redrose2100\src\demo
plugins: rerunfailures-10.0
collected 4 items                                                                                                                                                         

test_example.py .F

================================================================================ FAILURES ================================================================================
_________________________________________________________________________________ test_2 _________________________________________________________________________________

    def test_2():
        print("in test_2")
>       assert 1==2
E       assert 1 == 2

test_example.py:8: AssertionError
-------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------
in test_2
======================================================================== short test summary info =========================================================================
FAILED test_example.py::test_2 - assert 1 == 2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
====================================================================== 1 failed, 1 passed in 0.12s =======================================================================

G:\redrose2100\src\demo>

2 通过 pytest --maxfail 可以控制做多失败几次,即停止运行

用例内容再增加两个,如下:

def test_1():
    print("in test_1")
    assert 1==1

def test_2():
    print("in test_2")
    assert 1==2

def test_3():
    print("in test_3")
    assert 1==1

def test_4():
    print("in test_4")
    assert 1==2

def test_5():
    print("in test_5")
    assert 1==1

def test_6():
    print("in test_4")
    assert 1==2

使用 pytest --maxfail 2 即最多遇到两次失败用例,则停止执行后面其他用例,此场景主要用于比如当环境等因素出现异常时,可以设置比如正常情况下几乎所有用例均没有问题,但是当出现比如N次用例失败时,基本可以说明当前环境有问题了,此种情景下,此参数就很有用了

G:\redrose2100\src\demo>pytest --maxfail 2
========================================================================== test session starts ===========================================================================
platform win32 -- Python 3.9.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: G:\redrose2100\src\demo
plugins: rerunfailures-10.0
collected 6 items                                                                                                                                                         

test_example.py .F.F

================================================================================ FAILURES ================================================================================
_________________________________________________________________________________ test_2 _________________________________________________________________________________

    def test_2():
        print("in test_2")
>       assert 1==2
E       assert 1 == 2

test_example.py:8: AssertionError
-------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------
in test_2
_________________________________________________________________________________ test_4 _________________________________________________________________________________

    def test_4():
        print("in test_4")
>       assert 1==2
E       assert 1 == 2

test_example.py:16: AssertionError
-------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------
in test_4
======================================================================== short test summary info =========================================================================
FAILED test_example.py::test_2 - assert 1 == 2
FAILED test_example.py::test_4 - assert 1 == 2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 2 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
====================================================================== 2 failed, 2 passed in 0.14s =======================================================================

G:\redrose2100\src\demo>
目录
相关文章
|
7月前
|
Python
【python脚本】执行过程中触发若干次就停止执行脚本的方式
【python脚本】执行过程中触发若干次就停止执行脚本的方式
|
4天前
|
Python
使用break语句提前终止循环
在编程中,循环结构是一种常用的控制流程机制,它允许代码块重复执行,直到满足某个条件为止。然而,在某些情况下,我们可能希望在循环的某个点提前终止循环,而不是等到满足循环条件。这时,我们可以使用break语句来实现这一功能。
18 1
|
4天前
|
Shell Linux Windows
从您描述的情况来看,您在执行`exit`命令后,程序立即终止了
【2月更文挑战第32天】从您描述的情况来看,您在执行`exit`命令后,程序立即终止了
12 1
|
10月前
|
C语言 C++
【C++】 --- 写个函数在main函数执行前先运行
【C++】 --- 写个函数在main函数执行前先运行
89 0
|
8月前
|
算法
29.【算法五章-----03(未完毕)】
29.【算法五章-----03(未完毕)】
26 0
29.【算法五章-----03(未完毕)】
|
10月前
|
测试技术
29-pytest-运行上次失败用例
29-pytest-运行上次失败用例
|
Python
[oeasy]python0026_刷新时间_延迟时间_time_sleep_死循环_while_True
[oeasy]python0026_刷新时间_延迟时间_time_sleep_死循环_while_True
79 0
[oeasy]python0026_刷新时间_延迟时间_time_sleep_死循环_while_True
|
缓存 测试技术
Pytest----如何重执行失败用例
Pytest----如何重执行失败用例
358 0
|
测试技术
Pytest系列(5) - 测试用例执行后的几种状态
Pytest系列(5) - 测试用例执行后的几种状态
227 0
|
程序员 Shell
如何重复执行一条命令直至运行成功?
大家好,我是良许。 在我们的日常工作中,需要我们重复做的工作简直不能太多。比如,我们想要确认网络是否是连通的,传统的做法就是使用 ping 命令不停去测试某个地址(比如百度)。网络比较好还好说,但如果网络很差,那么就需要一直去运行 ping 命令。 作为程序员,重复性的工作怎么能忍呢?只要是重复性的工作,就有可能使用编程的方式来解决! 下面良许就介绍两种方法重复执行一条命令直至运行成功。 (PS:本文适合初学者,高手可绕道) 解决重复性的工作,自然而然会想到循环 。在 Shell 里,循环无非 3 种:for、while、until 。在本文里,我们使用后两种循环:while 、un
163 0