python unittest實現(xiàn)api自動化測試
項目測試對于一個項目的重要性,大家應(yīng)該都知道吧,寫python的朋友,應(yīng)該都寫過自動化測試腳本。
最近正好負責(zé)公司項目中的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}
ps:對于request的取值,我這里定義了常用的三種方法:
post方法:request.get_json(),在調(diào)用API時,傳值是json方式
get和put方法:request.args 或者reqparse.RequestParser(),調(diào)用API時,傳的是字符串
其次,定義Blueprint(藍圖)文件 init.py
#!/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')
然后,編寫測試腳本testPostM.py
#!/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)
ps:調(diào)用的api url 主要用的是flask_restful 的api.url_for,或者是flask的url_for,下面我來說下這2種方法的具體使用
flask_restful 的api.url_for說明
api.url_for(TestPost,PostData='adb'),這里的TestPost指的是restful api接口文件中定義的class,因為我們在api藍圖中,已經(jīng)通過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
小總結(jié):url_for的傳值和request中的取值是有對應(yīng)關(guān)系的,最后就是flask_restful中端點的方式,一定要是api.add_resource中類名的小寫。
領(lǐng)取干貨:零基礎(chǔ)入門學(xué)習(xí)python視頻教程
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡單了解Django項目應(yīng)用創(chuàng)建過程
這篇文章主要介紹了簡單了解Django項目應(yīng)用創(chuàng)建過程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
python中DataFrame常用的描述性統(tǒng)計分析方法詳解
這篇文章主要介紹了python中DataFrame常用的描述性統(tǒng)計分析方法詳解,描述性統(tǒng)計分析是通過圖表或數(shù)學(xué)方法,對數(shù)據(jù)資料進行整理、分析,并對數(shù)據(jù)的分布狀態(tài)、數(shù)字特征和隨機變量之間的關(guān)系進行估計和描述的方法,需要的朋友可以參考下2023-07-07

