2

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.

4
  • Is this test expected to hit the "do stuff" case, or the sys.exit() case? And should this command be calling sys.exit() at all? Commented Jun 19, 2015 at 16:53
  • 1
    Management command should be callable from other code, so raising a SystemExit is really a bad idea. Commented Jun 19, 2015 at 17:38
  • @brunodesthuilliers thanks, how then should I handle an exit from a management command? (I want to fail gracefully in a case where there's no network connection.) Commented Jun 22, 2015 at 10:41
  • @Richard docs.djangoproject.com/en/1.8/howto/custom-management-commands/… Commented Jun 22, 2015 at 10:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The odd thing is that if SystemExit isn't raised, the test should never even reach the call to assertEqual.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.