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

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.

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.

Related

Sessions Level Pytest Fixture

I want to create a suite setup for my Pytest framework.
I have created one fixture name suite setup, which return some values for test cases to consume.
#pytest.fixture(scope=session)
def suite_setup(arg1, arg2 ...)
then i have 3 files test1.py, test2.py andtest3.py file where i have multiple test cases, I want my suite setup to be called once and then all the test cases of .py files should execute and then in the end suite_teardown should execute.
I want order like this
Suite_steup
testcase1
testcase2
testcase..n
suite_teardown
can you please help me with the sytax.
if i run -n 2, my session level fixture should get called only 2 times on different env
i have created conftest with fixture but i am not sure how to call session level fixture only once for all test cases and i want to use values returned by session level fixture in all test cases.
i tried calling scope level fixture in test parameter of test cases, but it gets called everytime a new test case starts
A simple way can be to define the fixture as a context manager by using a yield instead of a standard return. This will easily allow you to specify setup and teardown logic.
You may also wish to use autouse=True to force it to always be run even if the fixture is not explicitly specified as a test argument.
In conftest.py you can add something along the lines of this:
#pytest.fixture(scope="session", autouse=True)
def session_fixture():
fixture = do_setup()
yield fixture
do_teardown(fixture)

pytest fixture that gets data from a database - limit to one call?

I have a fixture that queries a MongoDB database and returns a record.
#pytest.fixture
def chicago():
return GetRecord("CHICAGO,IL")
Each time I pass this fixture into a test, it runs the query again. Is there a best practice on how to make it so I only run this DB call once? I found I can do something like this, but it seems to defeat the purpose of a fixture.
chi = GetRecord("CHICAGO,IL")
#pytest.fixture
def chicago():
return chi
I could just pass in chi instead of declaring a fixture if this is the alternative. Any other solutions?
To have a fixture only evaluated in a certain scope, you can set the fixture scope. As described in the pytest documentation, there are five scopes that affect the context in which a fixture works. Basically a fixture is setup at the begin of the scope, and cleaned up at the end of a scope.
If you need the fixture to be initialized only once over all tests in a session, like in your case, you can use session scope:
#pytest.fixture(scope="session")
def chicago():
return GetRecord("CHICAGO,IL")
This will be evaluated once, as used by the first test, and would be cleaned up, if there were any cleanup code, after all test executions have finished.
Here is a simple example to show this:
import pytest
#pytest.fixture(scope="session")
def the_answer():
print("Calculating...")
yield 6 * 7
print("Answered the question!")
def test_session1(the_answer):
print(the_answer)
def test_session2(the_answer):
print(the_answer)
def test_session3(the_answer):
print(the_answer)
This results in:
$ python -m pytest -s session_based.py
==================== test session starts ====================
...
collected 3 items
session_based.py Calculating...
42
.42
.42
.Answered the question!
==================== 3 passed in 0.32s ====================
As you can see, the "calculation" is done only once before the first test, and the "cleanup" is done after all tests have finished.
Respectively, the rarely used package-scoped fixtures are executed once in a test package, module-scoped tests are executed once in a test module and cleaned up after the tests in that module finish, class-scoped tests are executed once per test class, and function-scoped fixtures (the default) have the lifetime of a single test that uses them.
I'm quite sure that has been answered before, but as I could not find a matching answer, here you are.

How to execute a parameterized fixture for only some of the parameters?

From the official documentation, in the example about parametrizing fixtures:
Parametrizing fixtures
Extending the previous example, we can flag the fixture to create two smtp_connection fixture instances which will cause all tests using the fixture to run twice.
#pytest.fixture(scope="module", params=["smtp.gmail.com", "mail.python.org"])
def smtp_connection(request):
I wrote a parametrized fixture like the above example, but now my problem is I want to use the fixture in a different test function that should only execute once for one of the parameters...Like so:
def my_test_function(smtp_connection)
# I want this function to execute only once for the first or the second parameter...
So my question is: Can a test function use the fixture and be executed only for some parameters using pytest API? Or is this use case already a mistake and should both the fixture or the test functions be implemented differently in such case? If so, what would conceptually be the correct design alternative?
I am looking for a programmatic solution that doesn't require using command-line flags when I run pytest.
You could use indirect fixture parametrization - in this case you would define the parameters you want to use in the test instead of the fixture:
#pytest.fixture(scope="module")
def smtp_connection(request):
url = request.param
...
pytest.mark.parametrize("smtp_connection", ["smtp.gmail.com", "mail.python.org"], indirect=True)
def def my_test_function(smtp_connection):
...
pytest.mark.parametrize("smtp_connection", ["smtp.gmail.com"], indirect=True)
def def my_other_test_function(smtp_connection):
...
This will parametrize the fixture with each of the parameters you provide in the list for a specific test. You can read the parameter from request.param as shown above.
Of course, if you have many tests that use the same parameters you are probably better off using specific fixtures instead.

Module level fixture is not running

I want to have a specific setup/tear down fixture for one of the test modules. Obviously, I want it to run the setup code once before all the tests in the module, and once after all tests are done.
So, I've came up with this:
import pytest
#pytest.fixture(scope="module")
def setup_and_teardown():
print("Start")
yield
print("End")
def test_checking():
print("Checking")
assert True
This does not work that way. It will only work if I provide setup_and_teardown as an argument to the first test in the module.
Is this the way it's supposed to work? Isn't it supposed to be run automatically if I mark it as a module level fixture?
Module-scoped fixtures behave the same as fixtures of any other scope - they are only used if they are explicitely passed in a test, marked using #pytest.mark.usefixtures, or have autouse=True set:
#pytest.fixture(scope="module", autouse=True)
def setup_and_teardown():
print("setup")
yield
print("teardown")
For module- and session-scoped fixtures that do the setup/teardown as in your example, this is the most commonly used option.
For fixtures that yield an object (for example an expansive resource that shall only be allocated once) that is accessed in the test, this does not make sense, because the fixture has to be passed to the test to be accessible. Also, it may not be needed in all tests.

Using pytest to do setup and teardown

I have a handful of tests in my test module that need some common setup and teardown to run before and after the test. I don't need the setup and teardown to run for every function, just a handful of them. I've found I can kind of do this with fixtures
#pytest.fixture
def reset_env():
env = copy.deepcopy(os.environ)
yield None
os.environ = env
def test_that_does_some_env_manipulation(reset_env):
# do some tests
I don't actually need to return anything from the fixture to use in the test function, though, so I really don't need the argument. I'm only using it to trigger setup and teardown.
Is there a way to specify that a test function uses a setup/teardown fixture without needing the fixture argument? Maybe a decorator to say that a test function uses a certain fixture?
Thanks to hoefling's comment above
#pytest.mark.usefixtures('reset_env')
def test_that_does_some_env_manipulation():
# do some tests
You could use autouse=True in your fixture. Autouse automatically executes the fixture at the beginning of fixture scope.
In your code:
#pytest.fixture(autouse=True)
def reset_env():
env = copy.deepcopy(os.environ)
yield None
os.environ = env
def test_that_does_some_env_manipulation():
# do some tests
But you need to be careful about the scope of the fixture as the fixture would be triggered for each scope. If you have all such tests under one directory, you can have it in a conftest file of the directory. Otherwise, you can declare the fixture in the test file.
Relevant pytest help doc

Categories

Resources