Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 测试Django模板是否正确呈现_Python_Django_Testing_Pytest Django - Fatal编程技术网

Python 测试Django模板是否正确呈现

Python 测试Django模板是否正确呈现,python,django,testing,pytest-django,Python,Django,Testing,Pytest Django,我有一个index.html,其中包含一个nav.html,它有一个url标记,指向一个不存在的路由名称。当我运行索引视图测试时,响应代码是200,测试通过。如果我manage.py runserver并导航到浏览器中的索引,我会得到一个NoReverseMatch错误消息页面。当我从index.html中删除include并将nav.html的内容直接放入index.html中时,测试会按预期失败 如何编写一个测试来捕获包含模板中的问题? nav.html {% url 'project:wr

我有一个index.html,其中包含一个nav.html,它有一个url标记,指向一个不存在的路由名称。当我运行索引视图测试时,响应代码是200,测试通过。如果我
manage.py runserver
并导航到浏览器中的索引,我会得到一个NoReverseMatch错误消息页面。当我从index.html中删除include并将nav.html的内容直接放入index.html中时,测试会按预期失败

如何编写一个测试来捕获包含模板中的问题?

nav.html

{% url 'project:wrong_name' %}
index.html

{% include 'project/nav.html' %}
views.py

def index(request):
    return render(request, 'project/index.html')
tests.py

def test_index_view(client):
    response = client.get(reverse('project:index')
    assert response.status_code == 200
设置.py

...
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
...
virtualenv(缩写):


您是否启用了
DEBUG
?如果“调试”处于关闭状态,则默认情况下,呈现
include
期间引发的所有异常都将被静默。我已尝试在打开和关闭调试的情况下运行测试,并获得相同的结果。如果您已定义
settings.py
中的
模板,则在
settings.py
中显示您的
配置。模板添加到问题中。请注意,行为是,在Django 2.1中,当呈现
{%include%}
标记时会引发异常。在此之前,您可以编写一个测试来检查include标记中的预期内容(您不需要检查每一件小事,include标记中的一项就足够了),或者在异常被吞噬时尝试检测弃用警告。
python==3.6.1
Django==1.11
pytest-django==3.1.2