I need a way to generate a dictionary containing unique paths for every pytest module/file in a given directory. It needs to be accessible before any test is even run to define the paths for the file:
TEST_DIRS = {}
path1 = TEST_DIRS['path']
path2 = TEST_DIRS['input']
def test_something():
target = path1 / 'input_file'
....
def test_another_thing():
....
Also there should be directory config file like conftest to perform the generating actions required by each module. Generator would create directories based on the test name.
I tried using conftest.py
file and pytest_collection_modify_items
but it still makes the TEST_DIRS work only inside test function not before them.
def pytest_collection_modifyitems(items):
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
for item in items:
module_name = item.module.__name__
test_paths = {
key: value.format(test_name=module_name, timestamp=timestamp)
for key, value in BASE_TEST_DIRS.items()
}
test_paths = {
key: home / Path(value)
for key, value in test_paths.items()
}
for key, path in test_paths.items():
if key.startswith('output'):
if not os.path.exists(path):
os.makedirs(path)
item.module.TEST_DIRS = test_paths
1