Louis GRIMALDI
Louis GRIMALDI

Reputation: 101

Cache fixture function result Pytest

I have a series of python tests to test the equality of two dataframes:

@pytest.fixture()
def prepare_data():
    (...)
    (time-consuming operations running on a cloud instance)

    return df1, df2

test_columns_are_equal(prepare_data):
    assert prepare_data[0].columns == prepare_data[1].columns

test_schemas_are_equal(prepare_data):
    assert prepare_data[0].dtypes.equals(prepare_data[1].dtypes)

test_something_else(prepare_data):
    assert(...)

Now the problem is that prepare_data is being re-run for each test, and triggering a lot of operations on my server.

What I want is to cache the results of prepare_data() the first time it is called, and then just run the tests on the cached outputs.

Upvotes: 1

Views: 1504

Answers (1)

SuaveIncompetence
SuaveIncompetence

Reputation: 513

Better late than never, @pytest.fixture(scope='module') will only generate the fixture once for the whole module and share that between the tests that access it. There are also scopes of 'class', 'session' and 'package' for other scenarios.

Note: Be aware that if a test case mutates the fixture then you will have unexpected test behaviour depending on which collections of tests are executed and in which order. This is why the default is to generate the fixture separately for each test case.

See the scope session of pytest fixture documentation. https://docs.pytest.org/en/latest/reference/reference.html#pytest.fixture

Upvotes: 3

Related Questions