KeyError:运行django测试时的'HTTP_HOST'

Zol*_*oli 8 python django django-testing django-tests

我是单元测试的新手,所以我不知道我做错了什么.我在Django1.8中使用python2.7

我跑的时候

python manage.py test myapp --keepdb
Run Code Online (Sandbox Code Playgroud)

我明白了

======================================================================
ERROR: test_view_content (myproject.news.tests.test_views.EntryTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/zoli/projects/project_dict/myproject/news/tests/test_views.py", line 27, in test_view_content
    response = client.get(reverse('news_list', kwargs={'page': 1}))
  File "/home/zoli/.virtualenvs/project_dict/local/lib/python2.7/site-packages/django/test/client.py", line 500, in get
    **extra)
  File "/home/zoli/.virtualenvs/project_dict/local/lib/python2.7/site-packages/django/test/client.py", line 303, in get
    return self.generic('GET', path, secure=secure, **r)
  File "/home/zoli/.virtualenvs/project_dict/local/lib/python2.7/site-packages/django/test/client.py", line 379, in generic
    return self.request(**r)
  File "/home/zoli/.virtualenvs/project_dict/local/lib/python2.7/site-packages/django/test/client.py", line 466, in request
    six.reraise(*exc_info)
  File "/home/zoli/.virtualenvs/project_dict/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 108, in get_response
    response = middleware_method(request)
  File "/home/zoli/projects/project_dict/myproject/middleware/multihostname.py", line 18, in process_request
    host = request.META['HTTP_HOST'].split(':')[0]
KeyError: u'HTTP_HOST'

----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我的测试看起来像

from django.test import TestCase, Client
from django.contrib.sites.models import Site
from myproject.news.models import Entry
from myproject.people.models import User
from django.core.urlresolvers import reverse


class EntryTestCase(TestCase):
    def setUp(self):
        user1 = User.objects.create(username='zoli')
        site1 = Site.objects.create(domain='mysite.sk', name='mysite')
        entry = Entry(author=user1, title='Titulok', text='Toto je obsah')
        entry.save()
        entry.sites.add(site1)
        entry.save()

    def test_view_content(self):
        client = Client()

        response = client.get(reverse('news_list', kwargs={'page': 1})) # This is raising and error
        print response.content
Run Code Online (Sandbox Code Playgroud)

当我访问/ novinky/strana/1 /一切都很好,所以我想错误是在测试中.如果您需要任何其他代码,我会将其粘贴在此处.

Ala*_*air 14

HTTP_HOST头没有被默认的Django测试客户端设置.您的多主机中间件假定标头始终存在,因此您可以KeyError在测试运行时获得.

您可能希望更改多主机名中间件,以便在标头不在请求中时不会导致错误.

if 'HTTP_HOST' in request.META:
    host = request.META['HTTP_HOST'].split(':')[0]
    ...
else:
    # do something else
Run Code Online (Sandbox Code Playgroud)

或者可能有默认主机:

host = request.META.get('HTTP_HOST', 'defaulthost.com').split(':')[0]
Run Code Online (Sandbox Code Playgroud)

如果要测试不同标头的效果,可以在发出请求时包含标头:

client = Client()
# Make a request, setting the header manually
client.get('/my_url', HTTP_HOST='example.com')
Run Code Online (Sandbox Code Playgroud)

或者,您可以设置测试客户端以在所有请求中包含标头:

client = Client(HTTP_HOST='example.com')
# The header will be set for both of the following requests
client.get('/my_url/')
client.get('/my_second_url/')
Run Code Online (Sandbox Code Playgroud)

请参阅有关提出更多信息请求的文档.