新聞中心
這篇文章主要為大家展示了“python中unittest如何實(shí)現(xiàn)api自動(dòng)化測(cè)試”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“python中unittest如何實(shí)現(xiàn)api自動(dòng)化測(cè)試”這篇文章吧。

首先,編寫(xiě)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:對(duì)于request的取值,我這里定義了常用的三種方法:
post方法:request.get_json(),在調(diào)用API時(shí),傳值是json方式
get和put方法:request.args 或者reqparse.RequestParser(),調(diào)用API時(shí),傳的是字符串
其次,定義Blueprint(藍(lán)圖)文件 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, '//postMeth') 然后,編寫(xiě)測(cè)試腳本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,下面我來(lái)說(shuō)下這2種方法的具體使用
flask_restful 的api.url_for說(shuō)明
api.url_for(TestPost,PostData='adb'),這里的TestPost指的是restful api接口文件中定義的class,因?yàn)槲覀冊(cè)赼pi藍(lán)圖中,已經(jīng)通過(guò)api.add_resource(TestPost, ‘//postMeth')添加類(lèi)的方式定義過(guò)
flask的url_for的使用說(shuō)明
url_for(‘testPostb.testpost', PostData='adb', ddos='123'),'testPostb.testpost'這個(gè)字符串中
testPostb指的是藍(lán)圖的名稱(chēng),也就是testPostb = Blueprint(‘testPostb', name)中Blueprint(‘testPostb',name)中的testPostb。
testpost指的是藍(lán)圖下endpoit的端點(diǎn)名稱(chēng),flask_restful中,指的是api.add_resource(TestPost, ‘//postMeth')中 類(lèi)名TestPost的小寫(xiě)
啟動(dòng)測(cè)試腳本:
C:\secauto3>python run.py test test_post (testPostM.APITestCase) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.056s OK
以上是“python中unittest如何實(shí)現(xiàn)api自動(dòng)化測(cè)試”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前文章:python中unittest如何實(shí)現(xiàn)api自動(dòng)化測(cè)試-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://fisionsoft.com.cn/article/dshioj.html


咨詢(xún)
建站咨詢(xún)
