Title: In unittest.TestCase docs for setUp() and tearDown() don't mention AssertionError
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.3, Python 3.4, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: Julian.Gindi, docs@python, ezio.melotti, michael.foord, nitika, pconnell, py.user, python-dev, terry.reedy
Priority: normal Keywords: easy, patch

Created on 2013-07-27 05:12 by py.user, last changed 2014-04-16 19:40 by nitika. This issue is now closed.

File name Uploaded Description Edit
issue18566.patch Julian.Gindi, 2013-11-20 01:15 review
doc18566.patch sreepriya, 2014-03-14 16:56 Adding doc for AssertionError and Skiptest exception review
document18566.patch nitika, 2014-03-21 15:32 review
Messages (15)
msg193772 - (view) Author: py.user (py.user) * Date: 2013-07-27 05:12
"any exception raised by this method will be considered an error rather than a test failure"
"Any exception raised by this method will be considered an error rather than a test failure."

#!/usr/bin/env python3

import unittest

class Test(unittest.TestCase):
    def setUp(self):
        raise AssertionError

    def tearDown(self):
        raise AssertionError
    def test_nothing(self):

[guest@localhost py]$ python3 -m unittest -v utest
test_nothing (utest.Test) ... FAIL

FAIL: test_nothing (utest.Test)
Traceback (most recent call last):
  File "./", line 8, in setUp
    raise AssertionError

Ran 1 test in 0.000s

FAILED (failures=1)
[guest@localhost py]$

also raising unittest.SkipTest works properly
msg193796 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-07-27 20:56
I re-ran with setUp 'raise' changed to 'pass' to see the effect of raise AssertionError or unittest.SkipTest in tearDown and indeed the test fails or skips even then. I suggest adding ', other than AssertionError or SkipTest,' just after 'method'. The same is true of test_xxx methods.

A slight anomaly is that AssertionError in test_nothing and SkipTest in tearDown results in "FAILED (failures=1, skipped=1)", which is not really a skip.

For setUpClass and setUpModule, AssertionErrors *are* errors, not failures, while SkipTest works everywhere.
msg203454 - (view) Author: Julian Gindi (Julian.Gindi) * Date: 2013-11-20 01:15
I did some further testing and it seems that you are right, testcase.SkipTest() never causes an error in setUp or tearDown but "raise AssertionError" does (even in setUp or tearDown). I went ahead and made relevant documentation changes, let me know what you think.
msg206928 - (view) Author: Julian Gindi (Julian.Gindi) * Date: 2013-12-25 20:45
Looks like this issue has been resolved. Can we close it?
msg206929 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-12-25 21:16
Resolved in what way? The doc seems unchanged.
msg206930 - (view) Author: Julian Gindi (Julian.Gindi) * Date: 2013-12-25 21:23
Sorry. I meant, merged.
msg206931 - (view) Author: py.user (py.user) * Date: 2013-12-25 21:39
I have built 3.4.0a4 and run - same thing
msg207087 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2013-12-29 23:54
Yep, those docs are just wrong. I'm trying to think of a concise rewording.
msg213668 - (view) Author: py.user (py.user) * Date: 2014-03-15 17:42
In proposed patches fix

Skiptest -> :exc:`SkipTest`
AssertionError -> :exc:`AssertionError`
msg214379 - (view) Author: Nitika Agarwal (nitika) * Date: 2014-03-21 15:32
I am attaching a patch with the changes made as suggested by py.user.
msg215618 - (view) Author: Nitika Agarwal (nitika) * Date: 2014-04-05 14:24
Please review the patch attached.
msg216375 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2014-04-15 20:12
Patch looks good to me.
msg216427 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-04-16 03:45
New changeset fdadc152b964 by Terry Jan Reedy in branch '2.7':
Issue #18566: Clarify unittest setUp, tearDown doc. Patch by Nitika Agarwal.

New changeset 9ab66a7f654a by Terry Jan Reedy in branch '3.4':
Issue #18566: Clarify unittest setUp, tearDown doc. Patch by Nitika Agarwal.

New changeset 72b1715e18c1 by Terry Jan Reedy in branch '2.7':
#18566: Whitespace

New changeset a37440dec1b6 by Terry Jan Reedy in branch '3.4':
#18566: Whitespace
msg216428 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-04-16 03:47
Nitika, congrats on first patch. One minor problem: spaces at end of lines. It it my fault for not checking, but try to avoid them anyway.
msg216563 - (view) Author: Nitika Agarwal (nitika) * Date: 2014-04-16 19:40
@Terry J.Reedy
Thanks a lot.And yes, I will take care of it next time.
Date User Action Args
2014-04-16 19:40:03nitikasetmessages: + msg216563
2014-04-16 03:47:01terry.reedysetstatus: open -> closed
resolution: fixed
messages: + msg216428

stage: needs patch -> resolved
2014-04-16 03:45:22python-devsetnosy: + python-dev
messages: + msg216427
2014-04-16 03:28:58terry.reedysetassignee: docs@python -> terry.reedy
2014-04-15 20:12:01michael.foordsetmessages: + msg216375
2014-04-05 14:24:16nitikasetmessages: + msg215618
2014-03-21 15:32:59nitikasetfiles: + document18566.patch
nosy: + nitika
messages: + msg214379

2014-03-15 17:42:12py.usersetmessages: + msg213668
2014-03-14 16:56:20sreepriyasetfiles: + doc18566.patch
2013-12-29 23:54:33michael.foordsetmessages: + msg207087
2013-12-25 21:39:42py.usersetmessages: + msg206931
2013-12-25 21:23:52Julian.Gindisetmessages: + msg206930
2013-12-25 21:16:38terry.reedysetmessages: + msg206929
2013-12-25 20:45:05Julian.Gindisetmessages: + msg206928
2013-11-20 01:15:28Julian.Gindisetfiles: + issue18566.patch

nosy: + Julian.Gindi
messages: + msg203454

keywords: + patch
2013-08-03 09:30:40pconnellsetnosy: + pconnell
2013-07-27 20:56:14terry.reedysetnosy: + terry.reedy

messages: + msg193796
versions: + Python 2.7, Python 3.3
2013-07-27 05:16:16ezio.melottisetkeywords: + easy
nosy: + ezio.melotti, michael.foord

stage: needs patch
2013-07-27 05:12:17py.usercreate