I have a Python 2.7 method that sometimes calls
sys.exit(1)
Is it possible to make a unit test that verifies this line of code is called when
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
SystemExitexception ... it is possible to intercept the exit attempt at an outer level.