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.

--HG--
branch : conftest-nodeid
This commit is contained in:
holger krekel 2014-09-15 12:44:16 +02:00
parent 1a80487e71
commit b6dcfd4377
5 changed files with 46 additions and 3 deletions

View File

@ -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
-----------

View File

@ -1,2 +1,2 @@
#
__version__ = '2.6.3.dev'
__version__ = '2.6.3.dev3'

View File

@ -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)

View File

@ -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'],

View File

@ -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("""