I have a Django custom command that can return non-zero exit codes. I’d like to test this command, but the call to sys.exit(1)
exits the test runner.
Is there a “proper” way to assign the exit code in a custom Django command?
My code currently looks like this:
JavaScript
x
4
1
# Exit with the appropriate return code
2
if len(result.failures) > 0 or len(result.errors) > 0:
3
sys.exit(1)
4
Advertisement
Answer
I chose to mock the call to sys.exit()
in the tests to prevent it to exit the test runner:
JavaScript
1
5
1
from mock import patch
2
with patch('sys.exit') as exit_mocked:
3
call_command( )
4
exit_mocked.assert_called_with(1)
5