Sessions Level Pytest Fixture - python

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)

Related

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.

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.
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.

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.

How to test the pytest fixture itself?

What is the proper way to test that the pytest fixture itself. Please do not confuse it with using fixture in tests. I want just tests the fixtures correctness by itself.
When trying to call and execute them inside test I am facing:
Fixture "app" called directly. Fixtures are not meant to be called directly
Any input on that will be appreciated. Docs on this topic are not giving me meaningful guidance: https://docs.pytest.org/en/latest/deprecations.html#calling-fixtures-directly
The motivation for testing fixtures itself come to me, because when our test were failing due to bug in fixture this wasn't tracked correctly in our TAP files, what motivated me to test the fixtures stand alone.
pytest has a pytester plugin that was made for the purpose of testing pytest itself and plugins; it executes tests in an isolated run that doesn't affect the current test run. Example:
# conftest.py
import pytest
pytest_plugins = ['pytester']
#pytest.fixture
def spam(request):
yield request.param
The fixture spam has an issue that it will only work with parametrized tests; once it is requested in an unparametrized test, it will raise an AttributeError. This means that we can't test it via a regular test like this:
def test_spam_no_params(spam):
# too late to verify anything - spam already raised in test setup!
# In fact, the body of this test won't be executed at all.
pass
Instead, we execute the test in an isolated test run using the testdir fixture which is provided by the pytester plugin:
import pathlib
import pytest
# an example on how to load the code from the actual test suite
#pytest.fixture
def read_conftest(request):
return pathlib.Path(request.config.rootdir, 'conftest.py').read_text()
def test_spam_fixture(testdir, read_conftest):
# you can create a test suite by providing file contents in different ways, e.g.
testdir.makeconftest(read_conftest)
testdir.makepyfile(
"""
import pytest
#pytest.mark.parametrize('spam', ('eggs', 'bacon'), indirect=True)
def test_spam_parametrized(spam):
assert spam in ['eggs', 'bacon']
def test_spam_no_params(spam):
assert True
""")
result = testdir.runpytest()
# we should have two passed tests and one failed (unarametrized one)
result.assert_outcomes(passed=2, errors=1)
# if we have to, we can analyze the output made by pytest
assert "AttributeError: 'SubRequest' object has no attribute 'param'" in ' '.join(result.outlines)
Another handy possibility of loading test code for the tests is the testdir.copy_example method. Setup the root path in the pytest.ini, for example:
[pytest]
pytester_example_dir = samples_for_fixture_tests
norecursedirs = samples_for_fixture_tests
Now create the file samples_for_fixture_tests/test_spam_fixture/test_x.py with the contents:
import pytest
#pytest.mark.parametrize('spam', ('eggs', 'bacon'), indirect=True)
def test_spam_parametrized(spam):
assert spam in ['eggs', 'bacon']
def test_spam_no_params(spam):
assert True
(it's the same code that was passed as string to testdir.makepyfile before). The above test changes to:
def test_spam_fixture(testdir, read_conftest):
testdir.makeconftest(read_conftest)
# pytest will now copy everything from samples_for_fixture_tests/test_spam_fixture
testdir.copy_example()
testdir.runpytest().assert_outcomes(passed=2, errors=1)
This way, you don't have to maintain Python code as string in tests and can also reuse existing test modules by running them with pytester. You can also configure test data roots via the pytester_example_path mark:
#pytest.mark.pytester_example_path('fizz')
def test_fizz(testdir):
testdir.copy_example('buzz.txt')
will look for the file fizz/buzz.txt relative to the project root dir.
For more examples, definitely check out the section Testing plugins in pytest docs; also, you may find my other answer to the question How can I test if a pytest fixture raises an exception? helpful as it contains yet another working example to the topic. I have also found it very helpful to study the Testdir code directly as sadly pytest doesn't provide an extensive docs for it, but the code is pretty much self-documenting.

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