classification
Title: In unittest.TestCase.assertWarns doc there is some text about assertRaises()
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: docs@python, py.user, python-dev, terry.reedy
Priority: normal Keywords: patch

Created on 2013-07-27 18:20 by py.user, last changed 2013-07-31 07:01 by py.user. This issue is now closed.

Files
File name Uploaded Description Edit
issue18573.diff py.user, 2013-07-27 18:20 review
Messages (7)
msg193788 - (view) Author: py.user (py.user) * Date: 2013-07-27 18:20
http://docs.python.org/3/library/unittest.html#unittest.TestCase.assertWarns
"When used as a context manager, assertRaises() accepts"
"... is to perform additional checks on the exception raised"
msg193791 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-07-27 20:16
New changeset 55dcf9e065be by Terry Jan Reedy in branch '3.3':
Issue #18573: Complete copy-paste from assertRaises entry to assertWarns entry.
http://hg.python.org/cpython/rev/55dcf9e065be
msg193792 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-07-27 20:17
assertWarns is new in 3.2 and not in 2.7.
The sentence was copied from the assertRaises section but not changed.
Thanks for reporting.
msg193969 - (view) Author: py.user (py.user) * Date: 2013-07-31 01:22
What about the second line?
It doesn't catch any exception


utest.py

#!/usr/bin/env python3

import unittest

class Test(unittest.TestCase):

    def test_warning(self):
        import warnings

        with self.assertWarns(RuntimeWarning) as cm:
            raise ValueError('f')
            warnings.warn('f', RuntimeWarning)
        print(repr(cm.warning))


[guest@localhost py]$ python3 -m unittest utest
E
======================================================================
ERROR: test_warning (utest.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./utest.py", line 11, in test_warning
    raise ValueError('f')
ValueError: f

----------------------------------------------------------------------
Ran 1 test in 0.000s

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


with commented raise

[guest@localhost py]$ python3 -m unittest utest
RuntimeWarning('f',)
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
[guest@localhost py]$


in the patch I copied terms from the first sentence of the paragraph
msg193971 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-07-31 02:30
> What about the second line?
What second line?
> It doesn't catch any exception
It? I do not understand your point. Your example works as documented, including "any unexpected exception is an error".

Actually, that should be 'any exception' as none is expected.

Also, "additional checks on the exception raised:" should be 'warning caught'
msg193972 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-07-31 02:31
New changeset 366beee880aa by Terry Jan Reedy in branch '3.3':
Issue #18573: More copy-paste fixes to assertWarns entry.
http://hg.python.org/cpython/rev/366beee880aa
msg193977 - (view) Author: py.user (py.user) * Date: 2013-07-31 07:01
> What second line?
the second line patched in the diff file
History
Date User Action Args
2013-07-31 07:01:01py.usersetmessages: + msg193977
2013-07-31 02:31:47python-devsetmessages: + msg193972
2013-07-31 02:30:35terry.reedysetmessages: + msg193971
2013-07-31 01:22:37py.usersetmessages: + msg193969
2013-07-27 20:17:33terry.reedysetstatus: open -> closed
versions: - Python 2.7
type: enhancement -> behavior
messages: + msg193792

resolution: fixed
stage: needs patch -> resolved
2013-07-27 20:16:13python-devsetnosy: + python-dev
messages: + msg193791
2013-07-27 20:09:06terry.reedysetassignee: docs@python -> terry.reedy

nosy: + terry.reedy
2013-07-27 19:04:44terry.reedysetstage: needs patch
versions: + Python 2.7, Python 3.3
2013-07-27 18:20:02py.usercreate