How do I import the Django DoesNotExist exception?

DjangoUnit TestingException

Django Problem Overview


I'm trying to create a UnitTest to verify that an object has been deleted.

from django.utils import unittest
def test_z_Kallie_can_delete_discussion_response(self):
  ...snip...
  self._driver.get("http://localhost:8000/questions/3/want-a-discussion") 
  self.assertRaises(Answer.DoesNotExist, Answer.objects.get(body__exact = '<p>User can reply to discussion.</p>'))

I keep getting the error:

DoesNotExist: Answer matching query does not exist.

Django Solutions


Solution 1 - Django

You can also import ObjectDoesNotExist from django.core.exceptions, if you want a generic, model-independent way to catch the exception:

from django.core.exceptions import ObjectDoesNotExist

try:
    SomeModel.objects.get(pk=1)
except ObjectDoesNotExist:
    print 'Does Not Exist!'

Solution 2 - Django

You don't need to import it - as you've already correctly written, DoesNotExist is a property of the model itself, in this case Answer.

Your problem is that you are calling the get method - which raises the exception - before it is passed to assertRaises. You need to separate the arguments from the callable, as described in the unittest documentation:

self.assertRaises(Answer.DoesNotExist, Answer.objects.get, body__exact='<p>User can reply to discussion.</p>')

or better:

with self.assertRaises(Answer.DoesNotExist):
    Answer.objects.get(body__exact='<p>User can reply to discussion.</p>')

Solution 3 - Django

DoesNotExist is always a property of the model that does not exist. In this case it would be Answer.DoesNotExist.

Solution 4 - Django

One thing to watch out for is that the second parameter to assertRaises needs to be a callable - not just a property. For instance, I had difficulties with this statement:

self.assertRaises(AP.DoesNotExist, self.fma.ap)

but this worked fine:

self.assertRaises(AP.DoesNotExist, lambda: self.fma.ap)

Solution 5 - Django

self.assertFalse(Answer.objects.filter(body__exact='<p>User...discussion.</p>').exists())

Solution 6 - Django

This is how I do such a test.

from foo.models import Answer

def test_z_Kallie_can_delete_discussion_response(self):

  ...snip...

  self._driver.get("http://localhost:8000/questions/3/want-a-discussion") 
  try:
      answer = Answer.objects.get(body__exact = '<p>User can reply to discussion.</p>'))      
      self.fail("Should not have reached here! Expected no Answer object. Found %s" % answer
  except Answer.DoesNotExist:
      pass # all is as expected

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBryanWheelockView Question on Stackoverflow
Solution 1 - DjangoChris PrattView Answer on Stackoverflow
Solution 2 - DjangoDaniel RosemanView Answer on Stackoverflow
Solution 3 - DjangodefrexView Answer on Stackoverflow
Solution 4 - DjangoXiong ChiamiovView Answer on Stackoverflow
Solution 5 - DjangoChrisView Answer on Stackoverflow
Solution 6 - DjangoSteve JalimView Answer on Stackoverflow