最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
python中unittest如何實現(xiàn)api自動化測試-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供東蘭網(wǎng)站建設(shè)、東蘭做網(wǎng)站、東蘭網(wǎng)站設(shè)計、東蘭網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、東蘭企業(yè)網(wǎng)站模板建站服務(wù),10年東蘭做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

首先,編寫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, '//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

以上是“python中unittest如何實現(xiàn)api自動化測試”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


文章名稱:python中unittest如何實現(xiàn)api自動化測試-創(chuàng)新互聯(lián)
當前網(wǎng)址:http://fisionsoft.com.cn/article/dshioj.html