mercredi 4 octobre 2017

How can I inspect a ValidationError exception while testing with django TestCase?

I'm working with Django testing using a django.test.TestCase and I would like to know the pythonic way to inspect a validation error. I've come up with the below, which works, but it feels a little long winded and prone to error over a large number of tests

    self.valid_account.contact_number = "1234567" # Too short
    with self.assertRaises(ValidationError):
        try:
            self.valid_account.full_clean()
            self.fail("Did not raise validation error")
        except ValidationError as e:
            self.assertTrue('contact_number' in e.message_dict)
            raise e

Is there a better way to inspect and test an exception?

Aucun commentaire:

Enregistrer un commentaire