From 18910fde6f830cced6434a6c558d837c40bddbf2 Mon Sep 17 00:00:00 2001 From: lukas leufen <l.leufen@fz-juelich.de> Date: Mon, 9 Dec 2019 14:27:05 +0100 Subject: [PATCH] pytest_runtest_teardown runs now a proper clean up --- conftest.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/conftest.py b/conftest.py index 7f6d2911..92d2159c 100644 --- a/conftest.py +++ b/conftest.py @@ -3,11 +3,23 @@ import shutil def pytest_runtest_teardown(item, nextitem): + """ + Teardown method to clean up folder creations during testing. This method is called after each test, but performs + deletions only after an entire test class was executed. + :param item: tested item + :param nextitem: next item (could be None, if no following test is available) + """ if nextitem is None or item.cls != nextitem.cls: - print(f"\nnew class: {item.cls} -> {nextitem.cls if nextitem else None}") - print(f"\n{nextitem.fspath.dirname}") - if "data" in os.listdir(item.fspath.dirname): - print("found data") - shutil.rmtree(os.path.join(item.fspath.dirname, "data"), ignore_errors=True) + # clean up all TestExperiment and data folder that have been created during testing + rel_path = os.path.relpath(item.fspath.dirname, os.path.abspath(__file__)) + path = os.path.dirname(__file__) + for stage in filter(None, rel_path.replace("..", ".").split("/")): + path = os.path.abspath(os.path.join(path, stage)) + list_dir = os.listdir(path) + if "data" in list_dir and path != os.path.dirname(__file__): # do not delete data folder in src + shutil.rmtree(os.path.join(path, "data"), ignore_errors=True) + if "TestExperiment" in list_dir: + shutil.rmtree(os.path.join(path, "TestExperiment"), ignore_errors=True) else: - print(f"\nsame class: {item.cls} == {nextitem.cls}") + pass # nothing to do if next test is from same test class + -- GitLab