2015-07-01 237 views
0

我只是想測試Flask REST服務的CRUD操作。單元測試REST API Python

  1. CREATE操作中,我想創建一個新的對象。
  2. UPDATE操作中,我需要獲取創建操作中創建的對象的id並更新它。
  3. DELETE操作中,我需要刪除創建操作中創建的對象。

我該如何解決這個問題?

當前的代碼看起來是這樣的

class BaseTestCase(TestCase): 
    def create_app(self): 
     return create_app('testing') 

    def setUp(self): 
     self.response = self.client.get('/api/v1/list_portfolios').json 
     self.portfolio_id = self.response['respbody']['Portfolios'][0]['_id'] 
     self.view_response = self.client.get('/api/v1/view_portfolio/' + self.portfolio_id).json 

class ModelTestAPI(BaseTestCase): 
    def test_model_create(self): 
     model_create_data = { 
      "PortfolioId": "558d0e575dddb726b8cd06bc", 
      "ModelName": "New Model", 
      "ModelLifetimePeriod": "Month", 
      "ModelLifetimeDuration": 12 
     } 
     response = self.client.post('/api/v1/model/create', data=dumps(model_create_data), 
            content_type='application/json').json 
     portfolio_model_id = response['respbody']['_id'] 
     print(portfolio_model_id) 

     new_model_dict = model_create_data.copy() 
     new_model_dict['_id'] = portfolio_model_id 

     new_json = response['respbody'].copy() 
     new_json.pop('CreateDate', None) 
     new_json.pop('LastUpdateDate', None) 

     self.assertDictEqual(new_model_dict, new_json) 

    def test_model_update(self): 
     data = { 
      "ModelName": "UPDATE New Model", 
      "ModelLifetimePeriod": "Month", 
      "ModelLifetimeDuration": 6 
     } 
     portfolio_model_id = self.view_response['respbody']['PortfolioModels'][-1]['_id'] 
     json = self.client.put('/api/v1/model/' + portfolio_model_id, data=dumps(data), 
           content_type='application/json').json 

     data['_id'] = portfolio_model_id 
     new_json = json['respbody'].copy() 
     new_json.pop('CreateDate', None) 
     new_json.pop('LastUpdateDate', None) 
     new_json.pop('PortfolioId', None) 

     self.assertDictEqual(data, new_json) 

    def test_model_delete(self): 
     portfolio_model_id = self.view_response['respbody']['PortfolioModels'][-1]['_id'] 
     json = self.client.delete('http://localhost:5000/api/v1/model/' + portfolio_model_id).json 
     expected_dict = {'success': True} 
     self.assertDictEqual(expected_dict, json['respbody']) 
+0

你正在使用什麼框架?,以及當前的代碼沒有做,它應該做什麼? –

回答

1

根據我的經驗,你首先建立一個Web服務,第二次開始寫單元測試

Web服務的例子:

from rest_framework.views import APIView 
class view_example(APIView): 
    def get(self, request, format=None): 
     ... 
    def post(self, request, format=None): 
     ... 
    def put(self, request, format=None): 
     ... 
    def delete(self, request, format=None): 
     ... 

如果您確定在urls.py中註冊了視圖,那麼編寫unittest。

from rest_framework.test import APITestCase 

class ViewExampleTests(APITestCase): 
    def test_create(self): 
     ... 
     response = self.client.post(url, data, format='json') 
     ... 
    def test_update(self): 
     ... 
     response = self.client.put(url, data, format='json') 
     ... 
    def test_delete(self): 
     ... 
     response = self.client.delete(url, data, format='json') 
     ... 
    def test_read(self): 
     ... 
     response = self.client.get(url, data, format='json') 
     ... 

但是,這是大幅完成的作品。

+0

也就是說,通過vanilla Django testcast添加了一個'APITestCase'的功能,它需要一個字典並將其編碼爲您設置爲'settings.TEST_REQUEST_DEFAULT_FORMAT'的任何內容。 – spectras