diff --git a/CHANGELOG b/CHANGELOG index ecc93a3b9..ab198aced 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,10 @@ 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 fixtures. Thanks Jason R. Coombs for reporting and Bruno Oliveira for the PR. diff --git a/_pytest/config.py b/_pytest/config.py index c8a79f6b5..55a3bfa88 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -894,6 +894,9 @@ class Config(object): self.warn("I2", "could not load setuptools entry import: %s" % (e,)) self.pluginmanager.consider_env() 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: self.hook.pytest_load_initial_conftests(early_config=self, args=args, parser=self._parser) diff --git a/doc/en/customize.txt b/doc/en/customize.txt index b92194999..1c1655697 100644 --- a/doc/en/customize.txt +++ b/doc/en/customize.txt @@ -219,3 +219,10 @@ Builtin configuration file options One or more doctest flag names from the standard ``doctest`` module. :doc:`See how py.test handles doctests `. + +.. 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. diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 513ba43a5..64fa6d5e5 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -343,3 +343,44 @@ class TestConftestVisibility: with dirs[chdir].as_cwd(): reprec = testdir.inline_run(testarg, "-q", "--traceconfig") 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)