From ef7cb47b1efac13a38859c39cff89c518be4045b Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 7 Apr 2014 13:29:57 +0200 Subject: [PATCH] fix issue498: if a fixture finalizer fails, make sure that the fixture is still invalidated. --HG-- branch : issue498 --- CHANGELOG | 3 +++ _pytest/python.py | 14 ++++++++------ testing/python/fixture.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e06dfd6c4..39c2da126 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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. diff --git a/_pytest/python.py b/_pytest/python.py index 0c48f01c2..25a5f5c67 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -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() diff --git a/testing/python/fixture.py b/testing/python/fixture.py index 1f5565af3..d37801241 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -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