New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
assert for exception parameters #55968
Comments
I've just realized that unittest doesn't provide a way to test arguments of exception thrown during assertRaises check. |
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? |
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. |
I found that successful assert in twisted returns exception object. But thanks for workaround anyway. |
Using assertRaises as a context manager is not a hack, and is the correct way to do this in unittest. |
See also bpo-6275. |
I've also found bpo-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. |
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. The doc for assertRaises0 also is quite clear and even includes an example where the exception args are checked. |
Thanks for clarification, Ezio. I am still using Python 2.7 - that's why I've missed this part. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: