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.

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-01-27 12:09:30 +01:00
parent 17bd875444
commit b18ab6e03b
3 changed files with 43 additions and 1 deletions

View File

@ -1,6 +1,9 @@
Changes between 1.2.1 and 1.2.0 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 - 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 - update apipkg.py to fix an issue where recursive imports might
unnecessarily break importing unnecessarily break importing

View File

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

View File

@ -134,3 +134,38 @@ def test_method_setup_uses_fresh_instances(testdir):
""") """)
reprec.assertoutcome(passed=2, failed=0) 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()