Hello all,
A recent PR[1] seeks to replace most of the assertRaises() calls in the test-
suite with assertRaisesMessage(). The argument for it is that it then becomes
easier to find the test for some error handling, by grepping the test for the
message text. Also, in some cases, an existing assertRaises() is followed by a
separate, explicit verification of the message text[2], which surely indicates
that assertRaisesMessage() should have been used; and actually, this whole
move initiated as part of the Py2-support-deprecation changes.
However, I don't think we should generally prefer assertRaisesMessage to
assertRaises, because it means that trivial phrasing changes need to be made
twice (at least) instead of once. I don't believe exception messages should be
considered part of the stable API (as opposed to exception types), and so, I
think it is, in many cases, actually wrong to test them as such.
In some places, it makes sense to assert that certain information is included
in the message, and then assertRaisesRegex could be used. Tim made the
practical argument that regexes in these circumstances introduce more
complexity than they're worth, and that is certainly a valid argument; I can
agree with a general policy of "prefer assertRaisesMessage to
assertRaisesRegex". But I think in most places, we should only test for the
exception type, and if more info is required, it should be structured -- that
is, presented as attributes on the exception object rather than text in the
message.
What do you think?
Thanks,
Shai.
[1]
https://github.com/django/django/pull/8257
[2]
https://github.com/django/django/pull/8257#pullrequestreview-30486470