Django测试客户端忽略Django模板中的“包含”条款

时间:2019-01-19 14:14:14

标签: django testing django-testing

我对Django测试客户端有问题。 对于image_name = "J2ojU.png" # or whatever appropriate text = pytesseract.image_to_string(Image.open(image_name), lang='eng', config='--psm 7') 路径,我有这个模板(home):

home.html

<html> <body> {% include 'example.html' %} </body> </html> 中我遇到了错误:

example.html

我编写了一个可访问Django URL的测试。

<div>
   {% non_registered_tag arg1 arg2 %}
</div>

如果class HomePageAccess(TestCase): def test_home_page(self): client = Client() response = client.get(reverse_lazy('home')) self.assertEqual(response.status_code, 200) 中存在错误,则此代码成功失败,但是,home.html中包含的example.html中存在错误,即使我们希望失败,测试也会通过我将其包含在home.html中,并且在浏览器中遇到错误(状态码500),而在测试客户端中却没有发生。

正常吗?我正在使用Django 2.0.2。 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

我怀疑这里发生的事情是您在debug设置的False部分中将OPTIONS选项设置为TEMPLATES,或者您完全省略了它(在这种情况下,它将采用整体DEBUG设置的值。

debug显式设置为True应该会显示错误,并且测试应会按预期失败。

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'debug': True,
        },
    },
]

有关debug设置的更多信息可以在Django文档here的模板部分中找到。