如何 运行 测试 django rest 框架测试?

How to run tests django rest framework tests?

我正在学习 django 休息框架。我写了一个像这样的简单测试:

from rest_framework import status
from rest_framework.test import APITestCase


class ClinicTestCase(APITestCase):
    def getList(self):
        factory = APIRequestFactory()
        request = factory.get('/Clinic/')
        self.assertEqual(response.status_code, status.OK)

my api returns 该请求的空 json 数组。我不知道的是,我如何 运行 这个测试?

当我使用这个命令时:

python manage.py test

我得到

Ran 0 tests in 0.000s

作为输出。它没有写在如何 运行 测试的文档中。

我认为您的测试方法需要以 test 开头。将 def getList 更改为 def testGetListdef test_get_list

与其他 python 测试(参见 https://docs.python.org/2/library/unittest.html#basic-example)一样,如果方法不以 test 开头,它们将不会 运行 作为测试。

从 rest_framework.test 导入 APITestCase

来自 rest_framework 导入状态

classPortfolio_api_test(APITestCase): 定义设置(自我): 您的代码

def test_your_function_name(self):
    response = self.client.get('url')
    self.assertEqual(response.status_code, status.HTTP_200_OK)

您的测试函数名称应以 test_your_function_name

开头