[Pytest-commit] commit/pytest: hpk42: SO-17664702: call fixture finalizers even if the fixture function

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Jul 17 10:29:18 CEST 2013


1 new commit in pytest:

https://bitbucket.org/hpk42/pytest/commits/39a686b8775f/
Changeset:   39a686b8775f
User:        hpk42
Date:        2013-07-17 10:29:11
Summary:     SO-17664702: call fixture finalizers even if the fixture function
partially failed (finalizers would not always be called before)
Affected #:  5 files

diff -r 6b10ac6e0a44682ce1ba97e06eb61b1c90774938 -r 39a686b8775f100730751756e1b638e7a305e182 CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,9 @@
 Changes between 2.3.5 and 2.4.DEV
 -----------------------------------
 
+- SO-17664702: call fixture finalizers even if the fixture function
+  partially failed (finalizers would not always be called before)
+
 - color the last line red or green depending if failures/errors occured
   or everything passed.  thanks Christian Theunert.
 

diff -r 6b10ac6e0a44682ce1ba97e06eb61b1c90774938 -r 39a686b8775f100730751756e1b638e7a305e182 _pytest/__init__.py
--- a/_pytest/__init__.py
+++ b/_pytest/__init__.py
@@ -1,2 +1,2 @@
 #
-__version__ = '2.4.0.dev6'
+__version__ = '2.4.0.dev7'

diff -r 6b10ac6e0a44682ce1ba97e06eb61b1c90774938 -r 39a686b8775f100730751756e1b638e7a305e182 _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -1290,15 +1290,21 @@
         # route request.addfinalizer to fixturedef
         mp.setattr(self, "addfinalizer", fixturedef.addfinalizer)
 
-        # perform the fixture call
-        val = fixturedef.execute(request=self)
+        try:
+            # perform the fixture call
+            val = fixturedef.execute(request=self)
+        finally:
+            # if the fixture function failed it might still have
+            # registered finalizers so we can register
 
-        # prepare finalization according to scope
-        # (XXX analyse exact finalizing mechanics / cleanup)
-        self.session._setupstate.addfinalizer(fixturedef.finish, self.node)
-        self._fixturemanager.addargfinalizer(fixturedef.finish, argname)
-        for subargname in fixturedef.argnames: # XXX all deps?
-            self._fixturemanager.addargfinalizer(fixturedef.finish, subargname)
+            # prepare finalization according to scope
+            # (XXX analyse exact finalizing mechanics / cleanup)
+            self.session._setupstate.addfinalizer(fixturedef.finish,
+                                                  self.node)
+            self._fixturemanager.addargfinalizer(fixturedef.finish, argname)
+            for subargname in fixturedef.argnames: # XXX all deps?
+                self._fixturemanager.addargfinalizer(fixturedef.finish,
+                                                     subargname)
         mp.undo()
         return val
 

diff -r 6b10ac6e0a44682ce1ba97e06eb61b1c90774938 -r 39a686b8775f100730751756e1b638e7a305e182 setup.py
--- a/setup.py
+++ b/setup.py
@@ -12,7 +12,7 @@
         name='pytest',
         description='py.test: simple powerful testing with Python',
         long_description = long_description,
-        version='2.4.0.dev6',
+        version='2.4.0.dev7',
         url='http://pytest.org',
         license='MIT license',
         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

diff -r 6b10ac6e0a44682ce1ba97e06eb61b1c90774938 -r 39a686b8775f100730751756e1b638e7a305e182 testing/python/fixture.py
--- a/testing/python/fixture.py
+++ b/testing/python/fixture.py
@@ -309,6 +309,39 @@
         print(ss.stack)
         assert teardownlist == [1]
 
+    def test_request_addfinalizer_failing_setup(self, testdir):
+        testdir.makepyfile("""
+            import pytest
+            l = [1]
+            @pytest.fixture
+            def myfix(request):
+                request.addfinalizer(l.pop)
+                assert 0
+            def test_fix(myfix):
+                pass
+            def test_finalizer_ran():
+                assert not l
+        """)
+        reprec = testdir.inline_run("-s")
+        reprec.assertoutcome(failed=1, passed=1)
+
+    def test_request_addfinalizer_failing_setup_module(self, testdir):
+        testdir.makepyfile("""
+            import pytest
+            l = [1, 2]
+            @pytest.fixture(scope="module")
+            def myfix(request):
+                request.addfinalizer(l.pop)
+                request.addfinalizer(l.pop)
+                assert 0
+            def test_fix(myfix):
+                pass
+        """)
+        reprec = testdir.inline_run("-s")
+        mod = reprec.getcalls("pytest_runtest_setup")[0].item.module
+        assert not mod.l
+
+
     def test_request_addfinalizer_partial_setup_failure(self, testdir):
         p = testdir.makepyfile("""
             l = []

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the pytest-commit mailing list