I have three test cases with some dependency of two of them on the third one. Namely, tests test_inner_1
and test_inner_2
are independent from each other but their execution makes no sense if test_outher
fails. They both should be run if test test_outher
passes and both should be skipped if test_outher
fails.
The pytest
manual https://pytest.org/latest/example/simple.html
presents some simple example how to implement incremental testing with
test steps. I am trying to apply this approach to my situation and
to implement something like that:
content of conftest.py:
import pytest
def pytest_runtest_makereport(item, call):
if "incremental" in item.keywords:
if call.excinfo is not None:
parent = item.parent
parent._previousfailed = item
def pytest_runtest_setup(item):
if "incremental" in item.keywords:
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail("previous test failed (%s)" % previousfailed.name)
content of test_example.py:
import pytest
@pytest.mark.incremental
class TestUserHandling:
def test_outher(self):
assert 0
class TestInner:
def test_inner_1(self):
assert 0
def test_inner_2(self):
pass
Unfortunately, I have got the output
==================== 2 failed, 1 passed in 0.03 seconds ====================
while expected to get the output
=================== 1 failed, 2 xfailed in 0.03 seconds ====================
How to correct the conftest.py
to get the desired behaviour?