這篇文章主要為大家詳細介紹了python unittest實現api自動化測試的方法,具有一定的參考價值,有興趣的小夥伴們可以參考一下
專案測試對於一個專案的重要性,大家應該都知道吧,寫python的朋友,應該都寫過自動化測試腳本。
最近剛好負責公司專案中的api測試,下面寫了一個簡單的例子,對API 測試進行梳理。
首先,寫restful api介面檔testpost.py,包含了get,post,put方法
##
#!/usr/bin/env python # -*- coding: utf-8 -*- from flask import request from flask_restful import Resource from flask_restful import reqparse test_praser = reqparse.RequestParser() test_praser.add_argument('ddos') class TestPost(Resource): def post(self, PostData): data = request.get_json() user = User('wangjing') if data['ddos']: return {'hello': 'uese', "PostData": PostData, 'ddos': 'data[\'ddos\']'} return {'hello': 'uese', "PostData": PostData} def get(self, PostData): data = request.args if data and data['ddos']: return "hello" + PostData + data['ddos'], 200 return {'hello': 'uese', "PostData": PostData} def put(self, PostData): data = test_praser.parse_args() if data and data['ddos']: return "hello" + PostData + data['ddos'], 200 return {'hello': 'uese', "PostData": PostData}
post方法:request.get_json(),在呼叫API時,傳值是json方式#其次,定義Blueprint(藍圖)檔案init.pyget和put方法:request.args 或reqparse.RequestParser(),呼叫API時,傳的是字串
#
#!/usr/bin/env python # -*- coding: utf-8 -*- from flask import Blueprint from flask_restful import Api from testpost import TestPost testPostb = Blueprint('testPostb', __name__) api = Api(testPostb) api.add_resource(TestPost, '/<string:PostData>/postMeth')
#
#!/usr/bin/env python # -*- coding: utf-8 -*- import unittest import json from secautoApp.api.testPostMeth import api from flask import url_for from run import app from secautoApp.api.testPostMeth import TestPost headers = {'Accept': 'application/json', 'Content-Type': 'application/json' } class APITestCase(unittest.TestCase): def setUp(self): # self.app = create_app(os.getenv("SECAUTOCFG") or 'default') self.app = app # self.app_context = self.app.app_context() # self.app_context.push() self.client = self.app.test_client() # # def tearDown(self): # self.app_context.pop() def test_post(self): # with app.test_request_context(): response = self.client.get(api.url_for(TestPost, PostData='adb', ddos='123')) self.assertTrue(response.status_code == 200) response = self.client.get(url_for('testPostb.testpost', PostData='adb', ddos='123')) self.assertTrue(response.status_code == 200) self.assertTrue(json.loads(response.data)['PostData'] =='adb') response = self.client.post(url_for('testPostb.testpost', PostData='adb'), headers=headers, data=json.dumps({"ddos": '123'})) print json.loads(response.data) self.assertTrue(response.status_code == 200) response = self.client.put(url_for('testPostb.testpost', PostData='adb', ddos='123')) self.assertTrue(json.loads(response.data) == 'helloadb123') response = self.client.put(url_for('testPostb.testpost', PostData='adb')) print json.loads(response.data)['PostData'] self.assertTrue(response.status_code == 200)
flask_restful 的api.url_for說明
api.url_for(TestPost,PostData='adb'),這裡的TestPost指的是restful api介面檔中定義的class,因為我們在api藍圖中,已經透過api .add_resource(TestPost, '//postMeth')加入類別的方式定義過
#flask的url_for的使用說明
#url_for('testPostb.testpost', PostData='adb', ddos='123'),'testPostb.testpost'這個字串中
testPostb指的是藍圖的名稱,也就是testPostb = Blueprint( 'testPostb', name)中Blueprint('testPostb',name)中的testPostb。testpost指的是藍圖下endpoit的端點名稱,flask_restful中,指的是api.add_resource(TestPost, '//postMeth')中類別名稱TestPost的小寫
C:\secauto3>python run.py test test_post (testPostM.APITestCase) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.056s OK
url_for的傳值和request中的取值是有對應關係的,最後就是flask_restful中端點的方式,一定要是api.add_resource中類別名稱的小寫。 相關推薦:
######python如何使用unittest測試介面_python############python+requests+unittest API介面測試問題### ############################以上是python unittest實現api自動化測試_python的詳細內容。更多資訊請關注PHP中文網其他相關文章!