Search code examples
pythonunit-testingpytest

Does Pytest cache fixture data when called by multiple test functions?


I have unit tests that require test data. This test data is downloaded and has decently large filesize.

@pytest.fixture
def large_data_file():
    large_data = download_some_data('some_token')
    return large_data

# Perform tests with input data
def test_foo(large_data_file): pass
def test_bar(large_data_file): pass
def test_baz(large_data_file): pass
# ... and so on

I do not want to download this data more than once. It should only be downloaded once, and passed to all the tests that require it. Does pytest call on large_data_file once and use it for every unit test that uses that fixture, or does it call the large_data_file each time?

In unittest, you would simply download the data for all the tests once in the setUpClass method.

I would rather not just have a global large_data_file = download_some_data('some_token') in this py script. I would like to know how to handle this use-case with Pytest.


Solution

  • Does pytest call on large_data_file once and use it for every unit test that uses that fixture, or does it call the large_data_file each time?

    It depends on the fixture scope. The default scope is function, so in your example large_data_file will be evaluated three times. If you broaden the scope, e.g.

    @pytest.fixture(scope="session")
    def large_data_file():
        ...
    

    the fixture will be evaluated once per test session and the result will be cached and reused in all dependent tests. Check out the section Scope: sharing fixtures across classes, modules, packages or session in pytest docs for more details.