How can I unit test django messages?

前端 未结 6 669
南笙
南笙 2020-12-24 04:08

In my django application, I\'m trying to write a unit test that performs an action and then checks the messages in the response.

As far as I can tell, there is no n

6条回答
  •  不思量自难忘°
    2020-12-24 04:55

    From django documentation:

    Outside of templates, you can use get_messages()

    So, you could write something like:

    from django.contrib.messages import get_messages
    
    [...]
    
    messages = [m.message for m in get_messages(response.wsgi_request)]
    self.assertIn('My message', messages)
    

提交回复
热议问题