How to get around "sys.exit()" in python nosetest?

Hailiang Zhang picture Hailiang Zhang · Nov 30, 2011 · Viewed 11.4k times · Source

It seems that python nosetest will quit when encountered "sys.exit()", and mocking of this built-in doesn't work. Thanks for suggestions.

Answer

kichik picture kichik · Nov 30, 2011

You can try catching the SystemExit exception. It is raised when someone calls sys.exit().

with self.assertRaises(SystemExit):
  myFunctionThatSometimesCallsSysExit()