当前位置:网站首页>Don't know mock test yet? An article to familiarize you with mock
Don't know mock test yet? An article to familiarize you with mock
2022-07-25 22:05:00 【Code classmate software test】
Hello, Hello everyone , Today, Xiaobian will share with you an implementation mock Service tools moco.
So here comes the question , What is? mock service ?
Mock Service refers to some complex services during testing ( Or not very well constructed ) The object of , Replace it with a virtual object . If there is now A and B Two interfaces , A Need to call interface B To meet the business requirements .
This is the time B The interface has the following three situations :
B Not developed yet — You need to wait for the data of the interface to develop , At this time, the perfect interface Mock The service can greatly shorten the waiting time for development and joint commissioning .
B Some of the scenes are difficult to simulate — For example, timeout. 、 Unknown error or unstable third-party interface .
Isolation during performance testing B Interface ( Third party interface )— There will be problems during the pressure measurement .
Because it's hard to isolate performance changes from other services and third-party interfaces — The response time is not stable .
So how to achieve mock Service? ? There are many ways , There are many open source tools , such as ,mock.js,Easy-mock,moco,fiddler/charles etc. , Or use flask The framework directly implements itself , These are all OK . What I want to share with you today moco Is the realization of many mock One of the service tools , It is also a very popular tool , Also come down to introduce you in detail .
Moco This tool depends on java Environmental Science , So it needs to be installed in advance jdk, It implements mock The principle of service is to change json Configuration file for , It mainly changes the configuration parameter information in the two keywords of request and response . The configuration parameter information of common requests and responses is as follows :


Moco Of jar The download address of the package is as follows :
http://navo.top/UFV7fi

github The address of :
http://navo.top/uyABbu
Start command :
java -jar moco-runner--standalone.jar start -p Port number -c xxx.json
remarks : xxx.json We need to configure it ourselves , According to the configuration request and response Come on mock An interface service , So we can access the defined request To get the corresponding customized response 了 .
All of the following , You have to turn it on first mock service , I put all the configuration information in mock.json In this file , So the start command is java -jar moco-runner-1.1.0-standalone.jar start -p 10086 -c mock.json,
After the service is started successfully , Reuse postman Access interface , See the effect .
Next, I'll give you a detailed list of how to write several commonly used scenes json Script .
Case one , The request method is get The interface of .
Below json The script is that I define a request method, which is get request ,url The address is /get, The request parameter is id='12306’ and name='moco’ The response value is {“text”:“moco get”} Of mock Interface
[
{
"request":
{
"method":"get",
"uri":"/get",
"queries":
{
"id":"12306",
"name":"moco"
}
},
"response":
{
"text":"moco get"
}
}
]
Next we use postman Let's test our mock Interface

The second case , The request method is post The interface of .
2.1 The request parameter is forms Forms
Below json The script is the request method is post, request url The address is /post, The request parameter is forms Forms , The response value is {“text”:“bar”} Of mock Interface
{
"request" :
{
"method" : "post",
"uri": "/post",
"forms" :
{
"name" : "foo"
}
},
"response" :
{
"text" : "bar"
}
}
Next we use postman Let's test our mock Interface

2.2 The request parameter is json
Below json The script is the request method is post, request url The address is /json, The request parameter is json, The response value is {“text”:“bar”} Of mock Interface
[
{
"request": {
"method" : "post",
"uri": "/json",
"json": {
"foo": "bar"
}
},
"response": {
"text": "foo"
}
}
]
Next we use postman Let's test our mock Interface

2.3 The requested parameters are saved in json In the document json data , There is one your_file.json Such a document , The contents are as follows

Define the configuration information json The script is the request method is post, request url The address is /json, Request parameters are stored in your_file.json Medium json data , The response value is {“text”:“ok”} Of mock Interface .
[
{
"request":
{
"method" : "post",
"uri": "/json",
"file":
{
"json": "your_file.json"
}
},
"response":
{
"text": "ok"
}
}
]
Next we use postman Let's test our mock Interface

