Reputation: 83527
I am writing some tests using pytest many of which have similar fixtures. I want to put these "global" fixtures in a single file so that they can be reused across multiple test files. My first thought was to create a fixtures.py
file such as
import pytest
@pytest.fixture()
def my_fixture():
# do something
Now how do I use this fixture in my_tests.py
?
def test_connect(my_fixture):
pass
This gives fixture 'my_fixture' not found
. I can from fixtures import my_fixture
. What is the suggested solution to this situation?
Upvotes: 8
Views: 3989
Reputation: 8266
Fixtures located in a conftest.py
file have vectical scoping with respect to the visibility of the fixture variable to test modules. This means that all test modules located as a sibling in the directory where the conftest.py file is located, or any subdirectories beneath that directory can all see the fixtures present in the conftest.py module.
That is a particular form of reuse but not always desired. If you want to reuse horizontally across test projects located in entirely different folders then you must put the fixtures within a "plugin module".
Take a look at these answers to get a sense of the possibilities
Upvotes: 0
Reputation: 597
Pytest will share the fixtures in conftest.py
automatically. Move the shared fixture from fixtures.py
to conftest.py
.
Upvotes: 16