nose-devs/nose — Issue #35: Teardown not called if there is an error in context setup
Author: jpellerin
Created: 2011-12-14 15:56:12+00:00
State: closed

What version of the product are you using? On what operating system?
0.10.4 - win32

I ran into some problems with errors in context setup and teardown.
Depending on how a test is run, nose is not tearing down contexts properly
if there is an error in setup or teardown. If nose is run with a path to
the specific test module to run, it tries to set up the context by running
the setups from all the packages above it. While iterating through the
list of contexts to setup, if one of them fails, the contexts already set
up do not get torn down.

I've attached a simple test which reproduces this problem. It contains one
package, named 'tests', and one module, named 'tests.testFoo'. There is a
package level setup and teardown in 'tests.__init__', a test function in
'tests.testFoo' called 'testFoo', and a module level setup function in
'tests.testFoo'. The module level setup in 'tests.testFoo' raises an
Exception. I've also added a plugin which logs some information when
startTest, stopTest, startContext, and stopContext are called. When nose
is run on 'tests', everything works as it should, all setup and teardown
get called correctly. When nose is run on 'tests.testFoo', the package
level teardown of 'tests' does not get called.

Here is the output from the attached test code on 0.10.4:

$ python runtest.py
###### Running nose on 'tests' ######
start context
tests.setUpPackage
start context
tests.testFoo.setUpModule
add error Ooo! It's a snake!
tests.tearDownPackage
stop context
###### Running nose on 'tests.testFoo' ######
start context
tests.setUpPackage
start context
tests.testFoo.setUpModule
add error Ooo! It's a snake!

Notice the last two lines of the first run are not printed in the second.

Here is the output with the changes to suite.py (in the diff file) applied:

$ python runtest.py
###### Running nose on 'tests' ######
start context
tests.setUpPackage
start context
tests.testFoo.setUpModule
add error Ooo! It's a snake!
tests.tearDownPackage
stop context
###### Running nose on 'tests.testFoo' ######
start context
tests.setUpPackage
start context
tests.testFoo.setUpModule
add error Ooo! It's a snake!
tests.tearDownPackage
stop context

I'm not too familiar with the nose code so I tried to keep my solution
contained to just the area in suite.py that I understood. I'm sure someone
with more knowledge of the codebase can come up with a better solution to
the problem.

Google Code Info:
Issue #: 234
Author: jlarosas...@gmail.com
Created On: 2009-02-12T20:48:36.000Z
Closed On: 2009-03-26T20:10:37.000Z