4

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

我如何编写一个可以在包含的模板中发现问题的测试?

导航.html

{% url 'project:wrong_name' %}

索引.html

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

视图.py

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

测试.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(缩写):

python==3.6.1
Django==1.11
pytest-django==3.1.2
4

0 回答 0