Reputation: 2417
I am building a python package, using pytest
for all unit testing. My package is composed of several modules, with various submodules under each module. The unit tests are within the test
folder of each module (e.g., ./tumble/tumble/math/test/test_multiply.py
or ./tumble/tumble/science/test/test_divide.py
)
I have some fixtures that I want to share across all of the modules & submodules. Because of this, I want place them in a central location, ./tumble/tumble/test
in this example, and not have duplicate fixtures in each submodule (math/test
and science/test
).
If I place conftest.py
within the test
folder in each submodule, everything works as expected. However, I have the same fixtures in two locations, which isn't ideal.
When I place my fixtures in a central location, I am able to see them when I use the command pytest --fixtures
, however, when I run pytest
, it tells me fixture not found
and the fixture is not listed in the available fixtures
.
Do I need to move all of my unit tests under the test
folder, or is there something that I can do to keep unit tests within the submodules, but fixtures in a central location?
tumble
+-- setup.py
+-- README.md
+-- tumble
| +-- math
| | +-- __init__.py
| | +-- multiply.py
| | +-- test
| | +-- __init__.py
| | | +-- test
| | | | +-- __init__.py
| | | | +-- test_multiply.py
| +-- science
| | +-- __init__.py
| | +-- divide.py
| | +-- test
| | +-- __init__.py
| | | +-- test
| | | | +-- __init__.py
| | | | +-- test_divide.py
| +-- test
| | +-- __init__.py
| | +-- conftest.py
multiply.py
def multipy(x, y):
return x * y
conftest.py
import pytest
@pytest.fixture()
def numbers():
return (1, 5, 10)
test_multiply.py
import tumble
import pytest
assert tumble.multiply(numbers[0], numbers[1]) == 5
Upvotes: 8
Views: 7789
Reputation: 2208
from pytest doc and about plugins in pytest :
Local conftest.py plugins contain directory-specific hook implementations. Hook Session and test running activities will invoke all hooks defined in conftest.py files closer to the root of the filesystem
so, in order for your test to know the fixture, it should be higher in the hierarchy.
i.e. put your shared fixtures under the root folder, /tumble/conftest.py
Upvotes: 10