From 93cff6d4186d06c5f3e80d02a4d3e1fec8b1b7a6 Mon Sep 17 00:00:00 2001 From: Lorenzo Gorini Date: Wed, 26 Aug 2020 18:36:29 +0200 Subject: [PATCH] Moved temporary_data_dir fixture to conftest.py since it is a generic fixture that may be useful to multiple test scripts --- src/tests/conftest.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/tests/conftest.py diff --git a/src/tests/conftest.py b/src/tests/conftest.py new file mode 100644 index 0000000..94d86fa --- /dev/null +++ b/src/tests/conftest.py @@ -0,0 +1,38 @@ +import os +import shutil +from pathlib import Path + +import pytest + + +@pytest.fixture(scope="module") +def temporary_data_dir(request) -> Path: + """ + Create a temporary directory for test data and delete it after test end. + + The temporary directory is created in the working directory and it is + named "temp_test_data_folder". + The fixture uses a finalizer that deletes the temporary directory where + every test data was saved. Therefore every time the user calls tests that + use this fixture (and save data inside the returned directory), at the end + of the test the finalizer deletes this directory. + + Parameters + ---------- + + Returns + ------- + Path + Path where every temporary file used by tests is saved. + """ + temp_data_dir = Path(os.getcwd()) / "temp_test_data_folder" + try: + os.mkdir(temp_data_dir) + except FileExistsError: + pass + + def remove_temp_dir_created(): + shutil.rmtree(temp_data_dir) + + request.addfinalizer(remove_temp_dir_created) + return temp_data_dir