【问题标题】:Running Unit testing in Django在 Django 中运行单元测试
【发布时间】:2021-10-11 05:45:12
【问题描述】:

我正在 Django 中运行单元测试,但一直出错

File "C:\****\views.py", line 21, in post 
 s = str(points['string_points']) 
KeyError: 'string_points'

使用的命令:

python manage.py test

我的测试代码结构如下:

class TestSetUp(APITestCase):
    
    def setUp(self):
        self.getClosestDistance_url = reverse('getClosestDistance')
        self.calculateClosestDistance_url = reverse('calcClosestDistance')
        
        points = {
            "string_points": "(2,3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)"
        }
        return super().setUp()
    
    def tearDown(self):
        return super().tearDown()

class TestViews(TestSetUp):

    def test_getClosestPoints_with_no_data(self):
        res = self.client.post(self.calculateClosestDistance_url)
        import pdb # python debugger
        pdb.set_trace()
        self.assertEqual(res.status_code, 400)

我的 views.py 文件的结构如下:

class closestDistanceValue(APIView):

    def get(self, request): 
        points = Points.objects.all()
        serializer = PointsSerializer(points, many=True)  

        return Response(serializer.data)

    def post(self, request, format=None):
        points=request.data # user input
        s = str(points['string_points']) 
        closestPoint = getClosestDistance(s) 
        data = {
            'point': s,
            'closestPoint': closestPoint
        } 
        
        serializer = PointsSerializer(data=data) 
        if serializer.is_valid(): 
            serializer.save() 
            return Response(serializer.data, status=status.HTTP_200_OK) 
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 
POST 请求的

JSON 正文。作为用户输入键入

{
    "string_points": "(2,3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)"
}

我错过了什么?

【问题讨论】:

  • 您是否意识到setUp 中的points 变量未使用?尝试在您的方法中打印request.data(又名points)。您需要将其作为self.client.post 的第二个参数发送。

标签: python django unit-testing


【解决方案1】:

您需要随请求一起发送数据。您可以将数据设置为测试的属性,也可以将其与请求一起传递。

class TestSetUp(APITestCase):
    
    def setUp(self):
        self.getClosestDistance_url = reverse('getClosestDistance')
        self.calculateClosestDistance_url = reverse('calcClosestDistance')
        
        self.points = {
            "string_points": "(2,3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)"
        }
        return super().setUp()
    
    def tearDown(self):
        return super().tearDown()

class TestViews(TestSetUp):

    def test_getClosestPoints_with_no_data(self):
        res = self.client.post(self.calculateClosestDistance_url, self.points)
        self.assertEqual(res.status_code, 400)

或与测试本身;

    def test_getClosestPoints_with_no_data(self):
        data = {
            "string_points": "(2,3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)"
        }
        res = self.client.post(self.calculateClosestDistance_url, data)
        self.assertEqual(res.status_code, 400)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多