[英]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)
我的 api 為該請求返回空的 json 數組。 我不知道的是,我如何運行此測試?
當我使用這個命令時:
python manage.py test
我明白了
Ran 0 tests in 0.000s
作為輸出。 它沒有寫在如何運行測試的文檔中。
我相信您的測試方法需要從test
開始。 更改def getList
到def testGetList
或def test_get_list
。
與其他 python 測試一樣(參見https://docs.python.org/2/library/unittest.html#basic-example ),如果方法不以test
開頭,它們將不會作為測試運行。
從 rest_framework.test 導入 APITestCase
從 rest_framework 導入狀態
class Portfolio_api_test(APITestCase): def setUp(self): 你的代碼
def test_your_function_name(self):
response = self.client.get('url')
self.assertEqual(response.status_code, status.HTTP_200_OK)
您的測試函數名稱應以 test_your_function_name 開頭
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.