Skip to content

Commit 5676365

Browse files
authored
Fix issue #209 (#210)
handle skip and error on setup
1 parent 15a3e94 commit 5676365

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

CHANGES.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Changelog
44
11.2 (unreleased)
55
-----------------
66

7-
- Nothing changed yet.
7+
Bug fixes
8+
+++++++++
9+
10+
- Execute teardown when test was skipped in setup phase of a fixture.
811

912

1013
11.1.1 (2023-02-17)

pytest_rerunfailures.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,8 @@ def pytest_runtest_teardown(item, nextitem):
517517
# -> teardown needs to be skipped as well
518518
return
519519

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

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

537-
item.teardown()
538-
539535

540536
@pytest.hookimpl(hookwrapper=True)
541537
def pytest_runtest_makereport(item, call):
542538
outcome = yield
543539
result = outcome.get_result()
544-
if call.when == "call":
545-
item.test_failed = result.failed
540+
if result.when == "setup":
541+
# clean failed statuses at the beginning of each test/rerun
542+
setattr(item, "_test_failed_statuses", {})
543+
_test_failed_statuses = getattr(item, "_test_failed_statuses", {})
544+
_test_failed_statuses[result.when] = result.failed
545+
item._test_failed_statuses = _test_failed_statuses
546546

547547

548548
def pytest_runtest_protocol(item, nextitem):

test_pytest_rerunfailures.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,20 @@ def function_fixture():
729729
yield
730730
logging.info('function teardown')
731731
732+
@pytest.fixture(scope='function')
733+
def function_skip_fixture():
734+
logging.info('skip fixture setup')
735+
pytest.skip('some reason')
736+
yield
737+
logging.info('skip fixture teardown')
738+
739+
@pytest.fixture(scope='function')
740+
def function_setup_fail_fixture():
741+
logging.info('fail fixture setup')
742+
assert False
743+
yield
744+
logging.info('fail fixture teardown')
745+
732746
class TestFirstPassLastFail:
733747
734748
@staticmethod
@@ -775,6 +789,16 @@ def test_2():
775789
logging.info("TestSkipLast 2")
776790
assert False
777791
792+
class TestSkipFixture:
793+
@staticmethod
794+
def test_1(function_skip_fixture):
795+
logging.info("TestSkipFixture 1")
796+
797+
class TestSetupFailed:
798+
@staticmethod
799+
def test_1(function_setup_fail_fixture):
800+
logging.info("TestSetupFailed 1")
801+
778802
class TestTestCaseFailFirstFailLast(TestCase):
779803
780804
@staticmethod
@@ -874,6 +898,24 @@ def test_2():
874898
mock.call("TestSkipLast 1"),
875899
mock.call("function teardown"),
876900
mock.call("class teardown"),
901+
# TestSkipFixture
902+
mock.call("class setup"),
903+
mock.call("function setup"),
904+
mock.call("skip fixture setup"),
905+
mock.call("function teardown"),
906+
mock.call("class teardown"),
907+
# TestSetupFailed
908+
mock.call("class setup"),
909+
mock.call("function setup"),
910+
mock.call("fail fixture setup"),
911+
mock.call("function teardown"),
912+
mock.call("function setup"),
913+
mock.call("fail fixture setup"),
914+
mock.call("function teardown"),
915+
mock.call("function setup"),
916+
mock.call("fail fixture setup"),
917+
mock.call("function teardown"),
918+
mock.call("class teardown"),
877919
# TestTestCaseFailFirstFailLast
878920
mock.call("class setup"),
879921
mock.call("function setup"),
@@ -923,4 +965,4 @@ def test_2():
923965
]
924966

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

0 commit comments

Comments
 (0)