diff --git a/CHANGELOG b/CHANGELOG index a2a129fde..7c4c983d2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ Changes between 2.3.5 and 2.4.DEV ----------------------------------- +- fix issue300 - Fix order of conftest loading when starting py.test + in a subdirectory. + - fix issue323 - sorting of many module-scoped arg parametrizations - add support for setUpModule/tearDownModule detection, thanks Brian Okken. diff --git a/_pytest/config.py b/_pytest/config.py index f3c4034eb..87d3e0a2a 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -196,27 +196,20 @@ class Conftest(object): self.getconftestmodules(x) def getconftestmodules(self, path): - """ return a list of imported conftest modules for the given path. """ try: clist = self._path2confmods[path] except KeyError: if path is None: - raise ValueError("missing default confest.") - dp = path.dirpath() + raise ValueError("missing default conftest.") clist = [] - if dp != path: - cutdir = self._confcutdir - if cutdir and path != cutdir and not path.relto(cutdir): - pass - else: - conftestpath = path.join("conftest.py") - if conftestpath.check(file=1): - clist.append(self.importconftest(conftestpath)) - clist[:0] = self.getconftestmodules(dp) + for parent in path.parts(): + if self._confcutdir and self._confcutdir.relto(parent): + continue + conftestpath = parent.join("conftest.py") + if conftestpath.check(file=1): + clist.append(self.importconftest(conftestpath)) self._path2confmods[path] = clist - # be defensive: avoid changes from caller side to - # affect us by always returning a copy of the actual list - return clist[:] + return clist def rget(self, name, path=None): mod, value = self.rget_with_confmod(name, path) diff --git a/testing/python/fixture.py b/testing/python/fixture.py index e82e38366..87cd4f928 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -102,6 +102,77 @@ class TestFillFixtures: "*2 passed*" ]) + def test_extend_fixture_module_class(self, testdir): + testfile = testdir.makepyfile(""" + import pytest + + @pytest.fixture + def spam(): + return 'spam' + + class TestSpam: + + @pytest.fixture + def spam(self, spam): + return spam * 2 + + def test_spam(self, spam): + assert spam == 'spamspam' + """) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["*1 passed*"]) + result = testdir.runpytest(testfile) + result.stdout.fnmatch_lines(["*1 passed*"]) + + def test_extend_fixture_conftest_module(self, testdir): + testdir.makeconftest(""" + import pytest + + @pytest.fixture + def spam(): + return 'spam' + """) + testfile = testdir.makepyfile(""" + import pytest + + @pytest.fixture + def spam(spam): + return spam * 2 + + def test_spam(spam): + assert spam == 'spamspam' + """) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["*1 passed*"]) + result = testdir.runpytest(testfile) + result.stdout.fnmatch_lines(["*1 passed*"]) + + def test_extend_fixture_conftest_conftest(self, testdir): + testdir.makeconftest(""" + import pytest + + @pytest.fixture + def spam(): + return 'spam' + """) + pkg = testdir.mkpydir("pkg") + pkg.join("conftest.py").write(py.code.Source(""" + import pytest + + @pytest.fixture + def spam(spam): + return spam * 2 + """)) + testfile = pkg.join("test_spam.py") + testfile.write(py.code.Source(""" + def test_spam(spam): + assert spam == "spamspam" + """)) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["*1 passed*"]) + result = testdir.runpytest(testfile) + result.stdout.fnmatch_lines(["*1 passed*"]) + def test_funcarg_lookup_error(self, testdir): p = testdir.makepyfile(""" def test_lookup_error(unknown): diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 6b9689fe3..d753f5672 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -204,3 +204,14 @@ def test_conftest_confcutdir(testdir): """)) result = testdir.runpytest("-h", "--confcutdir=%s" % x, x) result.stdout.fnmatch_lines(["*--xyz*"]) + +def test_conftest_import_order(testdir, monkeypatch): + ct1 = testdir.makeconftest("") + sub = testdir.mkdir("sub") + ct2 = sub.join("conftest.py") + ct2.write("") + def impct(p): + return p + conftest = Conftest() + monkeypatch.setattr(conftest, 'importconftest', impct) + assert conftest.getconftestmodules(sub) == [ct1, ct2] diff --git a/testing/test_terminal.py b/testing/test_terminal.py index edbd1f51b..804f3b656 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -654,9 +654,9 @@ def pytest_report_header(config, startdir): """) result = testdir.runpytest("a") result.stdout.fnmatch_lines([ + "*hello: 42*", "line1", str(testdir.tmpdir), - "*hello: 42*", ]) @pytest.mark.xfail("not hasattr(os, 'dup')")