Django Unitest 检查模板变量的值

标签 django django-unittest

假设我在模板中有 {{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/

相关文章:

django - 检查 pdb 中的 django unittest 错误

django - 使用 Django 测试客户端测试预期的 404 会导致未处理的异常

python - 无法登录 Django 单元测试

python - 如何计算 Django 中跨字段的值?

Django 0.0.0.0 :80; can't access remotely

django - Blank=False,null=False 在模型代码 Django 中不起作用

python - Django 测试 - 打印一系列 n 次相同测试的所有测试失败

python - 如何从 View 类测试 Django 重定向

javascript - 谷歌地图 Default.css 样式表导入

django - Axios 不存储 Django session cookie