Opened 8 years ago

Closed 8 years ago

Last modified 6 years ago

#17971 closed Bug (wontfix)

RequestFactory requests can't be used to test views that call messages.add

Reported by: hoffmaje Owned by: nobody
Component: Testing framework Version: 1.4
Severity: Normal Keywords: RequestFactory, MessageFailure
Cc: hoffmaje Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


To reproduce the problem:

  • start new project with a new app
  • define a view 'my_view' that calls messages.add
  • define a test for that view as follows:
def test_my_view(self):
  request = RequestFactory().get('/my_view/')

Running the test outputs:

  File "/usr/local/lib/python2.7/dist-packages/django/contrib/messages/",
line 22, in add_message
    raise MessageFailure('You cannot add messages without installing '
MessageFailure: You cannot add messages without installing django.contrib.messag

Note: The same test passed in 1.3.

I could fix my tests, by setting:

setattr(request, 'session', 'session')
messages = FallbackStorage(request)
setattr(request, '_messages', messages)

Change History (4)

comment:1 Changed 8 years ago by hoffmaje

Cc: hoffmaje added

comment:2 Changed 8 years ago by budlight@…

Keywords: MessageFailure added

This isn't actually a test failure, but you get this exact same message in a default install of django 1.4 as soon as you enable the root logger with

'root': {

'level': 'WARNING',
'handlers': console?,


and then run
./ test messages

comment:3 Changed 8 years ago by Claude Paroz

Resolution: wontfix
Status: newclosed

I think this is by design. Quote from the RequestFactory documentation:

It does not support middleware. Session and authentication attributes must be supplied by the test itself if required for the view to function properly.

So either you can:

comment:4 Changed 6 years ago by anonymous

This makes the RequestFactory useless for my cases.

Note: See TracTickets for help on using tickets.
Back to Top