[pytest-dev] Fixtures not being found when module is named "conftest"

Bruno Oliveira nicoddemus at gmail.com
Fri Jan 2 14:10:48 CET 2015


Hi everyone!

Suppose the following files:

# foo/conftest.py
import pytest
@pytest.fixture
def my_fixture():
    return 1

# foo/__init__.py
<empty file>

# bar/conftest.py
pytest_plugins = ['foo.conftest']

# bar/test_bar.py
def test_it(my_fixture):
    assert my_fixture == 1

Supposing your PYTHONPATH is configured so you can import "foo.conftest",
running "py.test bar" fails because it does not find "my_fixture".

However, if you rename "foo/conftest.py" to something else like
"foo/plugin.py" and update the reference in "bar/conftest.py", then pytest
is able to find "my_fixture".

Looking at the code, I see that there is a special case for "conftest.py"
files in _pytest.python, line 1617. I was wondering what is the rationale
behind this? I was under the impression that "conftest.py" files where
simply plugins which were loaded automatically based on their relative
location to the tests.

In my case, I would like to reuse fixtures defined in conftest files
between different projects.
For instance, project2 depends on project1's fixtures which are defined in
a conftest file, so I would like to simply add a "pytest_plugins =
['project1.conftest']" to project2's own conftest file. If I understand
correctly, I would have to move project1's fixture to a different file, say
"project1.plugin", and reference it in both project1 and project2's
conftest files. Is this the recommended approach?

Cheers,
Bruno
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20150102/5924cf1d/attachment.html>


More information about the pytest-dev mailing list