Merged in hpk42/pytest-hpk/issue498 (pull request #151)

fix issue498: if a fixture finalizer fails, make sure that the fixture
This commit is contained in:
Ronny Pfannschmidt 2014-04-07 13:51:03 +02:00
commit 270d0f89ba
3 changed files with 40 additions and 6 deletions

View File

@ -1,6 +1,9 @@
NEXT (2.6) NEXT (2.6)
----------------------------------- -----------------------------------
- fix issue498: if a fixture finalizer fails, make sure that
the fixture is still invalidated.
- fix issue453: the result of the pytest_assertrepr_compare hook now gets - fix issue453: the result of the pytest_assertrepr_compare hook now gets
it's newlines escaped so that format_exception does not blow up. it's newlines escaped so that format_exception does not blow up.

View File

@ -1778,13 +1778,15 @@ class FixtureDef:
self._finalizer.append(finalizer) self._finalizer.append(finalizer)
def finish(self): def finish(self):
try:
while self._finalizer: while self._finalizer:
func = self._finalizer.pop() func = self._finalizer.pop()
func() func()
try: finally:
# even if finalization fails, we invalidate
# the cached fixture value
if hasattr(self, "cached_result"):
del self.cached_result del self.cached_result
except AttributeError:
pass
def execute(self, request): def execute(self, request):
# get required arguments and register our own finish() # get required arguments and register our own finish()

View File

@ -2087,6 +2087,35 @@ class TestErrors:
"*1 error*", "*1 error*",
]) ])
def test_issue498_fixture_finalizer_failing(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture
def fix1(request):
def f():
raise KeyError
request.addfinalizer(f)
return object()
l = []
def test_1(fix1):
l.append(fix1)
def test_2(fix1):
l.append(fix1)
def test_3():
assert l[0] != l[1]
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines("""
*ERROR*teardown*test_1*
*KeyError*
*ERROR*teardown*test_2*
*KeyError*
*3 pass*2 error*
""")
def test_setupfunc_missing_funcarg(self, testdir): def test_setupfunc_missing_funcarg(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest