Python Django REST API测试
作为我在python中创建API测试的第一次尝试,我的测试文件目前如下所示:Python Django REST API测试,python,django,testing,django-rest-framework,Python,Django,Testing,Django Rest Framework,作为我在python中创建API测试的第一次尝试,我的测试文件目前如下所示: from django.test import TestCase, RequestFactory from customer.models import Customer, CustomerStatus from customer.views import CustomerPartialView from rest_framework.test import APIRequestFactory import ppr
from django.test import TestCase, RequestFactory
from customer.models import Customer, CustomerStatus
from customer.views import CustomerPartialView
from rest_framework.test import APIRequestFactory
import pprint
from django.utils import timezone
class CustomerTest(TestCase) :
def setUp(self) :
self.factory = RequestFactory()
self.cust_status = CustomerStatus.objects.create(
status_id=1,name="banned",description="test desc" )
self.customer = Customer.objects.create(
guid='b27a3251-56e0-4870-8a03-27b0e92af9e5',
created_date=timezone.now(),
status_id=1,first_name='Hamster')
def test_get_customer(self) :
"""
Ensure we can get a customer from db through the API
"""
factory = APIRequestFactory()
request = factory.get('/customer/b27a3251-56e0-4870-8a03-27b0e92af9e5')
reponse = CustomerPartialView(request)
#in here I need something to check:
#response.data.first_name = "Hamster"
from customer.models import Customer, CustomerStatus
from customer.views import CustomerPartialView
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
import pprint
from django.utils import timezone
class CustomerTest(APITestCase) :
def setUp(self) :
self.cust_status = CustomerStatus.objects.create(
status_id=1,name="banned",description="test desc" )
self.customer = Customer.objects.create(
guid='b27a3251-56e0-4870-8a03-27b0e92af9e5',
created_date=timezone.now(),
status_id=1,first_name='Hamster')
def test_get_customer(self) :
"""
Ensure we can get a customer from db through the API
"""
response = self.client.get('/customer/%s/' % self.customer.guid)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
(response.data['first_name'],response.data['guid']),
('Hamster','b27a3251-56e0-4870-8a03-27b0e92af9e5'))
我想要实现的是将虚拟数据插入数据库,然后使用APIRequestFactory检索单个客户记录,并确保他们的名字与我期望的相符
我已经走了这么远,但不知道下一步该怎么办
我的问题是:
response.data.first\u name=仓鼠?
3) 总有其他的方法,问题是它们是否更好…有一些工具可以让你的测试更容易,比如or(还有更多)。因此,可能有更好的方法…如果我理解正确,我认为您的问题可能是您使用的是
response.data.first\u name
而不是response.data['first\u name']
,因为response.data是一个命令
所以应该是这样的:
self.assertEqual(response.data['first_name'], 'Hamster')
或者,我的建议是:
self.assertEqual(response.data['first_name'], self.customer.first_name)
我还认为将请求url更改为'/customer/%s'%self.customer.guid
保存您正在写入的字符串。感谢各位的输入 我发现我应该使用
APITestCase
而不是TestCase
作为DRF
我已经在我的测试脚本中实现了你的两个答案,我得到了一个工作测试脚本,如下所示:
from django.test import TestCase, RequestFactory
from customer.models import Customer, CustomerStatus
from customer.views import CustomerPartialView
from rest_framework.test import APIRequestFactory
import pprint
from django.utils import timezone
class CustomerTest(TestCase) :
def setUp(self) :
self.factory = RequestFactory()
self.cust_status = CustomerStatus.objects.create(
status_id=1,name="banned",description="test desc" )
self.customer = Customer.objects.create(
guid='b27a3251-56e0-4870-8a03-27b0e92af9e5',
created_date=timezone.now(),
status_id=1,first_name='Hamster')
def test_get_customer(self) :
"""
Ensure we can get a customer from db through the API
"""
factory = APIRequestFactory()
request = factory.get('/customer/b27a3251-56e0-4870-8a03-27b0e92af9e5')
reponse = CustomerPartialView(request)
#in here I need something to check:
#response.data.first_name = "Hamster"
from customer.models import Customer, CustomerStatus
from customer.views import CustomerPartialView
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
import pprint
from django.utils import timezone
class CustomerTest(APITestCase) :
def setUp(self) :
self.cust_status = CustomerStatus.objects.create(
status_id=1,name="banned",description="test desc" )
self.customer = Customer.objects.create(
guid='b27a3251-56e0-4870-8a03-27b0e92af9e5',
created_date=timezone.now(),
status_id=1,first_name='Hamster')
def test_get_customer(self) :
"""
Ensure we can get a customer from db through the API
"""
response = self.client.get('/customer/%s/' % self.customer.guid)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
(response.data['first_name'],response.data['guid']),
('Hamster','b27a3251-56e0-4870-8a03-27b0e92af9e5'))
您是否遇到了具体的错误或异常?