国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python unittest實現api自動化測試

2020-02-22 23:36:06
字體:
來源:轉載
供稿:網友

項目測試對于一個項目的重要性,大家應該都知道吧,寫python的朋友,應該都寫過自動化測試腳本。
最近正好負責公司項目中的api測試,下面寫了一個簡單的例子,對API 測試進行梳理。

首先,編寫restful api接口文件 testpost.py,包含了get,post,put方法

#!/usr/bin/env python# -*- coding: utf-8 -*-from flask import requestfrom flask_restful import Resourcefrom flask_restful import reqparsetest_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(),在調用API時,傳值是json方式
get和put方法:request.args 或者reqparse.RequestParser(),調用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)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 夹江县| 慈溪市| 邵武市| 龙游县| 武陟县| 莲花县| 元阳县| 扎鲁特旗| 闵行区| 灵山县| 西城区| 岳普湖县| 汉川市| 华阴市| 兴化市| 特克斯县| 郑州市| 通化市| 怀宁县| 保定市| 孝感市| 浪卡子县| 临沂市| 台东市| 河池市| 资中县| 同心县| 二连浩特市| 道真| 辉南县| 苗栗县| 曲松县| 织金县| 衡山县| 怀远县| 房山区| 许昌市| 兰州市| 富源县| 那坡县| 宜都市|