Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 284
user2304916 Avatar asked Mar 23 '14 13:03

user2304916


People also ask

How do you execute the same test case with multiple sets of data?

By using @Factory and @DataProvider annotation of TestNG you can execute same test-case multiple times with different data.

How do you test a dataset?

A simple evaluation method is a train test dataset where the dataset is divided into a train and a test dataset, then the learning model is trained using the train data and performance is measured using the test data. In a more sophisticated approach, the entire dataset is used to train and test a given model.


1 Answers

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
like image 154
falsetru Avatar answered Oct 04 '22 22:10

falsetru