Skip to content
This repository was archived by the owner on Aug 13, 2025. It is now read-only.

Commit 0c2bdbc

Browse files
committed
Keep the tempdir in case test setup/teardown fails
When an issue occurs inside a fixture (e.g. servers fail to start/stop), the test result won't be detected as failed, but rather an error will be thrown. To ensure the tempdir is kept even if the test itself passes but the system_test() fixture throws an error, a different mechanism is needed. At the start of the critical test setup section, note that the fixture hasn't finished yet. When this is detected in the system_test_dir() fixture, it is recognized as error in test setup/teardown and the temp directory is kept. This may seem cumbersome, because it is. It's basically a workaround for the way pytest handles fixtures and test errors in general.
1 parent b5c7ccf commit 0c2bdbc

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

bin/tests/system/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def control_port():
6666

6767
# ----------------------- Globals definition -----------------------------
6868

69+
FIXTURE_OK = pytest.StashKey[bool]() # pylint: disable=no-member
6970
LOG_FORMAT = "%(asctime)s %(levelname)7s:%(name)s %(message)s"
7071
XDIST_WORKER = os.environ.get("PYTEST_XDIST_WORKER", "")
7172
FILE_DIR = os.path.abspath(Path(__file__).parent)
@@ -313,6 +314,10 @@ def get_test_result():
313314
logger.debug("--noclean requested, keeping temporary directory")
314315
elif result == "failed":
315316
logger.debug("test failure detected, keeping temporary directory")
317+
elif not request.node.stash[FIXTURE_OK]:
318+
logger.debug(
319+
"test setup/teardown issue detected, keeping temporary directory"
320+
)
316321
else:
317322
logger.debug("deleting temporary directory")
318323
shutil.rmtree(testdir)
@@ -453,10 +458,17 @@ def get_core_dumps():
453458
port = int(env["PORT"])
454459
logger.info("using port range: <%d, %d>", port, port + PORTS_PER_TEST - 1)
455460

461+
request.node.stash[FIXTURE_OK] = True
462+
456463
# Perform checks which may skip this test.
457464
check_net_interfaces()
458465
check_prerequisites()
459466

467+
# Store the fact that this fixture hasn't successfully finished yet.
468+
# This is checked before temporary directory teardown to decide whether
469+
# it's okay to remove the directory.
470+
request.node.stash[FIXTURE_OK] = False
471+
460472
setup_test()
461473
try:
462474
start_servers()
@@ -466,3 +478,4 @@ def get_core_dumps():
466478
logger.debug("test(s) finished")
467479
stop_servers()
468480
get_core_dumps()
481+
request.node.stash[FIXTURE_OK] = True

0 commit comments

Comments
 (0)