diff --git a/CHANGELOG b/CHANGELOG index e8cafe328..5b416963b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,6 +12,10 @@ NEXT instead of presuming any RuntimeError is that one (implemented in py dep). Thanks Charles Cloud for analysing the issue. +- fix conftest related fixture visibility issue: when running with a + CWD outside a test package pytest would get fixture discovery wrong. + Thanks to Wolfgang Schnerring for figuring out a reproducable example. + 2.6.2 ----------- diff --git a/_pytest/__init__.py b/_pytest/__init__.py index 1ad918750..6b608b043 100644 --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,2 @@ # -__version__ = '2.6.3.dev' +__version__ = '2.6.3.dev3' diff --git a/_pytest/python.py b/_pytest/python.py index fbcdca691..9ce1d2170 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1611,10 +1611,15 @@ class FixtureManager: except AttributeError: pass else: + # construct the base nodeid which is later used to check + # what fixtures are visible for particular tests (as denoted + # by their test id) if p.basename.startswith("conftest.py"): - nodeid = p.dirpath().relto(self.session.fspath) + nodeid = self.session.fspath.bestrelpath(p.dirpath()) if p.sep != "/": nodeid = nodeid.replace(p.sep, "/") + if nodeid == ".": + nodeid = "" self.parsefactories(plugin, nodeid) self._seenplugins.add(plugin) diff --git a/setup.py b/setup.py index 495fff434..890758728 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ def main(): name='pytest', description='pytest: simple powerful testing with Python', long_description=long_description, - version='2.6.3.dev', + version='2.6.3.dev3', url='http://pytest.org', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], diff --git a/testing/python/fixture.py b/testing/python/fixture.py index fd8131827..9739b8eb8 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -1001,6 +1001,40 @@ class TestFixtureManagerParseFactories: reprec = testdir.inline_run("-s") reprec.assertoutcome(passed=1) + def test_parsefactories_relative_node_ids(self, testdir): + # example mostly taken from: + # https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html + runner = testdir.mkdir("runner") + package = testdir.mkdir("package") + package.join("conftest.py").write(dedent("""\ + import pytest + @pytest.fixture + def one(): + return 1 + """)) + package.join("test_x.py").write(dedent("""\ + def test_x(one): + assert one == 1 + """)) + sub = package.mkdir("sub") + sub.join("__init__.py").ensure() + sub.join("conftest.py").write(dedent("""\ + import pytest + @pytest.fixture + def one(): + return 2 + """)) + sub.join("test_y.py").write(dedent("""\ + def test_x(one): + assert one == 2 + """)) + reprec = testdir.inline_run() + reprec.assertoutcome(passed=2) + with runner.as_cwd(): + reprec = testdir.inline_run("..") + reprec.assertoutcome(passed=2) + + class TestAutouseDiscovery: def pytest_funcarg__testdir(self, testdir): testdir.makeconftest("""