[py-svn] py-trunk commit f0035a36f714: fix issue78 - now python-level teardown functions are now called even if the setup failed.

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Jan 27 12:12:58 CET 2010


# HG changeset patch -- Bitbucket.org
# Project py-trunk
# URL http://bitbucket.org/hpk42/py-trunk/overview/
# User holger krekel <holger at merlinux.eu>
# Date 1264590570 -3600
# Node ID f0035a36f714f9d06da65162a50a81a1e1947363
# Parent 13115a130ed644130ed55def87838b8c1fa23d72
fix issue78 - now python-level teardown functions are now called even if the setup failed.
Important detail: if the setup raises a Skipped exception, teardown will not be called.  This helps
to avoid breaking setup_module/class that performs a skip - it would otherwise internally
be considered as a "successful" setup in order to have teardown called later.  I guess
it also makes sense to treat Skip specially because it is unlikely a teardown should be
called if a Skip was raised on setup.

In any case, failing setups and teardowns will be reported separately.

--- a/testing/plugin/test_pytest_runner_xunit.py
+++ b/testing/plugin/test_pytest_runner_xunit.py
@@ -134,3 +134,38 @@ def test_method_setup_uses_fresh_instanc
     """)
     reprec.assertoutcome(passed=2, failed=0)
 
+def test_failing_setup_calls_teardown(testdir):
+    p = testdir.makepyfile("""
+        def setup_module(mod):
+            raise ValueError(42)
+        def test_function():
+            assert 0
+        def teardown_module(mod):
+            raise ValueError(43)
+    """)
+    result = testdir.runpytest(p)
+    result.stdout.fnmatch_lines([
+        "*42*",
+        "*43*",
+        "*2 error*"
+    ])
+
+def test_setup_that_skips_calledagain_and_no_teardown(testdir):
+    p = testdir.makepyfile("""
+        import py
+        def setup_module(mod):
+            py.test.skip("x") 
+        def test_function1():
+            pass
+        def test_function2():
+            pass
+        def teardown_module(mod):
+            raise ValueError(43)
+    """)
+    result = testdir.runpytest(p)
+    result.stdout.fnmatch_lines([
+        "*2 skipped*",
+    ])
+    assert "43" not in result.stdout.str()
+
+

--- a/py/_plugin/pytest_runner.py
+++ b/py/_plugin/pytest_runner.py
@@ -249,5 +249,9 @@ class SetupState(object):
                 break 
             self._pop_and_teardown()
         for col in needed_collectors[len(self.stack):]: 
-            col.setup() 
             self.stack.append(col) 
+            try:
+                col.setup() 
+            except Skipped:
+                self.stack.pop()
+                raise 

--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,9 @@
 Changes between 1.2.1 and 1.2.0
 =====================================
 
+- fix issue78: always call python-level teardown functions even if the
+  according setup failed - but make sure that setup is called repeatedly 
+  and no teardown if the setup raises a Skipped (as sone by py.test.skip()).
 - fix issue63: assume <40 columns to be a bogus terminal width, default to 80
 - update apipkg.py to fix an issue where recursive imports might
   unnecessarily break importing



More information about the pytest-commit mailing list