Skip to content

Commit 023687d

Browse files
committed
Merge pull request pytest-dev#872 from nicoddemus/confcutdir-inifile
Avoid detecting conftest files upwards from setup.cfg/pytest.ini/tox.ini by default
2 parents ae28e4b + ab9e246 commit 023687d

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
2.8.0.dev (compared to 2.7.X)
22
-----------------------------
33

4+
- fix issue82: avoid loading conftest files from setup.cfg/pytest.ini/tox.ini
5+
files and upwards by default (--confcutdir can still be set to override this).
6+
Thanks Bruno Oliveira for the PR.
7+
48
- fix issue768: docstrings found in python modules were not setting up session
59
fixtures. Thanks Jason R. Coombs for reporting and Bruno Oliveira for the PR.
610

_pytest/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,9 @@ def _preparse(self, args, addopts=True):
897897
self.warn("I2", "could not load setuptools entry import: %s" % (e,))
898898
self.pluginmanager.consider_env()
899899
self.known_args_namespace = ns = self._parser.parse_known_args(args)
900+
if self.known_args_namespace.confcutdir is None and self.inifile:
901+
confcutdir = py.path.local(self.inifile).dirname
902+
self.known_args_namespace.confcutdir = confcutdir
900903
try:
901904
self.hook.pytest_load_initial_conftests(early_config=self,
902905
args=args, parser=self._parser)

doc/en/customize.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,10 @@ Builtin configuration file options
219219

220220
One or more doctest flag names from the standard ``doctest`` module.
221221
:doc:`See how py.test handles doctests <doctest>`.
222+
223+
.. confval:: confcutdir
224+
225+
Sets a directory where search upwards for ``conftest.py`` files stops.
226+
By default, pytest will stop searching for ``conftest.py`` files upwards
227+
from ``pytest.ini``/``tox.ini``/``setup.cfg`` of the project if any,
228+
or up to the file-system root.

testing/test_conftest.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,44 @@ def test_parsefactories_relative_node_ids(
343343
with dirs[chdir].as_cwd():
344344
reprec = testdir.inline_run(testarg, "-q", "--traceconfig")
345345
reprec.assertoutcome(passed=expect_ntests_passed)
346+
347+
348+
@pytest.mark.parametrize('confcutdir,passed,error', [
349+
('.', 2, 0),
350+
('src', 1, 1),
351+
(None, 1, 1),
352+
])
353+
def test_search_conftest_up_to_inifile(testdir, confcutdir, passed, error):
354+
"""Test that conftest files are detected only up to a ini file, unless
355+
an explicit --confcutdir option is given.
356+
"""
357+
root = testdir.tmpdir
358+
src = root.join('src').ensure(dir=1)
359+
src.join('pytest.ini').write('[pytest]')
360+
src.join('conftest.py').write(py.code.Source("""
361+
import pytest
362+
@pytest.fixture
363+
def fix1(): pass
364+
"""))
365+
src.join('test_foo.py').write(py.code.Source("""
366+
def test_1(fix1):
367+
pass
368+
def test_2(out_of_reach):
369+
pass
370+
"""))
371+
root.join('conftest.py').write(py.code.Source("""
372+
import pytest
373+
@pytest.fixture
374+
def out_of_reach(): pass
375+
"""))
376+
377+
args = [str(src)]
378+
if confcutdir:
379+
args = ['--confcutdir=%s' % root.join(confcutdir)]
380+
result = testdir.runpytest(*args)
381+
match = ''
382+
if passed:
383+
match += '*%d passed*' % passed
384+
if error:
385+
match += '*%d error*' % error
386+
result.stdout.fnmatch_lines(match)

0 commit comments

Comments
 (0)