[issue5538] tearDown in unittest should be executed regardless of result in setUp

Kumar McMillan report at bugs.python.org
Mon Mar 23 22:13:37 CET 2009


Kumar McMillan <kumar.mcmillan at gmail.com> added the comment:

fwiw, changing tearDown() so that it executes unconditionally will break
a countless number of test suites.  No test suite since the dawn of
unittest has been designed to tearDown() what may not have been setUp()
correctly.

in other words, this is how [in my experience] setUp and tearDown are
typically used together.  Imageine this error in setUp() :

def setUp(self):
    self.tmp_io = TempIO() # raise NameError("no such name TempIO")
    self.db = DataBase()

def tearDown(self):
    self.tmp_io.destroy()
    self.db.destroy()

With the change, you would need messy code like:

def tearDown(self):
    if hasattr(self, 'tmp_io'):
        self.tmp_io.destroy()
    if hasattar(self, 'db'):
        self.db.destroy()

This is just a simple example; things would get complicated fast.

I think addCleanup() is a good idea though.  Or what about a new hook
that would act like tearDown() but execute unconditionally. 
alwaysTearDown() ?  (I'm bad with names.)  Using a different hook would
solve the problem of porting test suites to this new paradigm.  But
besides that there are alternatives for doing cleanup.  I.E. if setUp()
in a class fails, then teardown_module() will still be called.

----------
nosy: +kumar303

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5538>
_______________________________________


More information about the Python-bugs-list mailing list