#34730: Add an assertMessages assertion
-----------------------------------+--------------------------------------
Reporter: François Freitag | Owner: nobody
Type: New feature | Status: new
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Description changed by François Freitag:
Old description:
> 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.
>
> {{{#!python
> 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 a
> `request` 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
> - 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.
New description:
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.
{{{#!python
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 a
`request` 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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/34730#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018978b32b41-04b9c7c7-0877-47ba-be1c-013b13e8f15a-000000%40eu-central-1.amazonses.com.