【发布时间】:2017-08-28 20:02:24
【问题描述】:
在unittest 样式中,我可以通过调用assertTemplateUsed 来测试页面是否使用特定模板。这很有用,例如,当 Django 通过模板插入值时,我不能只测试字符串是否相等。
pytest中的等价语句应该怎么写?
我一直在寻找pytest-django,但不知道该怎么做。
【问题讨论】:
-
我不明白你想测试传递给模板的数据是否正确呈现的问题?
-
@Augustin 不,我想测试当我得到一个页面时,它使用的是我希望它使用的模板文件。
-
如果你得到“页面”,那么你得到的是模板,如果没有,你得到一个“页面”是什么意思?你能提供一些代码让我帮你吗?
-
assertTemplateUsed 和
_assert_template_used的代码显示reponse中有一个属性.templates。所以你需要像assert response.templates and ('mytemplate' in [t.name for t in response.templates if t.name is not None])这样的东西。
标签: python django pytest pytest-django