Search code examples
pythonunit-testingnosepytest

Run same test on multiple datasets


I'm starting to use pytest to add unit test to a software that can analyse different kind of datasets.

I wrote a set of test functions that I would like to apply to different datasets. One complication is that the datasets are quite big, so I would like to do:

  • Load dataset1
  • Run tests
  • Load dataset2
  • Run tests

and so on.

Right now I'm able to use one dataset using a fixture:

@pytest.fixture(scope="module")
def data():
    return load_dataset1()

and then passing datato each test function.

I know that I can pass the params keyword to pytest.fixture. But, how can I implement the sequential load of the different datasets (not loading all of them in RAM at the same time)?


Solution

  • Use params as you mentioned:

    @pytest.fixture(scope='module', params=[load_dataset1, load_dataset2])
    def data(request):
        loader = request.param
        dataset = loader()
        return dataset
    

    Use fixture finalization if you want to do fixture specific finalization:

    @pytest.fixture(scope='module', params=[load_dataset1, load_dataset2])
    def data(request):
        loader = request.param
        dataset = loader()
        def fin():
            # finalize dataset-related resource
            pass
        request.addfinalizer(fin)
        return dataset