[issue32971] unittest.TestCase.assertRaises
New submission from NODA, Kai <nodakai@gmail.com>: https://docs.python.org/dev/library/unittest.html#unittest.TestCase.assertRa...
If only the exception and possibly the msg arguments are given, return a context manager so that the code under test can be written inline rather than as a function:
with self.assertRaises(SomeException): do_something()
When used as a context manager, assertRaises() accepts the additional keyword argument msg.
Perhaps we don't need the second sentence on the `msg` argument which isn't adding anything new. Ideally it should be more clear when the method operates in context manager mode. ("If only" and "possibly" don't play nicely together.) Maybe along the lines of "If no callable was passed as an argument ..." ? I haven't looked in to the actual implementation yet... ---------- assignee: docs@python components: Documentation messages: 313061 nosy: docs@python, nodakai priority: normal severity: normal status: open title: unittest.TestCase.assertRaises versions: Python 3.8 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue32971> _______________________________________
Change by NODA, Kai <nodakai@gmail.com>: ---------- title: unittest.TestCase.assertRaises -> Docs on unittest.TestCase.assertRaises() should be improved _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue32971> _______________________________________
Change by Serhiy Storchaka <storchaka+cpython@gmail.com>: ---------- nosy: +ezio.melotti _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue32971> _______________________________________
Change by Brett Cannon <brett@python.org>: ---------- title: Docs on unittest.TestCase.assertRaises() should be improved -> Docs on unittest.TestCase.assertRaises() should clarify context manager details _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue32971> _______________________________________
R. David Murray <rdmurray@bitdance.com> added the comment: The implementation is literally that no non-keyword arguments other than the exception are given. If any keyword arguments other than msg are given, you get a warning. To say just "no callable" would be about as confusing as the 'and possibly' given the documented prototype, since in fact any non-keyword argument will be assumed to be the callable. So the text is correct as written, and I'm not sure how one would make it clearer and still be technically correct. But further suggestions are welcome. ---------- nosy: +r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue32971> _______________________________________
Change by Dave Shawley <daveshawley@gmail.com>: ---------- keywords: +patch pull_requests: +9605 stage: -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue32971> _______________________________________
Change by anthony shaw <anthonyshaw@apache.org>: ---------- pull_requests: -9605 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue32971> _______________________________________
Change by anthony shaw <anthonyshaw@apache.org>: ---------- stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue32971> _______________________________________
participants (6)
-
anthony shaw -
Brett Cannon -
Dave Shawley -
NODA, Kai -
R. David Murray -
Serhiy Storchaka