Python Django REST API 测试

Python Django REST API test

作为我在 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 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"

我想实现的是将虚拟数据插入数据库,然后使用 APIRequestFactory 检索单个客户记录并确保他们的名字与我期望的相符。

我已经做到了这一点,但不确定下一步该做什么。

我的问题是:

  1. 我走在正确的轨道上吗?
  2. 如何测试我的结果,即 response.data.first_name = hamster ?
  3. 有没有更好的方法来实现我想要实现的目标

我是 python 的新手,所以如果我的代码中有任何重大错误,我提前道歉。

谢谢

1) 基本测试,作为一个开始看起来不错。

2) 检查 https://docs.djangoproject.com/en/1.8/topics/testing/overview/ 以进行基本的 Django 测试。在这里,您将继续正常的 self.assertWhatever(..) 过程。

3) 总有其他方法,问题是它们是否更好……有一些工具可以使您的测试更容易,例如 mock or mixer(以及更多)。所以,可能有更好的方法...

如果我理解正确的话,我认为你的问题可能是你使用的是 response.data.first_name 而不是 response.data['first_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 会更整洁一些 节省您重新编写字符串的时间。

感谢大家的意见。

我发现我应该为 DRF 使用 APITestCase 而不是 TestCase

我已经在我的测试脚本中实现了你的两个答案,并且我有一个看起来像这样的工作测试脚本:

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'))