测试是一个函数,问题是您的代码,如所写,只有一个测试 (test_TestsRunAfterFailure)。当代码遇到第一个失败的assert 语句时,该测试就失败了。
如果您想运行多个测试,您需要:
第二种解决方案可能是您想要的,可能看起来像这样:
import pytest
@pytest.mark.parametrize('value', [4, 5, 6])
def test_TestsRunAfterFailure(value):
x = [1, 2, 3, 4]
assert (value in x)
运行上面的代码会产生如下输出:
===================================== test session starts =====================================
platform linux -- Python 3.9.4, pytest-6.0.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/lars/tmp/python
plugins: cov-2.11.1, flake8-1.0.7, asyncio-0.14.0, xonsh-0.9.26
collected 3 items
test_values.py .FF [100%]
========================================== FAILURES ===========================================
________________________________ test_TestsRunAfterFailure[5] _________________________________
value = 5
@pytest.mark.parametrize('value', [4, 5, 6])
def test_TestsRunAfterFailure(value):
x = [1, 2, 3, 4]
> assert (value in x)
E assert 5 in [1, 2, 3, 4]
test_values.py:6: AssertionError
________________________________ test_TestsRunAfterFailure[6] _________________________________
value = 6
@pytest.mark.parametrize('value', [4, 5, 6])
def test_TestsRunAfterFailure(value):
x = [1, 2, 3, 4]
> assert (value in x)
E assert 6 in [1, 2, 3, 4]
test_values.py:6: AssertionError
=================================== short test summary info ===================================
FAILED test_values.py::test_TestsRunAfterFailure[5] - assert 5 in [1, 2, 3, 4]
FAILED test_values.py::test_TestsRunAfterFailure[6] - assert 6 in [1, 2, 3, 4]
================================= 2 failed, 1 passed in 0.15s =================================