Repository navigation
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
Description
| BPO | 5679 |
|---|---|
| Nosy | @gpshead, @pitrou, @rbtcollins, @ezio-melotti, @ngie-eign, @voidspace |
| Files |
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']Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
Activity
voidspace commentedon Apr 3, 2009
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:
Usage is:
gpshead commentedon Apr 3, 2009
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?
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.
voidspace commentedon Apr 3, 2009
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
voidspace commentedon Apr 3, 2009
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.
voidspace commentedon Apr 3, 2009
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...
voidspace commentedon Apr 4, 2009
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
resources. It can also avoid the need for duplication of cleanup code.
pitrou commentedon Apr 4, 2009
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.
voidspace commentedon Apr 4, 2009
I'm agnostic on before / after tearDown, so happy to make that change.
rbtcollins commentedon Apr 4, 2009
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,
rbtcollins commentedon Apr 4, 2009
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.
22 remaining items
voidspace commentedon Apr 5, 2009
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
rbtcollins commentedon Apr 6, 2009
On Sun, 2009-04-05 at 23:57 +0000, Michael Foord wrote:
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
jml commentedon Apr 6, 2009
On Mon, Apr 6, 2009 at 10:06 AM, Robert Collins
<robertc@robertcollins.net> wrote:
No particular reason.
kumar303 commentedon Apr 6, 2009
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.
jml commentedon Apr 6, 2009
On Mon, Apr 6, 2009 at 2:16 PM, Kumar McMillan <report@bugs.python.org> wrote:
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.
jml commentedon Apr 6, 2009
FWIW, I kind of like the tests here:
http://bazaar.launchpad.net/~jml/testtools/trunk/annotate/head%3A/testtools/tests/test_testtools.py#L221
voidspace commentedon Apr 25, 2009
Damn - proper patch without extraneous stuff this time.
voidspace commentedon Apr 25, 2009
Proper patch and proper issue this time! Not my evening.
rbtcollins commentedon Apr 26, 2009
On Sat, 2009-04-25 at 23:17 +0000, Michael Foord wrote:
Did this want a new issue perhaps? [it looks fine, just unrelated].
-Rob
voidspace commentedon Apr 26, 2009
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.
voidspace commentedon May 2, 2009
Committed in revision 72219.
ngie-eign commentedon May 3, 2009
Cool! Thanks for all of the hard work Michael :D.
ezio-melotti commentedon Mar 9, 2011
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".