I have a project where each app has a tests
module as below:
app1/
tests/
__init__.py
# whatever test files are
app2/
tests/
__init__.py
# whatever test files are
And also have the project package in the project root as you can guess. It has a testing
subpackage, which contains base test cases for models and fields and, of course, fixtures as below:
myproject/
__init__.py
# settings and related files, you know
testing/
__init__.py
fixtures.py # for fixtures
# other modules for base test classes and stuff
So, my overall project structure is:
myproject/ # contains standard django stuff and a testing subpackage
app1/ # contains app and tests
app2/ # so as above
# so on and so forth
And the pytest.ini
content in project root is as below:
[pytest]
DJANGO_SETTINGS_MODULE = myproject.settings.development
python_files =
test_*.py
myproject/testing/fixtures.py
addopts = -s
console_output_style = count
log_cli_level = DEBUG
log_print = True
So, assuming myproject/testing/fixtures.py
contains a fixture as below:
# assuming i've imported related stuff
@pytest.fixture # might have different scope
def foo():
"""does foo"""
return "foo"
And later on, I do pytest --fixtures
to see if pytest properly discovers my fixture and so it does, which means there should not be a problem.
However, when I run tests in the project, I get the error below:
E fixture 'foo' not found
This is odd since I can clearly see it in pytest --fixtures
. I do not have any idea why this happens or what causes this.
pytest-django
3.4.8If you have fixtures that should be kept in a separate module and accessible by the whole test suite, best is to register the module as a plugin to ensure it loads before the test collection phase. Create a file named conftest.py
in the project root dir with the content
pytest_plugins = ['myproject.testing.fixtures']
Of course, you can just use the conftest.py
instead of the fixtures.py
and put the fixtures there.