Closed 13bd9cc7-04b0-4774-9e07-8b0322e89dcc closed 10 years ago
http://docs.python.org/3/library/unittest.html#unittest.TestCase.setUp "any exception raised by this method will be considered an error rather than a test failure"
http://docs.python.org/3/library/unittest.html#unittest.TestCase.tearDown "Any exception raised by this method will be considered an error rather than a test failure."
utest.py
import unittest
class Test(unittest.TestCase):
def setUp(self):
raise AssertionError
def tearDown(self):
raise AssertionError
def test_nothing(self):
pass
[guest@localhost py]$ python3 -m unittest -v utest test_nothing (utest.Test) ... FAIL
\====================================================================== FAIL: test_nothing (utest.Test) ----------------------------------------------------------------------
Traceback (most recent call last):
File "./utest.py", line 8, in setUp
raise AssertionError
AssertionError
Ran 1 test in 0.000s
FAILED (failures=1) [guest@localhost py]$
also raising unittest.SkipTest works properly
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.
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.
Looks like this issue has been resolved. Can we close it?
Resolved in what way? The doc seems unchanged.
Sorry. I meant, merged.
I have built 3.4.0a4 and run - same thing
Yep, those docs are just wrong. I'm trying to think of a concise rewording.
In proposed patches fix
Skiptest -> :exc:`SkipTest` AssertionError -> :exc:`AssertionError`
Hi, I am attaching a patch with the changes made as suggested by py.user.
Hi, Please review the patch attached.
Patch looks good to me.
New changeset fdadc152b964 by Terry Jan Reedy in branch '2.7': Issue bpo-18566: Clarify unittest setUp, tearDown doc. Patch by Nitika Agarwal. http://hg.python.org/cpython/rev/fdadc152b964
New changeset 9ab66a7f654a by Terry Jan Reedy in branch '3.4': Issue bpo-18566: Clarify unittest setUp, tearDown doc. Patch by Nitika Agarwal. http://hg.python.org/cpython/rev/9ab66a7f654a
New changeset 72b1715e18c1 by Terry Jan Reedy in branch '2.7': bpo-18566: Whitespace http://hg.python.org/cpython/rev/72b1715e18c1
New changeset a37440dec1b6 by Terry Jan Reedy in branch '3.4': bpo-18566: Whitespace http://hg.python.org/cpython/rev/a37440dec1b6
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.
@Terry J.Reedy Thanks a lot.And yes, I will take care of it next time.
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: ```python assignee = 'https://github.com/terryjreedy' closed_at =
created_at =
labels = ['easy', 'type-feature', 'docs']
title = "In unittest.TestCase docs for setUp() and tearDown() don't mention AssertionError"
updated_at =
user = 'https://github.com/py-user'
```
bugs.python.org fields:
```python
activity =
actor = 'nitika'
assignee = 'terry.reedy'
closed = True
closed_date =
closer = 'terry.reedy'
components = ['Documentation']
creation =
creator = 'py.user'
dependencies = []
files = ['32716', '34420', '34550']
hgrepos = []
issue_num = 18566
keywords = ['patch', 'easy']
message_count = 15.0
messages = ['193772', '193796', '203454', '206928', '206929', '206930', '206931', '207087', '213668', '214379', '215618', '216375', '216427', '216428', '216563']
nosy_count = 9.0
nosy_names = ['terry.reedy', 'ezio.melotti', 'michael.foord', 'docs@python', 'py.user', 'python-dev', 'pconnell', 'Julian.Gindi', 'nitika']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue18566'
versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']
```