This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rbcollins
Recipients neologix, rbcollins, yselivanov
Date 2015-07-23.13:57:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1437659837.8.0.495243392604.issue24694@psf.upfronthosting.co.za>
In-reply-to
Content
The ordering is deliberate to support folk migrating from tearDown to cleanups - its not a bug. I thought we documented it clearly - https://docs.python.org/dev/library/unittest.html#unittest.TestCase.doCleanups - but if you wanted to submit improvements to the docs, that would be welcome.

The reverse ordering you propose, setup -> cleanups -> teardown means that its nearly impossible to migrate to cleanups bottom-up, you have to migrate top-down, which locks folk in inheritance based frameworks into waiting for the top of the tree to migrate first.

class Base(TestCase):

 def setUp(self):
  super().setUp()
  self.helper = Something()

 def tearDown(self):
  self.helper.finished()
  self.helper = None
  super().tearDown()


class Child(Base):

 def setUp(self):
  super().setUp()
  self.addCleanup(self.helper.release, my_resource)



If the order is
setup
teardown
cleanup

then in the cleanup the helper is already finished, and the release call will fail.

With the order of
setup
cleanup
teardown

The child's cleanup runs before the parents teardowns.

It does of course pose the same reverse problem: the base of an inheritance based frame for tests needs to be more conservative adopting cleanups, or signal backwards incompatibility, but since that is generally true anyway (frameworks move slower than their users), we considered it a reasonable tradeoff.


btw, you don't need a lambda in your example:

 self.addCleanup(print, 'destroying')
History
Date User Action Args
2015-07-23 13:57:17rbcollinssetrecipients: + rbcollins, neologix, yselivanov
2015-07-23 13:57:17rbcollinssetmessageid: <1437659837.8.0.495243392604.issue24694@psf.upfronthosting.co.za>
2015-07-23 13:57:17rbcollinslinkissue24694 messages
2015-07-23 13:57:17rbcollinscreate