Python django rest framweork测试:get测试中请求后身份验证中断

Python django rest framweork测试:get测试中请求后身份验证中断,python,django,unit-testing,authentication,django-rest-framework,Python,Django,Unit Testing,Authentication,Django Rest Framework,我使用django rest framework测试库定义了两个测试: def test_read_site_API(self): """ Read a Site trough API """ self.client.login(email='test@mail.org', password='testing' ) response = self.client.get('/xos/sites/', format='json') parsed = j

我使用
django rest framework
测试库定义了两个测试:

def test_read_site_API(self):
    """
    Read a Site trough API
    """
    self.client.login(email='test@mail.org', password='testing' )
    response = self.client.get('/xos/sites/', format='json')
    parsed = json.loads(response.content)
    self.assertEqual(response.status_code, status.HTTP_200_OK)
    self.assertEqual(len(parsed), 1)
    self.assertEqual(parsed[0]['login_base'], 'test_')

def test_create_site_API(self):
    """
    Create a Site trough API
    """
    data = {
        'name': "Another Test Site",
        'login_base': "another_test_",
        'location': [10, 20],
        'abbreviated_name': 'test'
    }
    self.client.login(email='test@mail.org', password='testing' )
    response = self.client.post('/xos/sites/', data, format='json')
    print(response.content)
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)
    self.assertEqual(Site.objects.count(), 2)
    self.assertEqual(Site.objects.get(name="Another Test Site").count(), 1)
如果我只运行第一个测试,它就会工作。 如果我运行两个测试,结果是:

======================================================================
ERROR: test_read_site_API (core.tests.SiteTestAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/xos/core/tests.py", line 75, in test_read_site_API
    self.client.login(email='test@mail.org', password='testing' )
  File "/usr/local/lib/python2.7/site-packages/django/test/client.py", line 563, in login
    login(request, user)
  File "/usr/local/lib/python2.7/site-    packages/django/contrib/auth/__init__.py", line 102, in login
    user_logged_in.send(sender=user.__class__, request=request, user=user)
  File "/usr/local/lib/python2.7/site-    packages/django/dispatch/dispatcher.py", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/opt/xos/core/admin.py", line 1908, in cache_credentials
    auth = {'username': request.POST['username'],
KeyError: 'username'
你知道会发生什么吗


提前感谢

这并没有回答您的问题,而是每次都要调用
self.client.login
,我相信您应该能够将登录的用户传递给
post
,或者在测试中覆盖DRF设置。
self.client.login
也可以在
设置
方法中调用,这没什么大不了的。“覆盖DRF设置”是什么意思?在测试中,您可以简单地跳过身份验证。从内存DRF有一个用于所用身份验证的设置(全部允许、HTTP密码、Oauth等)。因此,在测试中,您可以覆盖该设置。