Skip to content
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

Closed
techtonik mannequin opened this issue Apr 4, 2011 · 9 comments
Closed

assert for exception parameters #55968

techtonik mannequin opened this issue Apr 4, 2011 · 9 comments
Labels
tests Tests in the Lib/test dir

Comments

@techtonik
Copy link
Mannequin

techtonik mannequin commented Apr 4, 2011

BPO 11759
Nosy @ezio-melotti, @bitdancer, @durban

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:

assignee = None
closed_at = <Date 2011-04-04.13:45:22.825>
created_at = <Date 2011-04-04.06:49:13.570>
labels = ['invalid', 'tests']
title = 'assert for exception parameters'
updated_at = <Date 2011-04-04.14:27:03.934>
user = 'https://bugs.python.org/techtonik'

bugs.python.org fields:

activity = <Date 2011-04-04.14:27:03.934>
actor = 'techtonik'
assignee = 'none'
closed = True
closed_date = <Date 2011-04-04.13:45:22.825>
closer = 'r.david.murray'
components = ['Tests']
creation = <Date 2011-04-04.06:49:13.570>
creator = 'techtonik'
dependencies = []
files = []
hgrepos = []
issue_num = 11759
keywords = []
message_count = 9.0
messages = ['132919', '132923', '132924', '132925', '132944', '132945', '132946', '132947', '132949']
nosy_count = 4.0
nosy_names = ['techtonik', 'ezio.melotti', 'r.david.murray', 'daniel.urban']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue11759'
versions = []

@techtonik
Copy link
Mannequin Author

techtonik mannequin commented Apr 4, 2011

I've just realized that unittest doesn't provide a way to test arguments of exception thrown during assertRaises check.

@techtonik techtonik mannequin added the tests Tests in the Lib/test dir label Apr 4, 2011
@durban
Copy link
Mannequin

durban mannequin commented Apr 4, 2011

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?

@techtonik
Copy link
Mannequin Author

techtonik mannequin commented Apr 4, 2011

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.

@techtonik
Copy link
Mannequin Author

techtonik mannequin commented Apr 4, 2011

I found that successful assert in twisted returns exception object. But thanks for workaround anyway.

@bitdancer
Copy link
Member

Using assertRaises as a context manager is not a hack, and is the correct way to do this in unittest.

@ezio-melotti
Copy link
Member

See also bpo-6275.

@techtonik
Copy link
Mannequin Author

techtonik mannequin commented Apr 4, 2011

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.

@ezio-melotti
Copy link
Member

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 assertRaises0 also is quite clear and even includes an example where the exception args are checked.

@techtonik
Copy link
Mannequin Author

techtonik mannequin commented Apr 4, 2011

Thanks for clarification, Ezio. I am still using Python 2.7 - that's why I've missed this part.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir
Projects
None yet
Development

No branches or pull requests

3 participants