New GitHub issue #96362 from ptmcg:<br>
<hr>
<pre>
<!--
If you're new to Python and you're not sure whether what you're experiencing is a bug, the CPython issue tracker is not
the right place to seek help. Consider the following options instead:
- reading the Python tutorial: https://docs.python.org/3/tutorial/
- posting in the "Users" category on discuss.python.org: https://discuss.python.org/c/users/7
- emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list
- searching our issue tracker (https://github.com/python/cpython/issues) to see if
your problem has already been reported
-->
# Bug report
[Docs](https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises) 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.)
``` python
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
<!-- Include as many relevant details as possible about the environment you experienced the bug in -->
- CPython versions tested on: 3.6 through 3.11
- Operating system and architecture: Windows 11
</pre>
<hr>
<a href="https://github.com/python/cpython/issues/96362">View on GitHub</a>
<p>Labels: type-bug</p>
<p>Assignee: </p>