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.

classification
Title: unittest: allow failures in tearDown method
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.0, Python 2.4, Python 3.1, Python 2.7, Python 2.6, Python 2.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, brett.cannon, gfxmonk, pitrou, rhettinger
Priority: normal Keywords: patch

Created on 2009-03-21 09:52 by gfxmonk, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unittest-fail-on-teardown-0.patch gfxmonk, 2009-03-21 09:52 unittest allow failures on teardown v0
unittest-fail-on-teardown-1.patch gfxmonk, 2009-03-21 10:10
unnamed brett.cannon, 2009-03-22 17:11
Messages (9)
msg83927 - (view) Author: Tim Cuthbertson (gfxmonk) Date: 2009-03-21 09:52
This patch adds the behaviour that when a unittest.failureException is
thrown in a TestCase's tearDown method, the test case is added to the
failures list (instead of the errors list, and only when the test case
body has passed successfully).

In some circumstances, tests may want to assert that something happens
at some point during the body of a test, and the best time to make these
checks can be in the tearDown method.

This is a modification I've made during development of my mocktest
library (https://github.com/gfxmonk/mocktest/tree), and I believe it is
beneficial to have as the default unittest behaviour.
msg83930 - (view) Author: Tim Cuthbertson (gfxmonk) Date: 2009-03-21 10:10
updated patch: made the test method name unique (oops), and added the
specific test case where both test body and tearDown raise failure
exceptions. This patch supercedes the original (v0)
msg83955 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2009-03-22 03:30
So I disagree with this idea. The point of a tearDown method is simply
to clean up, not to test for a failure. If the test failed because it
didn't complete a test and clean up after itself then the test should
fail explicitly, not have the tearDown do it for the test.
msg83956 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-03-22 05:26
I concur with Brett.
msg83957 - (view) Author: Tim Cuthbertson (gfxmonk) Date: 2009-03-22 05:33
I agree that this is not normally the point of tearDown methods.
However, allowing it reduces repetition when you want to verify that
some invariant is not violated by any test.

I also think that as far as the test writer is concerned, an assertion
error is a failed test - regardless of where in the code it came from.
msg83966 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-03-22 10:33
Agreed with Brett and Raymond.
Besides, I'm not sure how getting a failure rather an error changes
anything in the big picture (for my non native English-speaking brain,
it certainly doesn't make a difference :-)).
msg83969 - (view) Author: Tim Cuthbertson (gfxmonk) Date: 2009-03-22 11:16
In my mind, an error means something is wrong. A failure just means a
test case hasn't been implemented yet, or has regressed. For me it's
misleading to have an AssertionError reported as a failure. It's not as
if changing the reporting in this way could cause tests to succeed when
they should fail (or vice versa) - so it seems like there is no downside
to including this behaviour.
msg83973 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-03-22 16:20
I concur with other developers. In strict unittesting, assertions of
correct behavior should only ever happen in the test not during set up
or tear down.
msg83974 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2009-03-22 17:11
Thanks for the patch and the attempt, Tim!
History
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49781
2009-03-22 17:11:03brett.cannonsetfiles: + unnamed

messages: + msg83974
2009-03-22 16:20:30benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg83973

resolution: rejected
2009-03-22 11:16:30gfxmonksetmessages: + msg83969
2009-03-22 10:33:52pitrousetnosy: + pitrou
messages: + msg83966
2009-03-22 05:33:19gfxmonksetmessages: + msg83957
2009-03-22 05:26:10rhettingersetnosy: + rhettinger
messages: + msg83956
2009-03-22 03:30:18brett.cannonsetnosy: + brett.cannon
messages: + msg83955

components: + Library (Lib), - Extension Modules
type: enhancement
stage: patch review
2009-03-21 10:10:15gfxmonksetfiles: + unittest-fail-on-teardown-1.patch

messages: + msg83930
2009-03-21 09:52:35gfxmonkcreate