Skip to content

unittest.TestCase.assertRaises msg argument is not used #96362

@ptmcg

Description

@ptmcg

Bug report

Docs indicate that unitTest.TestCase.assertRaises supports an optional msg argument when used as a context manager, as an optional additional filter on matching exceptions.

The following code shows that the msg argument is not used. The test should fail because the exception string does not match the given compiled regex.

(It would also be nice if the initializer would accept a str and convert it to a compiled regex. There is no mention in the docs of what type is expected for msg.)

import unittest
import re

class AssertRaisesTest(unittest.TestCase):
    def test_x(self):
        d = {}
        # should not pass this test, since the exception 
        # string does not match the msg re
        with self.assertRaises(KeyError, msg=re.compile(r'xyz')) as ar:
            d['a']

if __name__ == '__main__':
    unittest.main()

Your environment

  • CPython versions tested on: 3.6 through 3.11
  • Operating system and architecture: Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions