[Python-Dev] Questions on unittest behaviour

Jonathan Lange jml at mumak.net
Sat Aug 19 08:36:48 CEST 2006


On 8/19/06, Collin Winter <collinw at gmail.com> wrote:
> 1) TestCase.tearDown() is only run if TestCase.setUp() succeeded. It
> seems to me that tearDown() should always be run, regardless of any
> failures in setUp() or the test method itself.
>
> The case I'm considering is something like this, ie, a multi-part setUp():
> > def setUp(self)
> >     lock_file(testfile) # open_socket(), connect_to_database(), etc
> >
> >     something_that_raises_an_exception()
> >
> > def tearDown(self):
> >     if file_is_locked(testfile):
> >         unlock_file(testfile)
>
> I'm undecided if this is a new feature (so it should go in for 2.6) or
> a bug fix; I'm leaning toward the latter.
>

Most existing tearDown() code assumes that setUp() has been
successfully called -- after all, that's what the docs say. It's also
the behaviour of other xUnit frameworks.

If this change is made, people will have to go through their
tearDown() methods and add checks like the one in your example in
order to make their tests correct. Further, it won't be obvious that
their tearDown() methods are incorrect until something happens to make
their setUp() methods incorrect.

I don't think this change is a good one. However, if it does go in, it
definitely shouldn't go in as a bug fix.

cheers,
jml


More information about the Python-Dev mailing list