0

I have this function which raises the ObjectDoesNotExist

def is_user_login(self,id):
    try:
        u = m.CustomUser.objects.get(id=id)
    except ObjectDoesNotExist as e:
        raise e

Now I am writing the test script.

    try:
        CommonFunc.is_user_login(4)
    except Exception as e:
        print(e)
        self.assertEqual(ObjectDoesNotExist,e)

It doesn't work.

It shows error like this below ,

AssertionError: <class 'django.core.exceptions.ObjectDoesNotExist'> != DoesNotExist('CustomUser matching query does not exist.')

How can I assert for ObjectDoesNotEqual?

0

1 Answer 1

0

This is normally done with an .assertRaises(…) [Python-doc] context processor:

with self.assertRaises(ObjectDoesNotExist):
    CommonFunc.is_user_login(4)

However re-raising the exception however does not seem to make much sense, why not:

def is_user_login(self, id):
    # no try
    u = m.CustomUser.objects.get(id=id)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.