Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 623
Alnitak Avatar asked Nov 05 '20 03:11

Alnitak


People also ask

Are pytest fixtures cached?

Pytest only caches one instance of a fixture at a time, which means that when using a parametrized fixture, pytest may invoke a fixture more than once in the given scope.

How often is a fixture function in pytest executed?

Fixtures with scope session will only be executed once per session. Every time you run pytest , it's considered to be one session.

How does fixture work in pytest?

pytest fixtures are functions attached to the tests which run before the test function is executed. Fixtures are a set of resources that have to be set up before and cleaned up once the Selenium test automation execution is completed.

How many times will a fixture of class scope run for the tests of a class?

Class: With Class scope, one fixture will be created per class object. Session: With the Session scope, the fixture will be created only once for entire test session.

How to access fixture function in pytest?

To access the fixture function, the tests have to mention the fixture name as input parameter. Pytest while the test is getting executed, will see the fixture name as input parameter.

How to use pytest to test a python function?

Import the pytest module in all Python files you want to test, as well as in any associated configuration files for the fixtures. We have to indicate that the function is a fixture with @pytest.fixture.

What is the difference between module and class in pytest fixture?

Module – As the name indicates, a fixture function with scope as Module is created (or invoked) only once per module. Class – The fixture function is created once per class object. The scope of pytest fixture function can be supplied with the @pytest.fixture marker

How to manage everything in pytest?

Everything is managed by the pytest fixture system. Each method only has to request the fixtures that it actually needs without worrying about order. This is because the act fixture is an autouse fixture, and it made sure all the other fixtures executed before it.


1 Answers

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.

like image 70
hoefling Avatar answered Oct 04 '22 17:10

hoefling