Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#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 Changed 7 years ago by Tim Graham

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 call re.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 that assertRaisesMessage won't need to change when dropping Python 2, unlike six.assertRaisesRegex (six -> self and removing self argument).

comment:2 Changed 7 years ago by Sergey Fedoseev

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.

comment:3 Changed 7 years ago by Tim Graham

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. 
    12301230.. method:: SimpleTestCase.assertRaisesMessage(expected_exception, expected_message, callable_obj=None, *args, **kwargs)
    12311231
    12321232    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.
    12371238
    12381239.. method:: SimpleTestCase.assertFieldOutput(fieldclass, valid, invalid, field_args=None, field_kwargs=None, empty_value='')

comment:4 Changed 7 years ago by Tim Graham

Has patch: set
Triage Stage: UnreviewedAccepted

comment:5 in reply to:  3 Changed 7 years ago by Sergey Fedoseev

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?

comment:6 Changed 7 years ago by Tim Graham

PR as you requested.

comment:7 Changed 7 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 77f50c9:

Fixed #25948 -- Added guidelines for SimpleTestCase.assertRaisesMessage() usage.

comment:8 Changed 7 years ago by Tim Graham <timograham@…>

In c0d8d5f9:

[1.9.x] Fixed #25948 -- Added guidelines for SimpleTestCase.assertRaisesMessage() usage.

Backport of 77f50c9cfc8b33bf86394816cde183e656ba28cd from master

Note: See TracTickets for help on using tickets.
Back to Top