7

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'
asked Nov 21, 2012 at 10:55
2

1 Answer 1

17

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)
answered Nov 21, 2012 at 10:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.