python / cpython

The Python programming language
https://www.python.org
Other
61.98k stars 29.8k forks source link

In unittest.TestCase docs for setUp() and tearDown() don't mention AssertionError #62766

Closed 13bd9cc7-04b0-4774-9e07-8b0322e89dcc closed 10 years ago

13bd9cc7-04b0-4774-9e07-8b0322e89dcc commented 11 years ago
BPO 18566
Nosy @terryjreedy, @ezio-melotti, @voidspace, @py-user, @phmc
Files
  • issue18566.patch
  • doc18566.patch: Adding doc for AssertionError and Skiptest exception
  • document18566.patch
  • 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'] ```

    13bd9cc7-04b0-4774-9e07-8b0322e89dcc commented 11 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

    !/usr/bin/env python3

    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

    terryjreedy commented 11 years ago

    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.

    101e49dd-21b6-4cd8-be82-82a32b99a59e commented 10 years ago

    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.

    101e49dd-21b6-4cd8-be82-82a32b99a59e commented 10 years ago

    Looks like this issue has been resolved. Can we close it?

    terryjreedy commented 10 years ago

    Resolved in what way? The doc seems unchanged.

    101e49dd-21b6-4cd8-be82-82a32b99a59e commented 10 years ago

    Sorry. I meant, merged.

    13bd9cc7-04b0-4774-9e07-8b0322e89dcc commented 10 years ago

    I have built 3.4.0a4 and run - same thing

    voidspace commented 10 years ago

    Yep, those docs are just wrong. I'm trying to think of a concise rewording.

    13bd9cc7-04b0-4774-9e07-8b0322e89dcc commented 10 years ago

    In proposed patches fix

    Skiptest -> :exc:`SkipTest` AssertionError -> :exc:`AssertionError`

    277f7258-6406-4c1c-9774-e4fa9878b24e commented 10 years ago

    Hi, I am attaching a patch with the changes made as suggested by py.user.

    277f7258-6406-4c1c-9774-e4fa9878b24e commented 10 years ago

    Hi, Please review the patch attached.

    voidspace commented 10 years ago

    Patch looks good to me.

    1762cc99-3127-4a62-9baf-30c3d0f51ef7 commented 10 years ago

    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

    terryjreedy commented 10 years ago

    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.

    277f7258-6406-4c1c-9774-e4fa9878b24e commented 10 years ago

    @Terry J.Reedy Thanks a lot.And yes, I will take care of it next time.