我已經(jīng)看過關(guān)于 SO 的兩個答案,它們可能已經(jīng)有了我的答案,但坦率地說,我只是不理解它們兩個 SO 問題都使用不同的方法。我試圖使用的方法是這樣的。我創(chuàng)建了這個類:from rest_framework.test import APIClientfrom django.test import testcasesfrom django.contrib.auth.models import Userclass RemoteAuthenticatedTest(testcases.TestCase): client_class = APIClient def setUp(self): self.username = 'mister_neutron' self.user = User.objects.create_user(username='mister_neutron', email='mister_neutron@example.com', password='F4kePaSs0d') super(RemoteAuthenticatedTest, self).setUp()我的單元測試如下所示:class InfoViewTestCase(RemoteAuthenticatedTest): def create_info_record(self): from random import randint blade = 'test-blade-host-name-%s' % (randint(0, 100)) breachs = randint(0,100) dimm = 'test dimm slot %s' % (randint(0,100)) url = reverse('info:info_creat') data = { 'blade_hostname': blade, 'breach_count': breachs, 'dimm_slot': dimm, } response = self.client.post(url, data, format='json', REMOTE_USER=self.username) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(Info.objects.count(), 1) self.assertEqual(Info.objects.get().blade_hostname, blade)我的 settings.py 文件中有這個:#AuthenticationsREST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.BasicAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.TokenAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ),}
添加回答
舉報
0/150
提交
取消