一、moco框架基本介绍
mock测试:mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。一般可分为模拟调用方和被调用方2种方式。
mock用来模拟接口,这里mock用的是moco框架,moco框架是github上的一个开源项目,可模拟http,https,Socket协议。moco有几种使用方法,这里介绍的是standolone用法,更多用法可参考https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md
二、下载和使用
安装和基本使用
下载
下载moco-runner-0.11.0-standalone.jar
启动命令:
java -jar http -p -c
示例:
java -jar moco-runner-1.1.0-standalone.jar http -p 9090 -c test.json
:jar包的路径
:http服务监听的端口
:配置文件路径
接口访问
打开浏览器,在浏览器地址栏中输入:http://localhost:9090/index
访问结果中文乱码
在response中添加设置编码格式为“GBK”,修改后json文件内容为:
{
"description": "这是我们的第一个mock例子",
"request": {
"uri": "/getdemo",
"method": "get"
},
"response": {
"text": "这是一个没有参数的get请求",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
},
mock使用
https://blog.csdn.net/panting30/article/details/96482749
https://zhuanlan.zhihu.com/p/159164380
参考文档
不带参数的get访问:
http://localhost:8888/getdemo
带参数的get访问:
http://localhost:8888/getdemo?name=tom&age=20
post访问:
通过postman进行访问