假设我在模板中有 {{registered}} 变量。我写了一段测试:
def nice_test():
response = self.client.post(reverse('app:register'), {'username': 'dupa'}
然后我想检查在响应中注册的变量值。怎么做?
最佳答案
来自测试客户端的响应可以访问所使用的模板上下文。
def nice_test():
response = self.client.post(reverse('app:register'), {'username': 'dupa'})
self.assertEqual(response.context['registered'], '<expected value>')
这里是对官方文档的引用:https://docs.djangoproject.com/en/1.7/topics/testing/tools/#django.test.Response.context
class Response
...
context
The template Context instance that was used to render the template that produced the response content.
关于Django Unitest 检查模板变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27190359/