This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: change assertRaises message when wrong exception is raised
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Nathaniel Manista, iritkatriel, kamilturek, mark.dickinson, rsk2, serhiy.storchaka, xtreak
Priority: normal Keywords:

Created on 2021-03-22 16:16 by rsk2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg389328 - (view) Author: R. Samuel Klatchko (rsk2) Date: 2021-03-22 16:16
Right now, this code:

    class FooError(Exception): pass
    class BarError(Exception): pass

    def test_me(self):
      with self.assertRaises(FooError):
        raise BarError("something")

will have the error "BarError: something" with no indication that an exception was expected but just that we got the wrong one.

It would be help to change the message to something like:

  Expected exception of type FooError but exception BarError('something') was raised.
msg398516 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-07-29 20:50
What you are suggesting replaces the type of the exception being raised. If it's something like a MemoryError or KeyboardInterrupt you don't want that, you want your test process to terminate.
msg398691 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-08-01 09:11
Another way to look at it is that your test has two problems. One is that the expected exception didn’t happen and the other is that an unexpected exception happened. 

The two problems are usually unrelated, and trying to conflate them into one error message can be confusing in some cases. When a test has multiple problems you get an error for the first one.
msg398693 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-01 10:46
I concur with Irit. The test is failed in any case, so you need to look at the code to fix it.

For the same reason we do not have assertNotRaises().
msg398701 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-08-01 14:53
I think I understand where the request comes from: in the case where you've changed the exception type that an API is expected to raise, and you've either updated the test to check for the new exception type and missed an update in the business logic, or vice versa, you'd like the test to tell you that the wrong exception type was raised. I've definitely done this myself on more than a few occasions.

So this *particular* case is an exception to Irit's observation that "The two problems are usually unrelated [...]". But I think it *is* an exception: I agree that the rule holds in general, and that the current behaviour shouldn't be changed.

I think we've got sufficient agreement from core devs to close here.
msg398702 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-08-01 14:56
I agree. That’s what the “usually” was for.
History
Date User Action Args
2022-04-11 14:59:43adminsetgithub: 87762
2021-08-01 14:56:19iritkatrielsetmessages: + msg398702
2021-08-01 14:53:55mark.dickinsonsetstatus: open -> closed

type: enhancement

nosy: + mark.dickinson
messages: + msg398701
resolution: rejected
stage: resolved
2021-08-01 10:46:45serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg398693
2021-08-01 09:11:14iritkatrielsetmessages: + msg398691
2021-07-29 20:50:36iritkatrielsetcomponents: + Library (Lib)
2021-07-29 20:50:24iritkatrielsetnosy: + iritkatriel
messages: + msg398516
2021-03-22 22:07:20kamiltureksetnosy: + kamilturek
2021-03-22 16:37:47xtreaksetnosy: + xtreak
2021-03-22 16:17:48Nathaniel Manistasetnosy: + Nathaniel Manista
2021-03-22 16:16:06rsk2create