I'm trying to develop a tool that takes files from a folder and creates random orders for loading this files later following some constrains. The idea is to prerandomize files for behavioural experiments in which there are several categories, so the main constrain is that it should support categories in the names of the files (like 'sentence', 'nonword', etc.). This is done by generating csv files with the names of the files in the intended order, after performing operations in them.
I need to generate two temporal directories: one containing named files (i.e. 'sentence_01' up to 'sentence_x'), and another one empty, to give to the function under test to generate the files there.
Right now I have something like this:
import tempfile
import pytest
def set_up_files(file_number, cat=False, categories=None):
input_folder = tempfile.mkdtemp()
output_folder = tempfile.mkdtemp()
if cat:
for category in categories:
for i in range(file_number):
tempfile.mkstemp(prefix=(category + '%02d' % i), dir=input_folder)
else:
for file in range(file_number):
tempfile.mkstemp(dir=input_folder)
return input_folder, output_folder
And then an test like:
def test_create_prerandomizations_raises(init_params):
temp_input_folder, temp_output_folder = set_up_files(30,
cat=True,
categories=init_params['categories'])
I would like to do this with pytest fixtures in order to move the function to a conftest.py file, and also so I avoid calling the function in each test.
with pytest.raises(ValueError) as excinfo:
create_prerandomizations(temp_input_folder,
3,
temp_output_folder,
categories=init_params['categories'],
subsets=False,
constrained=True,
method='mocos')
exception_msg = excinfo.value.args[0]
assert exception_msg == "method argument must be 'pseudo' or 'pure'"
# Delete temp folders and files
cleanup_files(temp_input_folder, temp_output_folder)
Aucun commentaire:
Enregistrer un commentaire