Skip to content
Snippets Groups Projects
Commit 18910fde authored by lukas leufen's avatar lukas leufen
Browse files

pytest_runtest_teardown runs now a proper clean up

parent e14f3474
No related branches found
No related tags found
2 merge requests!24include recent development,!20not distributed training
Pipeline #27158 passed
...@@ -3,11 +3,23 @@ import shutil ...@@ -3,11 +3,23 @@ import shutil
def pytest_runtest_teardown(item, nextitem): 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: if nextitem is None or item.cls != nextitem.cls:
print(f"\nnew class: {item.cls} -> {nextitem.cls if nextitem else None}") # clean up all TestExperiment and data folder that have been created during testing
print(f"\n{nextitem.fspath.dirname}") rel_path = os.path.relpath(item.fspath.dirname, os.path.abspath(__file__))
if "data" in os.listdir(item.fspath.dirname): path = os.path.dirname(__file__)
print("found data") for stage in filter(None, rel_path.replace("..", ".").split("/")):
shutil.rmtree(os.path.join(item.fspath.dirname, "data"), ignore_errors=True) 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: else:
print(f"\nsame class: {item.cls} == {nextitem.cls}") pass # nothing to do if next test is from same test class
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment