一俐筋、 mock
1. 什么是mock静秆?
①測試樁:模擬被測對象的返回粮揉,用于測試
②mock:通常指mock server ,模擬服務(wù)返回的接口數(shù)據(jù)抚笔,用于前端開發(fā)扶认、第三方接口聯(lián)調(diào)想·
2. 為什么要學(xué)習(xí)mock?
①前后端開發(fā)進(jìn)度不一致殊橙,前端開發(fā)快于后端開發(fā)辐宾,前端需要一個假的接口用于模擬后端數(shù)據(jù)返回。
②項目用到第三方接口膨蛮,三方接口沒有開發(fā)好或者沒有開發(fā)環(huán)境
3. 如何使用mock叠纹?
①利用抓包工具fiddler,但是不方便而且也不專業(yè)
②用web框架模擬:Django敞葛、flask(結(jié)構(gòu)簡單誉察,容易入門)
4. flask使用
①安裝flask:pip install flask
②使用flask
- 案例一:最簡單的get請求
# 服務(wù)端
# 導(dǎo)包
from flask import Flask
# 實例化一個web服務(wù)對象,app 這個名字是預(yù)定俗成的
app = Flask(__name__)
# 定義一個路由 -- 訪問服務(wù)器的根目錄就能得到結(jié)果
@app.route('/')
# 創(chuàng)建一個方法來處理請求
def hello():
data = {
"error_code": 0,
"reason": "success",
"result": {
"data": {
"conclusion": "獨營生意,和氣吉祥惹谐,排除萬難持偏,必獲成功",
"analysis": "主人性格類型:[大膽行事沖動派型],其具體表現(xiàn)為:愛好追尋刺激氨肌,有不理后果大膽行事的傾向鸿秆。崇尚自由奔放的戀愛,會拼盡全力愛一場怎囚,是就算明知無結(jié)果都在所不惜的沖動派卿叽。"
}
}
}
return data
# 入口
if __name__ == '__main__':
app.run('127.0.0.1', '9090')
# 客戶端
import requests
res = requests.get("http://127.0.0.1:9090/")
print(res.text)
- 案例二:構(gòu)建post請求的響應(yīng)
# 服務(wù)端
# 導(dǎo)包
from flask import Flask,request
# 實例化一個web服務(wù)對象,app 這個名字是預(yù)定俗成的
app = Flask(__name__)
# 構(gòu)造一個接受post請求的響應(yīng)
@app.route('/post',methods = ["POST"])
def test_post():
# 使用request處理請求發(fā)送過來的參數(shù)
id = request.form["id"]
name = request.form["name"]
return "接受到請求發(fā)送過來的數(shù)據(jù):"+name+id
# 入口
if __name__ == '__main__':
app.run('127.0.0.1', '9090')
# 客戶端
import requests
body = {
"id": "007",
"name": "anfly"
}
res = requests.post("http://127.0.0.1:9090/post", data=body)
print(res.text)
- 案例三:復(fù)雜的post
# 服務(wù)端
# 導(dǎo)包
from flask import Flask, request, json
# 實例化一個web服務(wù)對象,app 這個名字是預(yù)定俗成的
app = Flask(__name__)
# 處理稍微復(fù)雜的post請求
@app.route("/post/food", methods=["POST"])
def post_food():
# 拿到客戶端返回的數(shù)據(jù)
res = json.loads(request.get_data())
limit = res["limit"]
id = res["stage_id"]
page = res["page"]
body = {
"ret": 1,
"data": [
{
"id": "8289",
"title": "油燜大蝦",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg",
"collect_num": "1670",
"food_str": "大蝦 蔥 生姜 植物油 料酒",
"num": 1670
},
{
"id": "2127",
"title": "四川回鍋肉",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg",
"collect_num": "1591",
"food_str": "豬肉 青蒜 青椒 紅椒 姜片",
"num": 1591
},
{
"id": "30630",
"title": "超簡單芒果布丁",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg",
"collect_num": "1552",
"food_str": "QQ糖 牛奶 芒果",
"num": 1552
},
{
"id": "9073",
"title": "家常紅燒魚",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg",
"collect_num": "1426",
"food_str": "鮮魚 姜 蔥 蒜 花椒",
"num": 1426
},
{
"id": "10097",
"title": "家常煎豆腐",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg",
"collect_num": "1420",
"food_str": "豆腐 新鮮紅椒 青椒 蔥花 油",
"num": 1420
},
{
"id": "10509",
"title": "水煮肉片",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg",
"collect_num": "1342",
"food_str": "瘦豬肉 生菜 豆瓣醬 干辣椒 花椒",
"num": 1342
},
{
"id": "46968",
"title": "紅糖蘋果銀耳湯",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg",
"collect_num": "1253",
"food_str": "銀耳 蘋果 紅糖",
"num": 1253
},
{
"id": "10191",
"title": "麻婆豆腐",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg",
"collect_num": "1224",
"food_str": "豆腐 肉末 生抽 白糖 芝麻油",
"num": 1224
},
{
"id": "2372",
"title": "皮蛋瘦肉粥",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg",
"collect_num": "1151",
"food_str": "大米 皮蛋 豬肉 油條 香蔥",
"num": 1151
},
{
"id": "2166",
"title": "螞蟻上樹",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg",
"collect_num": "1145",
"food_str": "紅薯粉 肉 姜 蒜 花椒",
"num": 1145
},
{
"id": "2262",
"title": "糖醋肉",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg",
"collect_num": "1082",
"food_str": "豬肉 紅椒 黃椒 洋蔥 蛋清",
"num": 1082
},
{
"id": "9971",
"title": "魚香豆腐",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg",
"collect_num": "1010",
"food_str": "豆腐 木耳 胡蘿卜 香蔥 番茄醬",
"num": 1010
},
{
"id": "10172",
"title": "干煸四季豆",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg",
"collect_num": "993",
"food_str": "四季豆 干辣椒 蒜頭 醬油 糖",
"num": 993
},
{
"id": "2685",
"title": "胡蘿卜肉末蒸蛋",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg",
"collect_num": "929",
"food_str": "胡蘿卜 肉 蛋 生抽 鹽",
"num": 929
},
{
"id": "9972",
"title": "虎皮青椒",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg",
"collect_num": "892",
"food_str": "青辣椒 大蒜 香醋 白糖 生抽",
"num": 892
},
{
"id": "10437",
"title": "叉燒排骨",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg",
"collect_num": "806",
"food_str": "排骨 李錦記叉燒醬 植物油 清水 油菜",
"num": 806
},
{
"id": "2892",
"title": "“五行”彩蔬湯",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg",
"collect_num": "761",
"food_str": "黑木耳 玉米 牛蒡 胡蘿卜 西蘭花",
"num": 761
},
{
"id": "2348",
"title": "麻辣肉絲面",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg",
"collect_num": "760",
"food_str": "面條 肉絲 淀粉 醬油 辣椒",
"num": 760
},
{
"id": "10044",
"title": "土豆燉翅根",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg",
"collect_num": "758",
"food_str": "土豆 翅根 蔥 姜 料酒",
"num": 758
},
{
"id": "33783",
"title": "美人豆?jié){",
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg",
"collect_num": "758",
"food_str": "黃豆 紅豆 綠豆 黑豆 黑米",
"num": 758
}
]
}
if id != "1":
return {"ret": 0,
"res": "id有誤"}
if limit != "20":
return {"ret": 0,
"res": "頁碼一頁數(shù)據(jù)太多"}
if page != "1":
return {"ret": 0,
"res": "頁數(shù)不存在"}
return body
# 入口
if __name__ == '__main__':
app.run('127.0.0.1', '9090')
# 客戶端
import requests
body = {
"stage_id": "1",
"limit": "20",
"page": "1"
}
res = requests.post("http://127.0.0.1:9090/post/food", json=body)
print(res.json())
5. Charles數(shù)據(jù)mock
①新建一個文件夾mock,在文件夾里新建一個mock.json文件,寫上需要mock的數(shù)據(jù)附帽,即json數(shù)據(jù)
②配置tools -> Map Local Settings
③瀏覽器獲取數(shù)據(jù)
http://www.anfly.com:3000/api
④Charles可以抓包抓到數(shù)據(jù)
二埠戳、 埋點測試:http://www.reibang.com/p/e3d987750f2b
三、接口依賴:json提取器和正則提取器
四蕉扮、數(shù)據(jù)庫優(yōu)化
五整胃、requests中token處理
1. requests獲取token
token = requests.post(url).text.get('token')
2. 設(shè)置發(fā)送token,添加在header中
header = {token : "token"}
requests.post(url喳钟,headers=header)