测试Django模板正确呈现

时间:2017-06-09 19:56:23

标签: 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: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

settings.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

0 个答案:

没有答案