When I had tested code which had called sys.exit(), my usual approach was to use mock:
sys.exit()
def test_exit(mymodule): with mock.patch.object(mymodule.sys, "exit") as mock_exit: mymodule.should_exit() assert mock_exit.call_args[0][0] == 42
Let’s assume you have a simple code:
try: import requestsexcept ImportError: no_requests = True
I’m grossly simplifying, but at certain point you will have found you want to have ‘soft imports’. (If you are doing something…
* pause for eye rolls *