Opened 17 months ago
Last modified 16 months ago
#34730 closed New feature
Add an assertMessages assertion — at Version 2
Reported by: | François Freitag | Owned by: | nobody |
---|---|---|---|
Component: | contrib.messages | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Most projects I’ve worked on used the messages framework, and many of them were repeating some boilerplate in the form described in https://stackoverflow.com/questions/2897609/how-can-i-unit-test-django-messages.
from django.contrib.messages import get_messages messages = list(get_messages(response.wsgi_request)) self.assertEqual(len(messages), 1) self.assertEqual(str(messages[0]), 'my message')
I see several small pain points in this code:
- accessing
response.wsgi_request
, because get_messages needs arequest
but all we have is a response. get_messages
returns an iterator, that must be consumed to perform assertions, hence the casting to a list- the expectation lacks precision (missing the level) and clarity (in intent, and when failures are reported)
- it’s boilerplate-ish
Maybe adding an assertion to facilitate this test would benefit the other projects.
The code is small enough that it won’t be worth pulling down as a 3rd party package, especially since it requires changing the base test class over the entire test suite.
Change History (2)
comment:1 by , 17 months ago
comment:2 by , 17 months ago
Description: | modified (diff) |
---|
PR : https://github.com/django/django/pull/17101