Django测试 - ' HTTP_USER_AGENT'测试客户端的关键字参数无效

时间:2014-05-15 17:29:58

标签: python django unit-testing

我的Django单元测试已停止工作。现在,实例化Django测试客户端失败,出现以下错误:

Traceback (most recent call last):
  File "/vagrant/my/app/tests.py", line 43, in setUp
    self.client = Client(HTTP_USER_AGENT='Mozilla/5.0')
  File "/usr/local/lib/python2.6/dist-packages/Django-1.4.1-py2.6.egg/django/db/models/base.py", line 367, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
TypeError: 'HTTP_USER_AGENT' is an invalid keyword argument for this function

当我实例化Django测试客户端时,它们会失败。

from django.test.client import Client
...

class MyAppTestCase(TestCase):
    base_fixtures = ['fixtures.json']

    def setUp(self):
        self.client = Client(HTTP_USER_AGENT='Mozilla/5.0') # fails here
        self.setupSession()
        self.authenticateUser()

当我运行python manage.py shell并输入以下内容时,它运行正常。

vagrant@lucid32:/var/www/mytraps.com/spensa$ python manage.py shell
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.test.client import Client
>>> client = Client(HTTP_USER_AGENT='Mozilla/5.0')
>>> 

有关为什么它会阻塞HTTP_USER_AGENT关键字的任何想法?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。

我有一个名为'Client'的模型类。我的模型是在django测试Client类之后导入的。

你无法修复愚蠢。