fix issue498: if a fixture finalizer fails, make sure that the fixture

is still invalidated.

--HG--
branch : issue498
This commit is contained in:
holger krekel 2014-04-07 13:29:57 +02:00
parent 6efde60b8b
commit ef7cb47b1e
3 changed files with 40 additions and 6 deletions

View File

@ -1,6 +1,9 @@
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
it's newlines escaped so that format_exception does not blow up.

View File

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

View File

@ -2087,6 +2087,35 @@ class TestErrors:
"*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):
testdir.makepyfile("""
import pytest