当前位置:网站首页>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
- Summary of one question per day: String article (continuously updated)
- Organigramme des activités
- 力扣每日一题刷题总结:链表篇(持续更新)
- Vscode下中文乱码问题
- Global and Chinese markets for magnetic resonance imaging (MRI) transmission 2022-2028: Research Report on technology, participants, trends, market size and share
- OpenCV常用方法出处链接(持续更新)
- MySQL优化
- Data reverse attack under federated learning -- gradinversion
- Erase method in string
猜你喜欢
CarSim problem failed to start solver: path_ ID_ OBJ(X) was set to Y; no corresponding value of XXXXX?
Fundamentals of music theory (brief introduction)
w10升级至W11系统,黑屏但鼠标与桌面快捷方式能用,如何解决
On November 24, we celebrate the "full moon"
2022 Heilongjiang's latest eight member (Safety Officer) simulated test question bank and answers
When a custom exception encounters reflection
Animation synchronization of CarSim real-time simulation
11月24号,我们为“满月”庆祝
Using super ball embedding to enhance confrontation training
Carsim-路面3D形状文件参数介绍
随机推荐
业务架构图
Summary of one question per day: linked list (continuously updated)
Constant pointer and pointer constant
Specification for package drawing
C language implements XML generation and parsing library (XML extension)
Use of OpenCV 6.4 median filter
Use of opencv3 6.2 low pass filter
我的vim配置文件
Data reverse attack under federated learning -- gradinversion
The source code of the live app. When the verification method is mailbox verification, the verification code is automatically sent to the entered mailbox
Li Kou daily one question brushing summary: binary tree chapter (continuous update)
Global and Chinese market of wire loop, 2022-2028: Research Report on technology, participants, trends, market size and share
Brief introduction of prompt paradigm
Array and string processing, common status codes, differences between PHP and JS (JS)
Summary of one question per day: stack and queue (continuously updated)
笔记本电脑卡顿问题原因
Rhel7 operation level introduction and switching operation
Global and Chinese markets of tilting feeders 2022-2028: Research Report on technology, participants, trends, market size and share
Vscode下中文乱码问题
Erase method in string