Skip to content

Commit

Permalink
Moved temporary_data_dir fixture to conftest.py since it is a generic…
Browse files Browse the repository at this point in the history
… fixture that may be useful to multiple test scripts
  • Loading branch information
lorenz-gorini authored and alessiamarcolini committed Aug 28, 2020
1 parent 1179b56 commit 14925ae
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 14925ae

Please sign in to comment.