#25948 closed Cleanup/optimization (fixed)
documentation doesn't explain why assertRaisesMessage is needed
Reported by: | Sergey Fedoseev | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.9 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Apparently there is a good reason for assertRaisesMessage
to exists, but documentation doesn't say a word if it's preferred over assertRaisesRegex
. If it's really preferred, it's usage should be explicitly encouraged.
Change History (8)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
I think that documentation should say that behavior of assertRaisesMessage
is more straightforward and that assertRaisesRegex
should be used only if regex is really needed.
follow-up: 5 comment:3 by , 9 years ago
Is this explicit enough?
-
docs/topics/testing/tools.txt
diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt index c41e35d..4491878 100644
a b your test suite. 1230 1230 .. method:: SimpleTestCase.assertRaisesMessage(expected_exception, expected_message, callable_obj=None, *args, **kwargs) 1231 1231 1232 1232 Asserts that execution of callable ``callable_obj`` raised the 1233 ``expected_exception`` exception and that such exception has an 1234 ``expected_message`` representation. Any other outcome is reported as a 1235 failure. Similar to unittest's :meth:`~unittest.TestCase.assertRaisesRegex` 1236 with the difference that ``expected_message`` isn't a regular expression. 1233 ``expected_exception`` exception and that ``expected_message`` is in the 1234 exception's representation. Any other outcome is reported as a failure. 1235 It's a simpler version of :meth:`unittest.TestCase.assertRaisesRegex` with 1236 the difference that ``expected_message`` isn't treated as a regular 1237 expression. 1237 1238 1238 1239 .. method:: SimpleTestCase.assertFieldOutput(fieldclass, valid, invalid, field_args=None, field_kwargs=None, empty_value='')
comment:4 by , 9 years ago
Has patch: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:5 by , 9 years ago
Replying to timgraham:
It's nice adjustment, but I thought that we should document somewhere that assertRaisesMessage
is preferred for Django internal usage. Does this make sense?
It was added in #14503. Documentation says, "Similar to unittest’s assertRaisesRegex() with the difference that expected_message isn’t a regular expression." I guess with
assertRaisesRegex
you will have to callre.escape(expected_message)
to ensure the message isn't treated as a regular expression. Should that point be noted? Another advantage from my perspective is thatassertRaisesMessage
won't need to change when dropping Python 2, unlikesix.assertRaisesRegex
(six
->self
and removingself
argument).