Skip to content

Commit 9f90fab

Browse files
committed
Session._collectfile: use realpath
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.
1 parent 4c9015c commit 9f90fab

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/_pytest/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ def _collect(self, arg):
546546
yield y
547547

548548
def _collectfile(self, path):
549+
path = path.realpath()
549550
ihook = self.gethookproxy(path)
550551
if not self.isinitpath(path):
551552
if ihook.pytest_ignore_collect(path=path, config=self.config):

testing/test_conftest.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import py
55
import pytest
66
from _pytest.config import PytestPluginManager
7-
from _pytest.main import EXIT_NOTESTSCOLLECTED, EXIT_USAGEERROR
7+
from _pytest.main import EXIT_NOTESTSCOLLECTED, EXIT_OK, EXIT_USAGEERROR
88

99

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

188188

189+
@pytest.mark.skipif(
190+
not hasattr(py.path.local, "mksymlinkto"),
191+
reason="symlink not available on this platform",
192+
)
193+
def test_conftest_symlink(testdir):
194+
"""Ensure that conftest.py is used for resolved symlinks."""
195+
realtemp = testdir.tmpdir.mkdir("real")
196+
realtests = realtemp.mkdir("tests")
197+
linktemp = testdir.tmpdir.join("symlinktests")
198+
linktemp.mksymlinkto(realtests)
199+
testdir.makepyfile(
200+
**{
201+
"real/tests/test_foo.py": "def test1(fixture): pass",
202+
"real/conftest.py": textwrap.dedent(
203+
"""
204+
import pytest
205+
206+
print("conftest_loaded")
207+
208+
@pytest.fixture
209+
def fixture():
210+
print("fixture_used")
211+
"""
212+
),
213+
}
214+
)
215+
result = testdir.runpytest("-vs", "symlinktests")
216+
result.stdout.fnmatch_lines([
217+
"*conftest_loaded*",
218+
"real/tests/test_foo.py::test1 fixture_used",
219+
"PASSED",
220+
])
221+
assert result.ret == EXIT_OK
222+
223+
189224
def test_no_conftest(testdir):
190225
testdir.makeconftest("assert 0")
191226
result = testdir.runpytest("--noconftest")

0 commit comments

Comments
 (0)