Message189945
When creating the error handling tests for the new ipaddress module, one of the things I added was an "assertCleanError" method on the test cases [1].
This is a wrapper around assertRaisesRegex which ensures that either no exception context is set, or if there is one set, ensures that the display is suppressed by default.
While I don't think assertCleanError itself is suitable for adoption (there are two many use case specific details), I think it *would* be useful to provide two related helper assertions along the lines of the following (the failure message in the first case could likely be improved).
Firstly, ensuring that the context has been *suppressed* when it should have been:
def assertCleanTraceback(self, exc, msg=None):
exc_context = exc.__context__
if exc_context is not None and not exc.__suppress_context__:
if msg is None:
fmt = "{} has context set: {}"
msg = fmt.format(type(exc), type(exc_context))
self.fail(msg)
return exc_context
and secondly ensuring that the cause has been *set* when it should have been:
def assertRaisedFrom(self, exc, expected_cause, expected_regex, msg=None):
exc_cause = exc.__cause__
# Use the guts of assertRaises to compare the actual cause
# and the expected cause and raise an appropriate failure
# if they don't match
return exc_cause
Both proposed helpers return the actual context/cause so they can be used to test longer deliberate exception chaings.
[1] http://hg.python.org/cpython/file/04ca3f1515cf/Lib/test/test_ipaddress.py#l37 |
|
Date |
User |
Action |
Args |
2013-05-25 06:55:59 | ncoghlan | set | recipients:
+ ncoghlan, michael.foord |
2013-05-25 06:55:59 | ncoghlan | set | messageid: <1369464959.25.0.285327189549.issue18054@psf.upfronthosting.co.za> |
2013-05-25 06:55:59 | ncoghlan | link | issue18054 messages |
2013-05-25 06:55:58 | ncoghlan | create | |
|