Skip to content

Commit 7216151

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 7216151

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-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: 42 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,47 @@ 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+
realtests = testdir.tmpdir.mkdir("real").mkdir("app").mkdir("tests")
196+
testdir.tmpdir.join("symlinktests").mksymlinkto(realtests)
197+
testdir.makepyfile(
198+
**{
199+
"real/app/tests/test_foo.py": "def test1(fixture): pass",
200+
"real/conftest.py": textwrap.dedent(
201+
"""
202+
import pytest
203+
204+
print("conftest_loaded")
205+
206+
@pytest.fixture
207+
def fixture():
208+
print("fixture_used")
209+
"""
210+
),
211+
}
212+
)
213+
result = testdir.runpytest("-vs", "symlinktests")
214+
result.stdout.fnmatch_lines([
215+
"*conftest_loaded*",
216+
"real/app/tests/test_foo.py::test1 fixture_used",
217+
"PASSED",
218+
])
219+
assert result.ret == EXIT_OK
220+
221+
result = testdir.runpytest("-vs", "symlinktests/test_foo.py::test1")
222+
result.stdout.fnmatch_lines([
223+
"*conftest_loaded*",
224+
"real/app/tests/test_foo.py::test1 fixture_used",
225+
"PASSED",
226+
])
227+
assert result.ret == EXIT_OK
228+
229+
189230
def test_no_conftest(testdir):
190231
testdir.makeconftest("assert 0")
191232
result = testdir.runpytest("--noconftest")

0 commit comments

Comments
 (0)