This commit is contained in:
Katarzyna Jachim 2013-07-06 15:54:33 +02:00
commit ffa1bf726d
5 changed files with 94 additions and 16 deletions

View File

@ -1,6 +1,9 @@
Changes between 2.3.5 and 2.4.DEV 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 - fix issue323 - sorting of many module-scoped arg parametrizations
- add support for setUpModule/tearDownModule detection, thanks Brian Okken. - add support for setUpModule/tearDownModule detection, thanks Brian Okken.

View File

@ -196,27 +196,20 @@ class Conftest(object):
self.getconftestmodules(x) self.getconftestmodules(x)
def getconftestmodules(self, path): def getconftestmodules(self, path):
""" return a list of imported conftest modules for the given path. """
try: try:
clist = self._path2confmods[path] clist = self._path2confmods[path]
except KeyError: except KeyError:
if path is None: if path is None:
raise ValueError("missing default confest.") raise ValueError("missing default conftest.")
dp = path.dirpath()
clist = [] clist = []
if dp != path: for parent in path.parts():
cutdir = self._confcutdir if self._confcutdir and self._confcutdir.relto(parent):
if cutdir and path != cutdir and not path.relto(cutdir): continue
pass conftestpath = parent.join("conftest.py")
else: if conftestpath.check(file=1):
conftestpath = path.join("conftest.py") clist.append(self.importconftest(conftestpath))
if conftestpath.check(file=1):
clist.append(self.importconftest(conftestpath))
clist[:0] = self.getconftestmodules(dp)
self._path2confmods[path] = clist self._path2confmods[path] = clist
# be defensive: avoid changes from caller side to return clist
# affect us by always returning a copy of the actual list
return clist[:]
def rget(self, name, path=None): def rget(self, name, path=None):
mod, value = self.rget_with_confmod(name, path) mod, value = self.rget_with_confmod(name, path)

View File

@ -102,6 +102,77 @@ class TestFillFixtures:
"*2 passed*" "*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): def test_funcarg_lookup_error(self, testdir):
p = testdir.makepyfile(""" p = testdir.makepyfile("""
def test_lookup_error(unknown): def test_lookup_error(unknown):

View File

@ -204,3 +204,14 @@ def test_conftest_confcutdir(testdir):
""")) """))
result = testdir.runpytest("-h", "--confcutdir=%s" % x, x) result = testdir.runpytest("-h", "--confcutdir=%s" % x, x)
result.stdout.fnmatch_lines(["*--xyz*"]) 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]

View File

@ -654,9 +654,9 @@ def pytest_report_header(config, startdir):
""") """)
result = testdir.runpytest("a") result = testdir.runpytest("a")
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
"*hello: 42*",
"line1", "line1",
str(testdir.tmpdir), str(testdir.tmpdir),
"*hello: 42*",
]) ])
@pytest.mark.xfail("not hasattr(os, 'dup')") @pytest.mark.xfail("not hasattr(os, 'dup')")