2.4 carry header and cookie Request
Define the configuration information json The script is the request method is post, request url The address is /json, Request parameters are stored in your_file.json Medium json data , The response value is {“text”:“ok”} Of mock Interface ( The following data are # The following content is just to illustrate , When starting the service, put # And the following information is deleted )
[
{
"description":" This is a belt cookies and headers Of Post request ",# Description of the interface
"request":{
"uri":"/postDemoWithCookies", # Definition url Address
"cookies":{
# Definition cookies value
"login":"true"
},
"headers": {
# Definition headers Value
"content-type": "application/json"
},
"json":{
# Definition json Request parameters for
"name":"hi",
"age":"3"
}
},
"response":{
"status":"200", # Define the response status code
"json":{
# Define the response value as json The data of
"name":"success",
"status":"1"
}
}
}
]
Next we use postman Let's test our mock Interface
cookie and header The information is as follows :

The request parameters are as follows :

2.5 Request for redirection
Define the configuration information json The script is the request method is get, request url The address is /redirect,redirectTo The redirect address is defined after this field ( The following data are # The following content is just to illustrate , When starting the service, put # And the following information is deleted )
``json
[
{
“description”:“ Redirect to Baidu ”,
“request”:{
“uri”:“/redirect”,
“method”:“get”
},
“redirectTo”:“http://www.baidu.com” # Redirect to Baidu
}
]
Next we use postman Let's test our mock Interface

moco This mock That's all for today , People think it's easy to use ?
边栏推荐
- Bitcoin.com:USDD代表了真正去中心化稳定币
- 自动化测试岗花20K招人,到最后居然没一个合适的,招两个应届生都比他们强吧
- 分享|智慧消防应急管理平台解决方案(附PDF)
- 成为比开发硬气的测试人,我都经历了什么?
- 开源的RSS订阅器FreshRSS
- Guiding principles of information security construction
- jenkins+SVN配置
- Come again
- The file cannot be saved (what if the folder is damaged and cannot be read)
- 如何实现一个App应用程序,限制用户时间使用?
猜你喜欢

面了个腾讯三年经验的测试员,让我见识到了真正的测试天花板

After three years of software testing at Tencent, I was ruthlessly dismissed in July, trying to wake up my brother who was paddling

Jmeter---设置代理录制请求
![[Fantan] how to design a test platform?](/img/54/5aca54c0e66f8a7c1c3215b8f06613.png)
[Fantan] how to design a test platform?

After 2 years of functional testing, I feel like I can't do anything. Where should I go in 2022?

2022 the latest software tests eight part essay. Whether you can offer depends on how you recite it

jenkins+SVN配置

6-17漏洞利用-反序列化远程命令执行漏洞

Open source RSS subscriber freshrss

2022最新软件测试八股文,能不能拿心仪Offer就看你背得怎样了
随机推荐
Performance debugging -- chrome performance
Redis 使用详解
【饭谈】测试平台为什么有组件化?模块化?很多看不到的地方设计的很好是种浪费么?
2022 love analysis ― bank digitalization practice report
How to configure and use rocksdb in the flinksql environment
The testing work is not valued. Have you changed your position?
【饭谈】如何设计好一款测试平台?
Ansible+cronab batch deployment patrol
The reisson distributed lock renewal failed due to network reasons, resulting in the automatic release of the lock when the business is still executing but the lock is not renewed.
jenkins+SVN配置
虚拟内存与磁盘
Create files, file permissions, ownership, and sticky bits
Redis基础2(笔记)
Redis为何选择单线程?
Come again
五种分配方式是否会产生内部碎片、外部碎片
Summary of function test points of wechat sending circle of friends on mobile terminal
Can I buy financial products with a revenue of more than 6% after opening an account
The technical aspects of ByteDance are all over, but the result is still brushed. Ask HR why...
6-18漏洞利用-后门连接