Issue 16599: unittest: Access test result from tearDown (original) (raw)

Created on 2012-12-03 09:42 by techtonik, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (11)
msg176832 - (view) Author: anatoly techtonik (techtonik) Date: 2012-12-03 09:42
A common usage pattern is to prevent clean up in tearDown() if the test failed. However, it requires a hack: http://stackoverflow.com/questions/4414234/getting-pythons-unittest-results-in-a-teardown-method. Would be nice to have an officially documented feature.
msg176901 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-12-04 12:06
Another solution would be to add a new method named addSuccessCleanup.
msg176902 - (view) Author: anatoly techtonik (techtonik) Date: 2012-12-04 12:08
Or call tearDown() with some parameter that it's able to retrieve.
msg176904 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-12-04 12:09
Changing the signature of tearDown would be backwards incompatible. addSuccessCleanup is an interesting idea - cleanup functions that are only executed if the test passes. (What to do if a cleanup function raises an exception though? And when do these get executed - before or after standard cleanups.) In general cleanup functions are an improvement on tearDown.
msg176906 - (view) Author: anatoly techtonik (techtonik) Date: 2012-12-04 12:40
addSuccessCleanup is not the best name as it assumes that some cleanup is being added, which is confusing. Additional consideration that the need to leave the test results is a user run time preference, which may only be actual when debugging or working on the particular test.
msg176908 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-12-04 12:43
Well, addSuccessCleanup *would* be an api for adding a cleanup - one that is only called on success. So the cleanup is skipped on failure or error, which was the original use case. "Additional consideration that the need to leave the test results is a user run time preference, which may only be actual when debugging or working on the particular test." I can't parse that sentence, can you rephrase please. (Did you mean another word other than "leave", and I don't understand "may only be actual".)
msg176909 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-12-04 12:45
Why do you need it? You can add a test result depended code at the end of the test method. For example set a special flag def setUp(self): self.success = False def tearDown(self): if not self.success: ... def test_xxx(self): ... self.success = True I don't think any special support in stdlib needed.
msg176919 - (view) Author: anatoly techtonik (techtonik) Date: 2012-12-04 15:10
On Tue, Dec 4, 2012 at 3:43 PM, Michael Foord <report@bugs.python.org>wrote: > > Well, addSuccessCleanup *would* be an api for adding a cleanup - one that > is only called on success. So the cleanup is skipped on failure or error, > which was the original use case. > I thought it will be a callback method. > "Additional consideration that the need to leave the test results is a > user run time preference, which may only be actual when debugging or > working on the particular test." > > I can't parse that sentence, can you rephrase please. (Did you mean > another word other than "leave", and I don't understand "may only be > actual".) > Most of the time users need to clean up mess. It mostly during debug session you need to leave it. So the workaround should be easy and obvious to enable and disable. Serhiy's method is good. I was too busy with other stuff to think about it. Thanks.
msg178236 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-12-26 18:47
Anatoly, if this is good for you, please close the issue.
msg178341 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-12-27 22:01
I agree with Serhiy here. This use case seems too specialized, and there are easy ways to achieve the same thing in code.
msg222102 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-02 13:25
The solution suggested in seems accepted all around so can we please close this.
History
Date User Action Args
2022-04-11 14:57:39 admin set github: 60803
2014-08-22 09:35:26 Claudiu.Popa set status: open -> closedresolution: rejectedstage: needs patch -> resolved
2014-07-02 13:25:06 BreamoreBoy set nosy: + BreamoreBoymessages: + versions: + Python 3.5, - Python 3.4
2012-12-27 22:01:57 chris.jerdonek set nosy: + chris.jerdonekmessages: +
2012-12-26 18:47:44 serhiy.storchaka set messages: +
2012-12-04 15:10:50 techtonik set messages: +
2012-12-04 12:45:08 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2012-12-04 12:43:38 michael.foord set messages: +
2012-12-04 12:40:49 techtonik set messages: +
2012-12-04 12:09:51 michael.foord set assignee: michael.foordmessages: +
2012-12-04 12:08:07 techtonik set messages: +
2012-12-04 12:06:42 pitrou set nosy: + michael.foord, pitroumessages: +
2012-12-03 14:53:52 jcea set nosy: + jcea
2012-12-03 10:05:02 daniel.urban set stage: needs patchtype: enhancementversions: + Python 3.4, - Python 3.1, Python 2.7, Python 3.2, Python 3.3
2012-12-03 09:42:03 techtonik create