Merge pull request #872 from nicoddemus/confcutdir-inifile

Avoid detecting conftest files upwards from setup.cfg/pytest.ini/tox.ini by default
This commit is contained in:
Floris Bruynooghe 2015-07-25 14:03:24 +02:00
commit 023687d8d0
4 changed files with 55 additions and 0 deletions

View File

@ -1,6 +1,10 @@
2.8.0.dev (compared to 2.7.X) 2.8.0.dev (compared to 2.7.X)
----------------------------- -----------------------------
- fix issue82: avoid loading conftest files from setup.cfg/pytest.ini/tox.ini
files and upwards by default (--confcutdir can still be set to override this).
Thanks Bruno Oliveira for the PR.
- fix issue768: docstrings found in python modules were not setting up session - fix issue768: docstrings found in python modules were not setting up session
fixtures. Thanks Jason R. Coombs for reporting and Bruno Oliveira for the PR. fixtures. Thanks Jason R. Coombs for reporting and Bruno Oliveira for the PR.

View File

@ -897,6 +897,9 @@ class Config(object):
self.warn("I2", "could not load setuptools entry import: %s" % (e,)) self.warn("I2", "could not load setuptools entry import: %s" % (e,))
self.pluginmanager.consider_env() self.pluginmanager.consider_env()
self.known_args_namespace = ns = self._parser.parse_known_args(args) self.known_args_namespace = ns = self._parser.parse_known_args(args)
if self.known_args_namespace.confcutdir is None and self.inifile:
confcutdir = py.path.local(self.inifile).dirname
self.known_args_namespace.confcutdir = confcutdir
try: try:
self.hook.pytest_load_initial_conftests(early_config=self, self.hook.pytest_load_initial_conftests(early_config=self,
args=args, parser=self._parser) args=args, parser=self._parser)

View File

@ -219,3 +219,10 @@ Builtin configuration file options
One or more doctest flag names from the standard ``doctest`` module. One or more doctest flag names from the standard ``doctest`` module.
:doc:`See how py.test handles doctests <doctest>`. :doc:`See how py.test handles doctests <doctest>`.
.. confval:: confcutdir
Sets a directory where search upwards for ``conftest.py`` files stops.
By default, pytest will stop searching for ``conftest.py`` files upwards
from ``pytest.ini``/``tox.ini``/``setup.cfg`` of the project if any,
or up to the file-system root.

View File

@ -343,3 +343,44 @@ class TestConftestVisibility:
with dirs[chdir].as_cwd(): with dirs[chdir].as_cwd():
reprec = testdir.inline_run(testarg, "-q", "--traceconfig") reprec = testdir.inline_run(testarg, "-q", "--traceconfig")
reprec.assertoutcome(passed=expect_ntests_passed) reprec.assertoutcome(passed=expect_ntests_passed)
@pytest.mark.parametrize('confcutdir,passed,error', [
('.', 2, 0),
('src', 1, 1),
(None, 1, 1),
])
def test_search_conftest_up_to_inifile(testdir, confcutdir, passed, error):
"""Test that conftest files are detected only up to a ini file, unless
an explicit --confcutdir option is given.
"""
root = testdir.tmpdir
src = root.join('src').ensure(dir=1)
src.join('pytest.ini').write('[pytest]')
src.join('conftest.py').write(py.code.Source("""
import pytest
@pytest.fixture
def fix1(): pass
"""))
src.join('test_foo.py').write(py.code.Source("""
def test_1(fix1):
pass
def test_2(out_of_reach):
pass
"""))
root.join('conftest.py').write(py.code.Source("""
import pytest
@pytest.fixture
def out_of_reach(): pass
"""))
args = [str(src)]
if confcutdir:
args = ['--confcutdir=%s' % root.join(confcutdir)]
result = testdir.runpytest(*args)
match = ''
if passed:
match += '*%d passed*' % passed
if error:
match += '*%d error*' % error
result.stdout.fnmatch_lines(match)