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>