Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How are pytest fixure scopes intended to work?

I want to use pytest fixtures to prepare an object I want to use across a set of tests.
I follow the documentation and create a fixture in something_fixture.py with its scope set to session like this:

import pytest

@pytest.fixture(scope="session")
def something():
    return 'something'

Then in test_something.py I try to use the fixture like this:

def test_something(something):
    assert something == 'something'

Which does not work, but if I import the fixture like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from tests.something_fixture import something


def test_something(something):
    assert something == 'something'

the test passes…

Is this import necessary? Because to me this is not clear according to the documentation.

>Solution :

This session-scoped fixture should be defined in a conftest.py module, see conftest.py: sharing fixtures across multiple files in the docs.

The conftest.py file serves as a means of providing fixtures for an entire directory. Fixtures defined in a conftest.py can be used by any test in that package without needing to import them (pytest will automatically discover them).

By writing the fixture in something_fixture.py it was defined somewhere that went "unnoticed" because there was no reason for Python to import this module. The default test collection phase considers filenames matching these glob patterns:

- test_*.py
- *_test.py

Since it’s a session-scoped feature, define it instead in a conftest.py file, so it will be created at test collection time and available to all tests.

You can remove the import statement from tests.something_fixture import something. In fact the "tests" subdirectory generally doesn’t need to be importable at all.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading