79545850

Date: 2025-03-31 09:01:00
Score: 2.5
Natty:
Report link

Old question, haven't seen my usual answer. I dislike what is offered here because:

But Django is as usual setting contradicting standards (yikes) and so here is their ValidationError best practice doc https://docs.djangoproject.com/en/5.0/ref/forms/validation/#raising-validationerror . In it they recommand passing a more stable code="some_error" parameter.

If you can and the exception provides something like an error code (there doesn't seem to be a Python standard for it?), I would suggest this:

with self.assertRaises(SomeError) as error:
    trigger_error()

    self.assertEqual(error.code, 'deliberate_error')

I'm kinda surprised to not see this answer here already. Am I missing something?

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: dialka