Organizing tests and fixtures such that each element of a data object can be tested individually using pytest.parametrize - Stac

admin2025-04-19  0

I have a hierarchy of fixtures that collect and generate data. Let's assume

@pytest.fixture
def directories():
    """
    get a list of directories to retrieve data from
    this could be read from a config or a glob pattern passed from a CL argument
    """

@pytest.fixture
def feature_files(directories):
    """
    grabs a list of files which are used to generate data
    each directory has many such files
    """

 @pytest.fixture
 def data_objects(feature_file):
    """
    generate a test data object
    the underlying structure is unimportant
    what matters is each feature_file has many data_objects that are created/featurized
    """

The objective in using these as fixtures is such that creating these test cases are expensive,is dynamic on runtime arguments or a config that could be shared across many tests in many test modules (scope="session")

Ultimately I want each data object to be tested on a test function.

If this would have worked.

pytest.mark.parametrize("dataset", data_objects):
    assert (some_test)

  1. I understand that parametrize and test node creation is done under the collection phase and fixtures are executed in the execution phase. You can't call fixtures directly. So I understand that I might have to use an alternative to yield a test object

  2. I understand that I can't use fixture parametrization (to the best of my knowledge). Since each fixture is many to many, I can't use the param parameter in the fixture decorator. Even if I did each fixture would still lead to a list. So fixture parametrization is out.

  3. From the documentation of pytest-cases, I can get the value of a fixture inside pytest-cases' own parametrize decorator using fixture_ref, but only if its a scalar, not a list.

So I'm trying to find an alternative way to anize the tests such that fixtures are used and the data objects so that multiple tests reuse the same fixture during the session and using the same test function on a list of data objects, but there being a unique version of the test_function for each test object (like in parametrize).

One thing I haven't tried is iterating through a for loop in a function with a fixture as a parameter (so I assume this function has to be a fixture as well) and adding a case dectorator with an id from from the element

Something like this

@fixture(data_objects):
   for do in data_objects: 
       @case(id={some id retrieved from do})
       def dataset():
           return do

I don't think this would work. So I'm looking for alternative suggestions

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745071574a283312.html

最新回复(0)