I am using unittest to assert that my script raises the right SystemExit
code.
Based on the example from http://docs.python.org/3.3/library/unittest.html#unittest.TestCase.assertRaises
with self.assertRaises(SomeException) as cm:
do_something()
the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)
I coded this:
with self.assertRaises(SystemExit) as cm:
do_something()
the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)
However, this does not work. The following error comes up:
AttributeError: 'SystemExit' object has no attribute 'error_code'
SystemExit derives directly from BaseException and not StandardError, thus it does not have the attribute error_code
.
Instead of error_code
you have to use the attribute code
. The example would look like this:
with self.assertRaises(SystemExit) as cm:
do_something()
the_exception = cm.exception
self.assertEqual(the_exception.code, 3)