Skip to content

cleanUp stack for unittest #49929

Closed
Closed

Description

@voidspace
BPO 5679
Nosy @gpshead, @pitrou, @rbtcollins, @ezio-melotti, @ngie-eign, @voidspace
Files
  • unittest-cleanup.diff
  • unittest-no-exit.patch
  • unittest-cleanup.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/voidspace'
    closed_at = <Date 2009-05-02.20:22:01.424>
    created_at = <Date 2009-04-03.19:56:46.283>
    labels = ['type-feature', 'library']
    title = 'cleanUp stack for unittest'
    updated_at = <Date 2011-03-09.19:32:54.415>
    user = 'https://github.com/voidspace'

    bugs.python.org fields:

    activity = <Date 2011-03-09.19:32:54.415>
    actor = 'ezio.melotti'
    assignee = 'michael.foord'
    closed = True
    closed_date = <Date 2009-05-02.20:22:01.424>
    closer = 'michael.foord'
    components = ['Library (Lib)']
    creation = <Date 2009-04-03.19:56:46.283>
    creator = 'michael.foord'
    dependencies = []
    files = ['13612', '13787', '13791']
    hgrepos = []
    issue_num = 5679
    keywords = ['patch']
    message_count = 45.0
    messages = ['85321', '85323', '85324', '85325', '85344', '85404', '85407', '85413', '85432', '85434', '85437', '85439', '85443', '85444', '85445', '85446', '85447', '85463', '85465', '85466', '85471', '85494', '85497', '85562', '85565', '85567', '85568', '85569', '85570', '85572', '85589', '85590', '85592', '85593', '85594', '85609', '85612', '85613', '86568', '86569', '86576', '86607', '86978', '86998', '130459']
    nosy_count = 9.0
    nosy_names = ['gregory.p.smith', 'pitrou', 'rbcollins', 'vdupras', 'jml', 'ezio.melotti', 'kumar303', 'ngie', 'michael.foord']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue5679'
    versions = ['Python 2.7']

    Activity

    1. voidspace commented on Apr 3, 2009

      @voidspace
      ContributorAuthor

      Proposal to add a cleanUp stack to unittest.TestCase. This is a list of
      callables to be called (LIFO) to cleanup resources. If there are items
      on the stack it should be called even if setUp fails. Otherwise it
      should be called after tearDown.

      Similar functionality is in the testing frameworks used by bzr, Twisted,
      and Zope.

      I will create a patch similar to the bzr implementation.

      The public API is via a new method:

          def addCleanUpItem(self, callable, *args, **kwargs):

      Usage is:

          self.db = self.createDB()
          self.addCleanUpItem(self.db.close)
    2. added
      type-bug
      on Apr 3, 2009
    3. self-assigned this
      on Apr 3, 2009
    4. added
      stdlib
      on Apr 3, 2009
    5. gpshead commented on Apr 3, 2009

      @gpshead
      Member

      I'm used to doing this using a finally clause in the setUp() method (if
      I do it at all, I'm not used to setUp failing).

      I guess this would be a convenience to avoid the need for this pattern?
      I'm not sure we really need a list of cleanup callbacks. Got pointers
      to good examples of this in bzr/twisted/zope?

      def setUp(self):
        try:
          do_a_bunch_of_stuff_that_could_fail()
        finally:
          conditionally_undo_setup()
        do_more()
      
      def conditionally_undo_setup(self):
        if self.foo: 
          self.foo.close()
        if self.bar:
          shutil.rmtree(self.bar)
      
      def tearDown(self):
        conditionally_undo_setup()
        undo_more()

      fwiw, in your self.db.close example, normally i'd also want to remove
      and unlink the database as well as just closing it for a unittest.
      maybe not the best example.

    6. voidspace commented on Apr 3, 2009

      @voidspace
      ContributorAuthor

      This is a nice (simple to use and understand) pattern for resource
      allocation / deallocation.

      Supporting the cleaning up of resources when setUp fails (without
      duplicating clean up code) is just one use case. (I agree setUp failure
      is unusual.)

      It provides a clean way to clean up just the resources you have
      allocated in the event of test failure, without having to keep track
      yourself of how far the test has got. Manually tracking resource usage
      is easy to get wrong.

      bzr:

      http://bazaar.launchpad.net/~jml/testtools/trunk/annotate/head%3A/testtools/testcase.py

      And from zope.testing:

      The documentation:
      http://svn.zope.org/zope.testing/trunk/src/zope/testing/setupstack.txt?rev=92340&view=auto

      The module:
      http://svn.zope.org/zope.testing/trunk/src/zope/testing/setupstack.py?rev=92340&view=auto

    7. voidspace commented on Apr 3, 2009

      @voidspace
      ContributorAuthor

      From your example this would completely remove the need for the
      conditional checking in the cleanup code. Only resources actually
      allocated would be in the stack.

    8. added
      type-feature
      and removed
      type-bug
      on Apr 3, 2009
    9. voidspace commented on Apr 3, 2009

      @voidspace
      ContributorAuthor

      And actually your conditionally_undo_setup() call in setUp is incorrect.
      It should in an except block that re-raises rather than a finally block
      (which will do the cleanup even in non-exceptional circumstances). Easy
      code to get wrong...

    10. voidspace commented on Apr 4, 2009

      @voidspace
      ContributorAuthor

      Patch attached.

      No docs, if it is agreed I can apply I'll write docs.

      After a long discussion we arrived at some semblance of consensus on the
      Testing In Python mailing list that this was a good thing (tm).

      Only one -1 (thought that cleanUp should be a method) and Holger Krekel
      was only +0 because he thinks changes in unittest should be conservative
      (but he isn't actually using it).

      Many others were +1 - Fred Drake, Titus Brown, Laura Creighton, Robert
      Collins, Jonathan Lange, Kumar McMillan and others.

      It provides a clean and simple pattern for the cleaning up of resources

      • avoiding the need for nested try finallys and the manual tracking of
        resources. It can also avoid the need for duplication of cleanup code.
    11. pitrou commented on Apr 4, 2009

      @pitrou
      Member

      I'm not sure why it is called after tearDown. It would be better to call
      it before tearDown, so that test cases can also use it to initialize
      additional resources and finalize them in a LIFO way wrt the main tearDown.

    12. voidspace commented on Apr 4, 2009

      @voidspace
      ContributorAuthor

      I'm agnostic on before / after tearDown, so happy to make that change.

    13. rbtcollins commented on Apr 4, 2009

      @rbtcollins
      Member

      It should run after tearDown so that teardown can do actions that may
      require cleanup; because the cleanups run in LIFO you can acquire
      resources in setUp and have cleanups clean them up,

    14. rbtcollins commented on Apr 4, 2009

      @rbtcollins
      Member

      Actually let me phrase that differently.
      standard practice for setUp is
      super.setUp()
      my_setup_code()

      and tearDown is
      my_teardown_code()
      super.tearDown()

      because of the LIFO need.

      If you imagine that clean ups are being done in the base class teardown,
      at the end of that method, then it becomes a lot more obvious why it
      should run after tearDown - because thats the right place, and because
      many users may be failing to call the base class tearDown which has
      historically done nothing.

    15. 22 remaining items

    16. voidspace commented on Apr 5, 2009

      @voidspace
      ContributorAuthor

      My apologies - the jml code on launchpad runs clean ups before taerDown.

      http://bazaar.launchpad.net/~jml/testtools/trunk/annotate/head%3A/testtools/testcase.py

    17. rbtcollins commented on Apr 6, 2009

      @rbtcollins
      Member

      On Sun, 2009-04-05 at 23:57 +0000, Michael Foord wrote:

      Michael Foord <michael@voidspace.org.uk> added the comment:

      My apologies - the jml code on launchpad runs clean ups before taerDown.

      http://bazaar.launchpad.net/~jml/testtools/trunk/annotate/head%3A/testtools/testcase.py

      Ah, indeed it does. Well, FWIW my use has nearly all been with bzr's
      that does it after :)

      Jono - was there some particular reason testtools does it after? (We're
      discussing the merits of pre-and-post tearDown, ad-nauseum :)).

      -Rob

    18. jml commented on Apr 6, 2009

      jmlmannequin
      Mannequin

      On Mon, Apr 6, 2009 at 10:06 AM, Robert Collins
      <robertc@robertcollins.net> wrote:

      On Sun, 2009-04-05 at 23:57 +0000, Michael Foord wrote:
      > Michael Foord <michael@voidspace.org.uk> added the comment:
      >
      > My apologies - the jml code on launchpad runs clean ups before taerDown.
      >
      > http://bazaar.launchpad.net/~jml/testtools/trunk/annotate/head%3A/testtools/testcase.py

      Ah, indeed it does. Well, FWIW my use has nearly all been with bzr's
      that does it after :)

      Jono - was there some particular reason testtools does it after? (We're
      discussing the merits of pre-and-post tearDown, ad-nauseum :)).

      No particular reason.

    19. kumar303 commented on Apr 6, 2009

      kumar303mannequin
      Mannequin

      I like this patch. However, a nice-to-have would be that _doCleanups()
      prints a traceback from function(*args, **kwargs) (if there is one) the
      same way that atexit does. That would aid in debugging mis-written
      cleanup functions yet would not intrude on your test program.

    20. jml commented on Apr 6, 2009

      jmlmannequin
      Mannequin

      On Mon, Apr 6, 2009 at 2:16 PM, Kumar McMillan <report@bugs.python.org> wrote:

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

      I like this patch.  However, a nice-to-have would be that _doCleanups()
      prints a traceback from function(*args, **kwargs) (if there is one) the
      same way that atexit does.  That would aid in debugging mis-written
      cleanup functions yet would not intrude on your test program.

      I think reporting the errors via addError is the right thing. If your
      cleanups fail, then your test suite becomes unreliable, so they should
      be treated like tests that raise unexpected errors.

    21. jml commented on Apr 6, 2009

      jmlmannequin
      Mannequin
    22. voidspace commented on Apr 25, 2009

      @voidspace
      ContributorAuthor

      Damn - proper patch without extraneous stuff this time.

    23. voidspace commented on Apr 25, 2009

      @voidspace
      ContributorAuthor

      Proper patch and proper issue this time! Not my evening.

    24. rbtcollins commented on Apr 26, 2009

      @rbtcollins
      Member

      On Sat, 2009-04-25 at 23:17 +0000, Michael Foord wrote:

      Michael Foord <michael@voidspace.org.uk> added the comment:

      Proper patch and proper issue this time! Not my evening.

      ----------
      Added file: http://bugs.python.org/file13787/unittest-no-exit.patch

      Did this want a new issue perhaps? [it looks fine, just unrelated].

      -Rob

    25. voidspace commented on Apr 26, 2009

      @voidspace
      ContributorAuthor

      Updated patch with docs. My intention is to apply this in the next
      couple of days.

      I've settled on calling doCleanups *after* tearDown. The issues and
      reasoning explained below.

      One point of view concerns using addCleanups with existing tests.

      If the setUp allocates resources that are deallocated by tearDown, the
      natural LIFO pattern would seem to be setUp -> tests that create cleanup
      functions -> do cleanups (which may use resources allocated by setUp) ->
      tearDown.

      This pattern doesn't allow tearDown to create cleanup functions and
      doesn't permit setUp to create cleanup functions if tearDown need to
      access those resources (unless tearDown itself becomes one big cleanup
      function).

      If you look at the situation with new tests then it is perfectly natural
      for setUp, tests and tearDown to all create cleanup functions. The
      natural LIFO order is for cleanups to be run after tearDown.

      The solution I've opted for is to make doCleanups public. If you are
      adding the use of cleanups to old tests and need the cleanup functions
      run before tearDown then you are free to decorate the test with a
      function that calls doCleanups on success or failure.

      This takes into account the experience of those already using test
      frameworks that provide this mechanism.

    26. voidspace commented on May 2, 2009

      @voidspace
      ContributorAuthor

      Committed in revision 72219.

    27. ngie-eign commented on May 3, 2009

      ngie-eignmannequin
      Mannequin

      Cool! Thanks for all of the hard work Michael :D.

    28. ezio-melotti commented on Mar 9, 2011

      @ezio-melotti
      Member

      Apparently during a merge from trunk (r72477) the addCleanup and other methods ended up in 3.1rc1, even if they are documented as new in 3.2. I'll update the doc to say "new in 3.1".

    29. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibtype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions