Django unitest检查模板变量的值 [英] Django Unitest Checking Value Of Template Variable

查看:81
本文介绍了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.

这篇关于Django unitest检查模板变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