当前位置:网站首页>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-02 08:19:00 【Testfan_ zhou】
Make a little progress every day , Pay attention to us , Share test technology articles every day
This article is from 【 Code classmate software test 】
Code student official account : Automated software testing
Tiktok : Small code brother talks about software testing
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
Free collection Code classmate software test Course notes + More learning materials + Full video + The latest interview questions , You can forward articles + Direct messages 「 Code classmate 666」 Get information
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 ?
I share a benefit with you ! Code scanning reply 【csdn Code group 】 Join the software testing self-study exchange group , Free technical lectures + Receive learning materials + Watch video lessons for free

author : Code classmate software test
The copyright of this article belongs to the author , For any reprint, please contact the author for authorization and indicate the source .
边栏推荐
- 程序猿学英语-Learning C
- Matlab mathematical modeling tool
- Summary of one question per day: String article (continuously updated)
- JVM instructions
- Using transformer for object detection and semantic segmentation
- OpenCV常用方法出处链接(持续更新)
- install. IMG production method
- Rhel7 operation level introduction and switching operation
- Target detection for long tail distribution -- balanced group softmax
- 包图画法注意规范
猜你喜欢
When a custom exception encounters reflection
Carsim-問題Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?
OpenCV3 6.2 低通滤波器的使用
On November 24, we celebrate the "full moon"
Carla-UE4Editor导入RoadRunner地图文件(保姆级教程)
The internal network of the server can be accessed, but the external network cannot be accessed
SQLyog远程连接centos7系统下的MySQL数据库
11月24号,我们为“满月”庆祝
A brief analysis of graph pooling
Carsim-路面3D形状文件参数介绍
随机推荐
MySQL optimization
Summary of one question per day: stack and queue (continuously updated)
Matlab - autres
Force deduction method summary: double pointer
C language implements XML generation and parsing library (XML extension)
Installation and use of simple packaging tools
JVM instructions
力扣方法总结:滑动窗口
It's great to save 10000 pictures of girls
install. IMG production method
使用C#语言来进行json串的接收
Matlab数学建模工具
Static library and dynamic library
力扣每日一题刷题总结:链表篇(持续更新)
Development of digital collection trading website development of metauniverse digital collection
Find and rfind methods in string
Global and Chinese market of recovery equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Carla-UE4Editor导入RoadRunner地图文件(保姆级教程)
I'll show you why you don't need to log in every time you use Taobao, jd.com, etc?
Common shortcut keys of Jupiter notebook (you can also view it by pressing h in command mode)