Skip to content

Commit

Permalink
Fix issue #209 (#210)
Browse files Browse the repository at this point in the history
handle skip and error on setup
  • Loading branch information
lukasNebr authored Mar 9, 2023
1 parent 15a3e94 commit 5676365
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Changelog
11.2 (unreleased)
-----------------

- Nothing changed yet.
Bug fixes
+++++++++

- Execute teardown when test was skipped in setup phase of a fixture.


11.1.1 (2023-02-17)
Expand Down
16 changes: 8 additions & 8 deletions pytest_rerunfailures.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,8 @@ def pytest_runtest_teardown(item, nextitem):
# -> teardown needs to be skipped as well
return

# teardown when test not failed or rerun limit exceeded
if item.execution_count > reruns or getattr(item, "test_failed", None) is False:
item.teardown()
else:
_test_failed_statuses = getattr(item, "_test_failed_statuses", {})
if item.execution_count <= reruns and any(_test_failed_statuses.values()):
# clean cashed results from any level of setups
_remove_cached_results_from_failed_fixtures(item)

Expand All @@ -534,15 +532,17 @@ def pytest_runtest_teardown(item, nextitem):
if node != item:
item.session._setupstate.stack.remove(node)

item.teardown()


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
result = outcome.get_result()
if call.when == "call":
item.test_failed = result.failed
if result.when == "setup":
# clean failed statuses at the beginning of each test/rerun
setattr(item, "_test_failed_statuses", {})
_test_failed_statuses = getattr(item, "_test_failed_statuses", {})
_test_failed_statuses[result.when] = result.failed
item._test_failed_statuses = _test_failed_statuses


def pytest_runtest_protocol(item, nextitem):
Expand Down
44 changes: 43 additions & 1 deletion test_pytest_rerunfailures.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,20 @@ def function_fixture():
yield
logging.info('function teardown')
@pytest.fixture(scope='function')
def function_skip_fixture():
logging.info('skip fixture setup')
pytest.skip('some reason')
yield
logging.info('skip fixture teardown')
@pytest.fixture(scope='function')
def function_setup_fail_fixture():
logging.info('fail fixture setup')
assert False
yield
logging.info('fail fixture teardown')
class TestFirstPassLastFail:
@staticmethod
Expand Down Expand Up @@ -775,6 +789,16 @@ def test_2():
logging.info("TestSkipLast 2")
assert False
class TestSkipFixture:
@staticmethod
def test_1(function_skip_fixture):
logging.info("TestSkipFixture 1")
class TestSetupFailed:
@staticmethod
def test_1(function_setup_fail_fixture):
logging.info("TestSetupFailed 1")
class TestTestCaseFailFirstFailLast(TestCase):
@staticmethod
Expand Down Expand Up @@ -874,6 +898,24 @@ def test_2():
mock.call("TestSkipLast 1"),
mock.call("function teardown"),
mock.call("class teardown"),
# TestSkipFixture
mock.call("class setup"),
mock.call("function setup"),
mock.call("skip fixture setup"),
mock.call("function teardown"),
mock.call("class teardown"),
# TestSetupFailed
mock.call("class setup"),
mock.call("function setup"),
mock.call("fail fixture setup"),
mock.call("function teardown"),
mock.call("function setup"),
mock.call("fail fixture setup"),
mock.call("function teardown"),
mock.call("function setup"),
mock.call("fail fixture setup"),
mock.call("function teardown"),
mock.call("class teardown"),
# TestTestCaseFailFirstFailLast
mock.call("class setup"),
mock.call("function setup"),
Expand Down Expand Up @@ -923,4 +965,4 @@ def test_2():
]

logging.info.assert_has_calls(expected_calls, any_order=False)
assert_outcomes(result, failed=8, passed=2, rerun=16, skipped=4)
assert_outcomes(result, failed=8, passed=2, rerun=18, skipped=5, error=1)

0 comments on commit 5676365

Please sign in to comment.