Yes. sys.exit
raises SystemExit
, so you can check it with assertRaises
:
with self.assertRaises(SystemExit):
your_method()
Instances of SystemExit
have an attribute code
which is set to the proposed exit status, and the context manager returned by assertRaises
has the caught exception instance as exception
, so checking the exit status is easy:
with self.assertRaises(SystemExit) as cm:
your_method()
self.assertEqual(cm.exception.code, 1)
Exit from Python. This is implemented by raising the
SystemExit
exception … it is possible to intercept the exit attempt at an outer level.