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
As an additional note to Pavel's excellent answer, you can also check for specific statuses if they're provided in the function you're testing. For example, if your_method()
contained the following sys.exit("Error")
, it would be possible to test for "Error" specifically:
with self.assertRaises(SystemExit) as cm:
your_method()
self.assertEqual(cm.exception, "Error")