I have a management command that can do stuff, or can return a sys.exit().
I'm trying to handle the second case as follows in my unit tests:
with self.assertRaises(SystemExit) as cm:
call_command('geocode_practices', *args, **opts)
self.assertEqual(cm.exception, 1)
But this gives me:
AssertionError: None != 1
What am I doing wrong?
Also, what's the best way to handle the different scenarios? At the moment my test will fail if the script does not exit.
1 Answer 1
Apparently the code inside your with block doesn't raise an exception. Note that sys.exit() just raises a SystemExit exception.
And even if it did, the exception will not compare equal to 1. If not None, the cm.exception attribute contains the exception instance, not a number.
1 Comment
SystemExit isn't raised, the test should never even reach the call to assertEqual.
sys.exit()case? And should this command be callingsys.exit()at all?SystemExitis really a bad idea.