一、 mock
1. 什么是mock?
①测试桩:模拟被测对象的返回,用于测试
②mock:通常指mock server ,模拟服务返回的接口数据,用于前端开发、第三方接口联调想·
2. 为什么要学习mock?
①前后端开发进度不一致,前端开发快于后端开发,前端需要一个假的接口用于模拟后端数据返回。
②项目用到第三方接口,三方接口没有开发好或者没有开发环境
3. 如何使用mock?
①利用抓包工具fiddler,但是不方便而且也不专业
②用web框架模拟:Django、flask(结构简单,容易入门)
4. flask使用
①安装flask:pip install flask
②使用flask
- 案例一:最简单的get请求
# 服务端
# 导包
from flask import Flask
# 实例化一个web服务对象,app 这个名字是预定俗成的
app = Flask(__name__)
# 定义一个路由 -- 访问服务器的根目录就能得到结果
@app.route('/')
# 创建一个方法来处理请求
def hello():
data = {
"error_code": 0,
"reason": "success",
"result": {
"data": {
"conclusion": "独营生意,和气吉祥,排除万难,必获成功",
"analysis": "主人性格类型:[大胆行事冲动派型],其具体表现为:爱好追寻刺激,有不理后果大胆行事的倾向。崇尚自由奔放的恋爱,会拼尽全力爱一场,是就算明知无结果都在所不惜的冲动派。"
}
}
}
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)
- 案例二:构建post请求的响应
# 服务端
# 导包
from flask import Flask,request
# 实例化一个web服务对象,app 这个名字是预定俗成的
app = Flask(__name__)
# 构造一个接受post请求的响应
@app.route('/post',methods = ["POST"])
def test_post():
# 使用request处理请求发送过来的参数
id = request.form["id"]
name = request.form["name"]
return "接受到请求发送过来的数据:"+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)
- 案例三:复杂的post
# 服务端
# 导包
from flask import Flask, request, json
# 实例化一个web服务对象,app 这个名字是预定俗成的
app = Flask(__name__)
# 处理稍微复杂的post请求
@app.route("/post/food", methods=["POST"])
def post_food():
# 拿到客户端返回的数据
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": "美人豆浆",
"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": "页码一页数据太多"}
if page != "1":
return {"ret": 0,
"res": "页数不存在"}
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数据mock
①新建一个文件夹mock,在文件夹里新建一个mock.json文件,写上需要mock的数据,即json数据
②配置tools -> Map Local Settings
③浏览器获取数据
http://www.anfly.com:3000/api
④Charles可以抓包抓到数据
二、 埋点测试:https://www.jianshu.com/p/e3d987750f2b
三、接口依赖:json提取器和正则提取器
四、数据库优化
五、requests中token处理
1. requests获取token
token = requests.post(url).text.get('token')
2. 设置发送token,添加在header中
header = {token : "token"}
requests.post(url,headers=header)