This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: assertRaisesRegex doc'd with 'msg' arg, but it's not implemented?
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: Brian.Jones, benjamin.peterson, eric.araujo, ezio.melotti
Priority: normal Keywords:

Created on 2011-07-10 16:34 by Brian.Jones, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg140084 - (view) Author: Brian Jones (Brian.Jones) * Date: 2011-07-10 16:34
The documentation here:
http://docs.python.org/dev/library/unittest.html#unittest.TestCase.assertRaisesRegex

Indicates that, when used as a context manager, assertRaisesRegex should accept a keyword argument 'msg'. However, that doesn't appear to actually be implemented. I've just now done an hg pull, and in Lib/unittest/case.py, the source is here: 

    def assertRaisesRegex(self, expected_exception, expected_regex,
                          callable_obj=None, *args, **kwargs):
        """Asserts that the message in a raised exception matches a regex.

        Args:
            expected_exception: Exception class expected to be raised.
            expected_regex: Regex (re pattern object or string) expected
                    to be found in error message.
            callable_obj: Function to be called.
            msg: Optional message used in case of failure. Can only be used
                    when assertRaisesRegex is used as a context manager.
            args: Extra args.
            kwargs: Extra kwargs.
        """
        context = _AssertRaisesContext(expected_exception, self, callable_obj,
                                       expected_regex)

        return context.handle('assertRaisesRegex', callable_obj, args, kwargs)

I noticed this after attempting some simple example uses of assertRaisesRegex. Perhaps I'm just missing something that will be made obvious to others by seeing them. These are just various attempts to get my msg shown somewhere in the output: 

#!/usr/bin/env python3.3
import unittest

class TestInt(unittest.TestCase):
    def test_intfail(self):
        # this test should *not* fail, and doesn't
        with self.assertRaisesRegex(ValueError, 'literal'):
            int('XYZ')

    def test_intfail2(self):
        # should not fail, and doesn't
        with self.assertRaisesRegex(ValueError, 'lambda', msg='Foo!'):
            int('ABC')

    def test_intfail3(self):
        # should fail, and does, but no msg to be found.
        with self.assertRaisesRegex(ValueError, 'literal', msg='Foo!'):
            int(1)

    def test_intfail4(self):
        # should fail, and does, but no msg to be found.
        with self.assertRaisesRegex(TypeError, 'literal', msg='Foo!'):
            int('ABC')

if __name__ == '__main__':
    unittest.main()
msg140085 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-07-10 17:29
You're not getting this?

.FFE
======================================================================
ERROR: test_intfail4 (__main__.TestInt)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "x.py", line 22, in test_intfail4
    int('ABC')
ValueError: invalid literal for int() with base 10: 'ABC'

======================================================================
FAIL: test_intfail2 (__main__.TestInt)
----------------------------------------------------------------------
ValueError: invalid literal for int() with base 10: 'ABC'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "x.py", line 12, in test_intfail2
    int('ABC')
AssertionError: "lambda" does not match "invalid literal for int() with base 10: 'ABC'" : Foo!

======================================================================
FAIL: test_intfail3 (__main__.TestInt)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "x.py", line 17, in test_intfail3
    int(1)
AssertionError: ValueError not raised : Foo!

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (failures=2, errors=1)
msg140086 - (view) Author: Brian Jones (Brian.Jones) * Date: 2011-07-10 17:45
No, I'm not. I'm sorry for not including this output initially. Here's what I get (and I've added a sys.version_info line just to be double sure the right executable is being invoked at runtime): 

sys.version_info(major=3, minor=3, micro=0, releaselevel='alpha', serial=0)
.FFE
======================================================================
ERROR: test_intfail4 (__main__.TestInt)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./test_int.py", line 21, in test_intfail4
    int('ABC')
ValueError: invalid literal for int() with base 10: 'ABC'

======================================================================
FAIL: test_intfail2 (__main__.TestInt)
----------------------------------------------------------------------
ValueError: invalid literal for int() with base 10: 'ABC'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./test_int.py", line 13, in test_intfail2
    int('ABC')
AssertionError: "lambda" does not match "invalid literal for int() with base 10: 'ABC'"

======================================================================
FAIL: test_intfail3 (__main__.TestInt)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./test_int.py", line 17, in test_intfail3
    int(1)
AssertionError: ValueError not raised

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (failures=2, errors=1)
msg140087 - (view) Author: Brian Jones (Brian.Jones) * Date: 2011-07-10 17:46
If there's some reason, based on the source snippet I posted from case.py, that my msg should be making it to the output, can someone explain why/how it should get there? I don't see any reason, from looking at the source, that 'msg' should even be expected to make it to the output.  Thanks!
msg145997 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-10-20 01:47
Brian, can you still reproduce this?

I tried now with "Python 3.3.0a0 (default:bfbe144986d7, Oct 20 2011, 04:35:19)" and I can see the message (same output posted by Benjamin).

If I run the test with python3.2 (i.e. "Python 3.2 (r32:88445, Mar 25 2011, 19:28:28)"), I don't get the message (same output you posted), because msg is not implemented in 3.2.  Your sys.version_info output suggests you are running 3.3, but maybe you had an older version of the code (i.e. still 3.3 but before 8fc801ca9ea1)?  Have you updated after the pull?
msg146000 - (view) Author: Brian Jones (Brian.Jones) * Date: 2011-10-20 02:12
I've just done a fresh hg pull and new build, and I can no longer reproduce the problem. Yay!
msg146001 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-10-20 02:20
OK, I will close the issue then.
Thanks for the quick reply!
History
Date User Action Args
2022-04-11 14:57:19adminsetgithub: 56736
2011-10-20 02:20:39ezio.melottisetstatus: open -> closed

messages: + msg146001
stage: resolved
2011-10-20 02:12:15Brian.Jonessetmessages: + msg146000
2011-10-20 01:47:46ezio.melottisetresolution: works for me
messages: + msg145997
2011-07-19 13:26:04eric.araujosetnosy: + eric.araujo
2011-07-13 12:21:05ezio.melottisetassignee: ezio.melotti

nosy: + ezio.melotti
2011-07-10 17:46:58Brian.Jonessetmessages: + msg140087
2011-07-10 17:45:06Brian.Jonessetmessages: + msg140086
2011-07-10 17:29:02benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg140085
2011-07-10 16:34:50Brian.Jonescreate