When I had tested code which had called sys.exit(), my usual approach was to use mock: def test_exit(mymodule): with mock.patch.object(mymodule.sys, "exit") as mock_exit: mymodule.should_exit() assert mock_exit.call_args[0][0] == 42There are few things I don’t like here: Mocking is always a bit of a black magic, as we stop threating code as black box and start poking inside of it.If sys.exit is ca