Tests: How to messages.add_message() before self.client.get() ?

43 views
Skip to first unread message

Carsten Fuchs

unread,
Nov 10, 2020, 2:43:39 AM11/10/20
to Django users
Dear group,

in my tests I would like to test the processing of messages in the view.

For example:

def test_is_message_properly_handled(self):
# The problem: We have no `request` here!
messages.info(request, 'Hello')
response = self.client.get("/handling-messages-view/")
# asserts …

This is an artificial example and I'm aware that I could submit a POST request and then use `follow=True` so that eventually the messages are processed. But I would like to setup messages and their processing independently as shown above.
The problem is that we don't have the `request` instance that is used by the test client.
Is this possible?

Best regards,
Carsten


David Nugent

unread,
Nov 10, 2020, 8:29:52 PM11/10/20
to Carsten Fuchs, django...@googlegroups.com
Make your own request.

I don't see the need here but incase I'm missing something: request is just a HTTPRequest instance. You can instantiate it like any other python class. The only downside is that the request you manufacture won't have middleware applied or in fact anything else that is populated and processed before it gets to your handler (headers, method, parsed url parts, content and so on).

This may not actually be an issue in test scenarios, depending on your need, and in this case on the backing store used by the messages framework.  If it is set to session, then you'll need to obtain a session from the client and stick it onto your request object if you want that to persist through your subsequent test requests.

HTH /d

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/97157990-be0a-39f4-c7d2-a3595b8e2592%40cafu.de.

Carsten Fuchs

unread,
Nov 13, 2020, 3:08:26 AM11/13/20
to David Nugent, django...@googlegroups.com
Thank you very much!
I'll try that!

Best regards,
Carsten


Am 11.11.20 um 02:28 schrieb David Nugent:
> Make your own request.
>
> I don't see the need here but incase I'm missing something: request is just a HTTPRequest <https://docs.djangoproject.com/en/3.1/ref/request-response/#httprequest-objects> instance. You can instantiate it like any other python class. The only downside is that the request you manufacture won't have middleware applied or in fact anything else that is populated and processed before it gets to your handler (headers, method, parsed url parts, content and so on).
>
> This may not actually be an issue in test scenarios, depending on your need, and in this case on the backing store <https://docs.djangoproject.com/en/3.1/ref/contrib/messages/#storage-backends> used by the messages framework.  If it is set to session, then you'll need to obtain a session from the client and stick it onto your request object if you want that to persist through your subsequent test requests.
>
> HTH /d
>
>> On 10 Nov 2020, at 18:41, Carsten Fuchs <carste...@cafu.de <mailto:carste...@cafu.de>> wrote:
>>
>> Dear group,
>>
>> in my tests I would like to test the processing of messages in the view.
>>
>> For example:
>>
>>    def test_is_message_properly_handled(self):
>>        # The problem: We have no `request` here!
>>        messages.info <http://messages.info>(request, 'Hello')
Reply all
Reply to author
Forward
0 new messages