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
user1251007
17k14 gold badges52 silver badges78 bronze badges
-
1Possible duplicate of Is it possible for a unit test to assert that a method calls sys.exit()AGK– AGK2019年07月02日 15:45:31 +00:00Commented Jul 2, 2019 at 15:45
-
It's the other way: the other question is the duplicate since it was posted in 2013, this question was asked and answered in 2012user1251007– user12510072019年07月02日 19:45:39 +00:00Commented Jul 2, 2019 at 19:45
1 Answer 1
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
user1251007
17k14 gold badges52 silver badges78 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py