[Django]-Testing for a ValidationError in Django

8👍

Django does not automatically validate objects when you call save() or create(). You can trigger validation by calling the full_clean method.

def test_email_is_valid(self):
    enquiry = Enquiry(email="testtest.com")
    self.assertRaises(ValidationError, enquiry.full_clean)

Note that you don’t call full_clean() in the above code. You might prefer the context manager approach instead:

def test_email_is_valid(self):
    with self.assertRaises(ValidationError):
        enquiry = Enquiry(email="testtest.com")
        enquiry.full_clean()

Leave a comment