Mock简介
什么是Mock测试&Mock服务?
Mock是在测试过程中,对于一些不容易构造/获取的对象,创建一个Mock对象来模拟对象的行为。
而Mock服务是指mock一个server,这个server能够被其他方调用并且能够设定预期的返回值。
使用场景
- 接口功能尚未开发完成,此时如何开展接口测试工作?
- 项目开发时涉及到第三方的支付接口时,开发阶段如何进行调试?
- 依赖的接口不稳定,经常导致集成测试失败。
- 前端开发已写好页面,后台接口还未实现,前端人员想要调试页面的效果,没有数据
Moco
国人开发基于Java的开源项目,能够快速搭建Mock服务
https://github.com/dreamhead/moco
使用方式:
- 代码运行
https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md
- jar包运行
启动Moco服务
Step1:编写json配置文件
[
{
"description": "第一个请求响应",
"response": {
"text": "hello world"
}
}
]
Step2:加载jar包启动服务
java -jar moco-runner-1.1.0-standalone.jar http -p 9999 -c test.json
Step3:打开浏览器输入http://127.0.0.1:9999访问
Moco常用配置参数
请求信息设置
uri
指定请求资源地址
[
{
"description": "登录请求",
"request": {
"uri": "/login"
},
"response": {
"text": "登录成功"
}
}
]
method
指定请求方法
[
{
"description": "登录请求",
"request": {
"uri": "/login",
"method": "get"
},
"response": {
"text": "登录成功"
}
}
]
queries
指定查询参数
[
{
"description": "登录请求",
"request": {
"uri": "/login",
"method": "get",
"queries": {
"mobile_phone": "13323234545",
"pwd": "123456"
}
},
"response": {
"text": "登录成功"
}
}
]
forms
指定表单参数
[
{
"description": "登录请求",
"request": {
"uri": "/login",
"method": "post",
"forms": {
"mobile_phone": "13323234545",
"pwd": "123456"
}
},
"response": {
"text": "登录成功"
}
}
]
json
指定json请求体参数
[
{
"description": "登录请求",
"request": {
"uri": "/login",
"method": "post",
"json": {
"mobile_phone": "13323234545",
"pwd": "123456"
}
},
"response": {
"text": "登录成功"
}
}
]
headers
指定请求头
[
{
"description": "登录请求",
"request": {
"uri": "/login",
"method": "post",
"headers": {
"X-Lemonban-Media-Type": "lemonban.v1"
}
},
"response": {
"text": "登录成功"
}
}
]
响应信息设置
status
指定响应状态码
[
{
"description": "登录请求",
"request": {
"uri": "/login",
"method": "post"
},
"response": {
"status": 200
}
}
]
headers
指定响应头
[
{
"description": "登录请求",
"request": {
"uri": "/login",
"method": "post"
},
"response": {
"status": 200,
"headers" :{
"content-type" : "application/json"
}
}
}
]
cookies
指定响应cookie
[
{
"description": "登录请求",
"request": {
"uri": "/login",
"method": "post"
},
"response": {
"status": 200,
"headers": {
"content-type" : "application/json"
},
"cookies": {
"jsessionid": "1234567"
}
}
}
]
json
指定响应json数据
[
{
"description": "登录请求",
"request": {
"uri": "/login",
"method": "post"
},
"response": {
"status": 200,
"headers": {
"Content-Type" : "application/json"
},
"cookies": {
"jsessionid": "1234567"
},
"json":{
"code": 0,
"msg": "OK"
}
}
}
]
moco中文返回乱码问题:
启动moco服务加上参数
java -Dfile.encoding=UTF-8 -jar moco-runner-1.1.0-standalone.jar http -p 9090 -c test.json
欢迎来到testingpai.com!
注册 关于