Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django REST API测试_Python_Django_Testing_Django Rest Framework - Fatal编程技术网

Python Django REST 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

作为我在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"
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=仓鼠?
  • 有没有更好的方法来实现我的目标
  • 我是python新手,所以如果我的代码中有任何重大错误,我会提前道歉

    谢谢基本测试,看起来是个不错的开始

    2) 检查基本django测试。在这里,您将继续执行正常的self.assertWhatever(..)过程


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

    您是否遇到了具体的错误或异常?