python unittest實(shí)現(xiàn)api自動(dòng)化測(cè)試
項(xiàng)目測(cè)試對(duì)于一個(gè)項(xiàng)目的重要性,大家應(yīng)該都知道吧,寫(xiě)python的朋友,應(yīng)該都寫(xiě)過(guò)自動(dòng)化測(cè)試腳本。
最近正好負(fù)責(zé)公司項(xiàng)目中的api測(cè)試,下面寫(xiě)了一個(gè)簡(jiǎn)單的例子,對(duì)API 測(cè)試進(jìn)行梳理。
首先,編寫(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, '/<string:PostData>/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)圖的名稱,也就是testPostb = Blueprint(‘testPostb', name)中Blueprint(‘testPostb',name)中的testPostb。
testpost指的是藍(lán)圖下endpoit的端點(diǎn)名稱,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
小總結(jié):url_for的傳值和request中的取值是有對(duì)應(yīng)關(guān)系的,最后就是flask_restful中端點(diǎn)的方式,一定要是api.add_resource中類(lèi)名的小寫(xiě)。
領(lǐng)取干貨:零基礎(chǔ)入門(mén)學(xué)習(xí)python視頻教程
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python中Unittest框架的具體使用
- Python Unittest根據(jù)不同測(cè)試環(huán)境跳過(guò)用例的方法
- Python unittest 簡(jiǎn)單實(shí)現(xiàn)參數(shù)化的方法
- python+unittest+requests實(shí)現(xiàn)接口自動(dòng)化的方法
- Python 單元測(cè)試(unittest)的使用小結(jié)
- Python unittest單元測(cè)試框架總結(jié)
- Python Unittest自動(dòng)化單元測(cè)試框架詳解
- Python unittest基本使用方法代碼實(shí)例
相關(guān)文章
使用python腳本檢查ssl證書(shū)到期時(shí)間
這篇文章主要為大家介紹了使用python腳本檢查ssl證書(shū)到期時(shí)間,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01django定期執(zhí)行任務(wù)(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇django定期執(zhí)行任務(wù)(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Python進(jìn)階之多線程的實(shí)現(xiàn)方法總結(jié)
在python中主要有兩種實(shí)現(xiàn)多線程的方式:通過(guò)threading.Thread?()?方法創(chuàng)建線程和通過(guò)繼承?threading.Thread?類(lèi)的繼承重寫(xiě)run方法,接下來(lái)我們分別說(shuō)一下多線程的兩種實(shí)現(xiàn)形式吧2023-04-04python tkinter之頂層菜單、彈出菜單實(shí)例
這篇文章主要介紹了python tkinter之頂層菜單、彈出菜單實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03基于Django的Admin后臺(tái)實(shí)現(xiàn)定制簡(jiǎn)單監(jiān)控頁(yè)
Django自帶的后臺(tái)管理是Django明顯特色之一,可以讓我們快速便捷管理數(shù)據(jù)。后臺(tái)管理可以在各個(gè)app的admin.py文件中進(jìn)行控制。本文將主要介紹如何利用Admin后臺(tái)實(shí)現(xiàn)監(jiān)控頁(yè)的定制,快來(lái)和小編一起學(xué)習(xí)一下吧2021-12-12如何利用Python寫(xiě)個(gè)坦克大戰(zhàn)
這篇文章主要給大家介紹了關(guān)于如何利用Python寫(xiě)個(gè)坦克大戰(zhàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11教你使用Python?的?Template?類(lèi)生成文件報(bào)告
這篇文章主要介紹了用?Python?的?Template?類(lèi)生成文件報(bào)告,在閱讀本文時(shí),您不僅學(xué)習(xí)了Python字符串的基本知識(shí),Template類(lèi)以及使用它的原因,而且還實(shí)現(xiàn)了第一個(gè)文件報(bào)告腳本,需要的朋友可以參考下2022-08-08