Django unitest检查模板变量的值
问题描述:
)假设模板中有{{registered}}变量.我写了一段测试:
) Suppose I have {{registered}} variable in template. I wrote a piece of test:
def nice_test():
response = self.client.post(reverse('app:register;), {'username': 'dupa'}
,在那里我想检查作为响应注册的变量的值.怎么做 ?
and there I want to check value of variable registered in response. How to do it ?
答
来自测试客户端的响应可以访问所使用的模板上下文.
The response from the test client has access to the template context used.
def nice_test():
response = self.client.post(reverse('app:register'), {'username': 'dupa'})
self.assertEqual(response.context['registered'], '<expected value>')
Here is a reference to the official documentation: https://docs.djangoproject.com/en/1.7/topics/testing/tools/#django.test.Response.context
班级响应
...
上下文
模板Context实例,用于渲染生成响应内容的模板.
class Response
...
context
The template Context instance that was used to render the template that produced the response content.