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: assert for exception parameters
Type: Stage: resolved
Components: Tests Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: daniel.urban, ezio.melotti, r.david.murray, techtonik
Priority: normal Keywords:

Created on 2011-04-04 06:49 by techtonik, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (9)
msg132919 - (view) Author: anatoly techtonik (techtonik) Date: 2011-04-04 06:49
I've just realized that unittest doesn't provide a way to test arguments of exception thrown during assertRaises check.
msg132923 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2011-04-04 09:27
What about this:

>>> class MyTestCase(TestCase):
...     def test_foo(self):
...             with self.assertRaises(SyntaxError) as cm:
...                     compile('asdf jkl', 'file.py', 'eval')
...             self.assertEqual('file.py', cm.exception.filename)

This isn't good enough?
msg132924 - (view) Author: anatoly techtonik (techtonik) Date: 2011-04-04 09:43
Looks like a hack (and not the obvious one). I guess no asserts return values like this, so that usage is not really intuitive for me.
msg132925 - (view) Author: anatoly techtonik (techtonik) Date: 2011-04-04 09:46
I found that successful assert in twisted returns exception object. But thanks for workaround anyway.
msg132944 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-04-04 13:45
Using assertRaises as a context manager is not a hack, and is the correct way to do this in unittest.
msg132945 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-04-04 13:59
See also #6275.
msg132946 - (view) Author: anatoly techtonik (techtonik) Date: 2011-04-04 14:04
I've also found #9587. Now I wonder how many people requested return vs context, and how many would vote for one vs another. Nothing personal - just a pure curiosity.
msg132947 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-04-04 14:17
That was a design decision made by Guido (iirc), and even if it might seem not obvious at first, it's probably just because not everyone is Dutch.
Many people would probably vote on sequence.join(sep) too, but it's unlikely that another way to do it will be introduced.

The doc for assertRaises[0] also is quite clear and even includes an example where the exception args are checked.

[0]: http://docs.python.org/py3k/library/unittest.html#unittest.TestCase.assertRaises
msg132949 - (view) Author: anatoly techtonik (techtonik) Date: 2011-04-04 14:27
Thanks for clarification, Ezio. I am still using Python 2.7 - that's why I've missed this part.
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55968
2011-04-04 14:27:03techtoniksetmessages: + msg132949
2011-04-04 14:17:14ezio.melottisetmessages: + msg132947
2011-04-04 14:04:41techtoniksetmessages: + msg132946
2011-04-04 13:59:38ezio.melottisetnosy: + ezio.melotti
messages: + msg132945
2011-04-04 13:45:22r.david.murraysetstatus: open -> closed
nosy: + r.david.murray
messages: + msg132944

2011-04-04 09:46:58techtoniksetmessages: + msg132925
2011-04-04 09:43:22techtoniksetmessages: + msg132924
2011-04-04 09:37:04michael.foordsetresolution: not a bug
stage: resolved
2011-04-04 09:27:08daniel.urbansetnosy: + daniel.urban
messages: + msg132923
2011-04-04 06:49:13techtonikcreate