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: assertRaises should return the exception in its simple form
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: duplicate
Dependencies: Superseder: unittest.assertRaises() return the raised exception
View: 9587
Assigned To: Nosy List: facundobatista, r.david.murray, rhettinger
Priority: normal Keywords:

Created on 2016-09-13 20:12 by facundobatista, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg276334 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2016-09-13 20:12
So, you could do:

  exc = self.assertRaises(ValueError, somefunc, someargs)

And then, explore "exc" as will. 

Yes, you can get the exception if you use assertRaises as a context manager, but that leads to more cumbersome code:

  with self.assertRaises(ValueError) as cm:
      somefunc(someargs)
      exc = cm.exception
msg276454 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-09-14 14:13
-0 This doesn't feel Pythonic to me.
msg276458 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-09-14 14:44
This has been proposed and rejected several times before.

IMO, a strong reason to reject it is that no other assert methods return values.  Consistency is important.

Also note that cm survives the context manager, so you don't need that assignment statement.
History
Date User Action Args
2022-04-11 14:58:36adminsetgithub: 72322
2018-01-03 16:11:31r.david.murraylinkissue32487 superseder
2016-09-14 14:44:34r.david.murraysetstatus: open -> closed

superseder: unittest.assertRaises() return the raised exception

nosy: + r.david.murray
messages: + msg276458
resolution: duplicate
stage: resolved
2016-09-14 14:13:58rhettingersetnosy: + rhettinger
messages: + msg276454
2016-09-13 20:12:11facundobatistacreate