当前位置:网站首页>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 .
边栏推荐
- Go functions make, slice, append
- Installation and use of simple packaging tools
- What are the platforms for selling green label domain names? What is the green label domain name like?
- 学习写文章格式
- C language implements XML generation and parsing library (XML extension)
- Matlab - autres
- My VIM profile
- Real world anti sample attack against semantic segmentation
- Carsim-问题Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?
- Wang extracurricular words
猜你喜欢
Using transformer for object detection and semantic segmentation
On November 24, we celebrate the "full moon"
Smart agriculture solutions smart agriculture system development
Vs code configuration problem
Opencv3 6.3 reduced pixel sampling with filters
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
Array and string processing, common status codes, differences between PHP and JS (JS)
St-link connection error invalid ROM table of STM32 difficult and miscellaneous diseases
Data reverse attack under federated learning -- gradinversion
随机推荐
Short video with goods source code, double-click to zoom in when watching the video
Programmers can only be 35? The 74 year old programmer in the United States has been programming for 57 years and has not retired
力扣每日一题刷题总结:链表篇(持续更新)
Carsim problem failed to start Solver: Path Id Obj (X) was set to y; Aucune valeur de correction de xxxxx?
学习写文章格式
Sequence problem for tqdm and print
What are the platforms for selling green label domain names? What is the green label domain name like?
Specification for package drawing
My VIM profile
关于原型图的深入理解
W10 is upgraded to W11 system, but the screen is black, but the mouse and desktop shortcuts can be used. How to solve it
力扣方法总结:查找类
2022 Heilongjiang latest food safety administrator simulation exam questions and answers
力扣方法总结:滑动窗口
Backup, recovery and repair of XFS file system
Vs code configuration problem
C语言实现XML生成解析库(XML扩展)
Vscode下中文乱码问题
Organigramme des activités
Global and Chinese markets of tilting feeders 2022-2028: Research Report on technology, participants, trends, market size and share