当前位置:网站首页>Moco of Mock tools use tutorial
Moco of Mock tools use tutorial
2022-08-03 01:42:00 【Programmer Uncle Yang】
目录
一、什么是Moco
在开发过程中,经常会使用到一些http网络接口,而这部分功能通常是由第三方开发团队或者是后端同事进行开发的,在我们开发时不能给我们提供服务,这为我们的联调和测试造成了麻烦,这个时候就需要使用到mock测试.
Moco是一个简单搭建模拟服务器的程序库/工具:Moco会根据一些配置,启动一个真正的HTTP服务(会监听本地的某个端口).当发起请求满足一个条件时,它就给回复一个应答.
二、安装&配置
Jar包下载:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/moco-runner-0.11.0-standalone.jar
Moco文档:https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md
启动命令:
java -jar ./moco-runner-0.11.1-standalone.jar http -p 9090 -c all.json
-p 就是port 端口号, -c The following is the configuration interface contentjson文件.
启动成功效果:
三、接口配置&测试
3.1 first simple request:
all.json文件中增加以下request&response内容
[
{
"description": "这是第一个demo",
"request": {
"uri":"/demo"
},
"response": {
"text":"这是第一个demo",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
]
注意:mocoSimulated request returns Chinese garbled problem,Add response header encoding format asgbk,Can solve the results returned Chinese garbled
"headers":{
"Content-Type":"text/html;charset=gbk"
}
Test interface request result:
3.2 Get请求:
{
"description": "这是一个带参数的Get请求的Demo",
"request": {
"uri": "/getWithParam",
"method": "get",
"queries": {
"name": "张三",
"sex": "20"
}
},
"response": {
"text": "我张三会来了",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
Test interface request result:http://localhost:9090/getWithParam?name=张三&sex=20
3.3 Post请求:
{
"description": "这是一个带参数的post请求",
"request": {
"uri": "/postWithParam",
"method": "post",
"forms": {
"name": "张三",
"sex": "man"
}
},
"response": {
"text": "我张三带着参数回来了",
"headers":{
"Content-Type":"text/html;charset=gbk"
}
}
}
Test interface request result:
注意:Input parameter with Chinese value,如果换成x-www-form-urlencoded格式(postDefault format for requests),则会请求失败:
这是Moco目前的问题,没有解决,English entry is no problem:
3.4 返回值为Json格式的请求:
{
"description": "电话号码注册post请求",
"request": {
"uri": "/postWithPhone",
"method": "post",
"forms": {
"type": "1"
}
},
"response": {
"json":{
"code": "0000",
"msg": "请求成功",
"data": {
"userId": 111111,
"phone": "12011111111",
"cardNo": "8000000000"
}
}
}
}
Test interface request result:
3.5 带cookie信息才能访问的post请求:
{
"description": "这是一个需要带有cookie信息才能访问的post请求",
"request": {
"method": "post",
"uri": "/postWithCookie",
"cookies": {
"login":"true"
},
"json": {
"name": "zhangsan",
"age": "18"
}
},
"response": {
"status":200,
"json": {
"name":"张三",
"status": "1"
}
}
}
Test interface request result:
3.6 重定向请求:
{
"description": "重定向到百度",
"request": {
"uri":"/redirect"
},
"redirectTo":"http://www.baidu.com"
}
Test interface request result:
3.7 前后端联调mock实战:
- mocoAfter running locally,后端server地址: 127.0.0.1:9090
- Called by front-end page operation requestmockinterface test:
四、搭建Mocoserver for others to call
Since the local can be used,Then run on the server,more people(包括研发)使用,provide a simplemock服务.下载Mocothe package to the server and unpack it,然后运行:
访问Moco服务:http://服务器IP:9090/postDemo
服务器上mock返回日志:
注意点:
- As mentioned earlier, the local runtime,If the returned content has Chinese,Need to add a paragraphheader指定格式为gbk才能正常显示中文.Tests found running on the server,mock返回内容Json文件中,Instead, remove this paragraph,Returns the content to show Chinese properly,If there is this paragraph, it will display garbled characters in Chinese:
"headers":{
"Content-Type":"text/html;charset=gbk"
}
- If the server wants tomock服务一直运行,使用命令:
nohup java -jar ./moco-runner-0.11.1-standalone.jar http -p 9090 -c all.json > /dev/null 2> /dev/null &
If just in frontnohup,会报错:
nohup: ignoring input and appending output to ‘nohup.out’
原因是nohupExecution produces a log,默认是写到nohup.out文件中,文件没有写入权限.Need to cooperate with the following command > /dev/null 2> /dev/null & 将 nohup 的日志输出到 /dev/null,Similar to the directorylinuxof a black hole,will make all messages to it disappear automatically.
If you want to keep logs,可以新建一个文件,比如moco.log, 然后nohupWhen the specified log is written tomoco.log中:
nohup java -jar ./moco-runner-0.11.1-standalone.jar http -p 9090 -c all.json >moco.log &
The log information is recorded as follows:
=====================================================================================
以上就是本次的全部内容,如果对你有帮助,欢迎关注我的微信公众号:程序员杨叔,各类文章都会第一时间在上面发布,持续分享全栈测试知识干货,你的支持就是作者更新最大的动力~
边栏推荐
- Numpy数组中d[True]=1的含义
- 00 -- jieba分词
- nmap: Bad CPU type in executable
- WebShell 木马免杀过WAF
- What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
- 2022中国眼博会,山东眼健康展,视力矫正仪器展,护眼产品展
- 2022暑假牛客多校1 (A/G/D/I)
- vscode 自定义快捷键——设置eslint
- Technology Sharing | How to do assertion verification for xml format in interface automation testing?
- 十三、数据回显
猜你喜欢
随机推荐
最新真实软件测试面试题分享,收藏了还怕进入不了大厂?
Let's talk about the charm of code language
用了 TCP 协议,数据一定不会丢吗?
基于两级分解和长短时记忆网络的短期风速多步组合预测模型
markdown语法
2022杭电多校第一场(K/L/B/C)
qt静态编译出现Project ERROR: Library ‘odbc‘ is not defined
Technology Sharing | How to do assertion verification for xml format in interface automation testing?
mPEG-Cholesterol,mPEG-CLS,甲氧基-聚乙二醇-胆固醇可用于脂质体制备
HCIP(16)
Rebound shell principle and implementation
买母婴产品先来京东“券民空间站”抢券!大牌好物低至5折
如何突破测试/开发程序员思维?一种不一样的感觉......
1 - vector R language self-study
C语言函数详解(2)【函数参数——实际参数(实参)&形式参数(形参)】
嵌入式分享合集26
厌倦了安装数据库?改用 Docker
CAS:1445723-73-8,DSPE-PEG-NHS,磷脂-聚乙二醇-活性酯两亲性脂质PEG共轭物
CKAN教程之在 AWS 上部署 CKAN 应用程序
nmap: Bad CPU type in executable