Skip to content

Commit

Permalink
Session._collectfile: use realpath
Browse files Browse the repository at this point in the history
This fixes runnings `pytest tests`, where `tests` is a symlink to
`project/app/tests`: previously `project/app/conftest.py` would be
ignored for fixtures then.
  • Loading branch information
blueyed committed Oct 10, 2018
1 parent 4c9015c commit 9f90fab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ def _collect(self, arg):
yield y

def _collectfile(self, path):
path = path.realpath()
ihook = self.gethookproxy(path)
if not self.isinitpath(path):
if ihook.pytest_ignore_collect(path=path, config=self.config):
Expand Down
37 changes: 36 additions & 1 deletion testing/test_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import py
import pytest
from _pytest.config import PytestPluginManager
from _pytest.main import EXIT_NOTESTSCOLLECTED, EXIT_USAGEERROR
from _pytest.main import EXIT_NOTESTSCOLLECTED, EXIT_OK, EXIT_USAGEERROR


@pytest.fixture(scope="module", params=["global", "inpackage"])
Expand Down Expand Up @@ -186,6 +186,41 @@ def pytest_addoption(parser):
assert "warning: could not load initial" not in result.stdout.str()


@pytest.mark.skipif(
not hasattr(py.path.local, "mksymlinkto"),
reason="symlink not available on this platform",
)
def test_conftest_symlink(testdir):
"""Ensure that conftest.py is used for resolved symlinks."""
realtemp = testdir.tmpdir.mkdir("real")
realtests = realtemp.mkdir("tests")
linktemp = testdir.tmpdir.join("symlinktests")
linktemp.mksymlinkto(realtests)
testdir.makepyfile(
**{
"real/tests/test_foo.py": "def test1(fixture): pass",
"real/conftest.py": textwrap.dedent(
"""
import pytest
print("conftest_loaded")
@pytest.fixture
def fixture():
print("fixture_used")
"""
),
}
)
result = testdir.runpytest("-vs", "symlinktests")
result.stdout.fnmatch_lines([
"*conftest_loaded*",
"real/tests/test_foo.py::test1 fixture_used",
"PASSED",
])
assert result.ret == EXIT_OK


def test_no_conftest(testdir):
testdir.makeconftest("assert 0")
result = testdir.runpytest("--noconftest")
Expand Down

0 comments on commit 9f90fab

Please sign in to comment.