当前位置:网站首页>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 .
边栏推荐
- 力扣每日一题刷题总结:栈与队列篇(持续更新)
- Common shortcut keys of Jupiter notebook (you can also view it by pressing h in command mode)
- STL速查手册
- Summary of one question per day: String article (continuously updated)
- Principes fondamentaux de la théorie musicale (brève introduction)
- 2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
- How to uninstall SQL Server cleanly
- STL quick reference manual
- High school mathematics compulsory one
- Short video with goods source code, double-click to zoom in when watching the video
猜你喜欢

CarSim learning experience - rough translation 1

Carsim 学习心得-粗略翻译1

Cvpr19 deep stacked hierarchical multi patch network for image deblurring paper reproduction

Generate database documents with one click, which can be called swagger in the database industry

2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers

STM32-新建工程(参考正点原子)

OpenCV3 6.3 用滤波器进行缩减像素采样

Intelligent manufacturing solutions digital twin smart factory

【无标题】

Programmers can only be 35? The 74 year old programmer in the United States has been programming for 57 years and has not retired
随机推荐
STM32-新建工程(参考正点原子)
OpenCV 6.4 中值滤波器的使用
Matlab-其它
Use C language to receive JSON strings
Erase method in string
高中数学必修一
Sparse matrix storage
High school mathematics compulsory one
Business architecture diagram
Force buckle method summary: sliding window
The internal network of the server can be accessed, but the external network cannot be accessed
Cvpr19 deep stacked hierarchical multi patch network for image deblurring paper reproduction
SQL操作数据库语法
STM32疑难杂症之ST-LINK Connection error INVALID ROM TABLE
Global and Chinese market of wire loop, 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL optimization
Comparison between setTimeout and requestanimationframe (page refresh)
Library function of C language
How to back up the configuration before the idea when reinstalling the idea
SQL server如何卸载干净