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)
sys.exit Documentation:
Exit from Python. This is implemented by raising the SystemExit
exception ... it is possible to intercept the exit attempt at an outer level.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…