当前位置:网站首页>Several common implementation methods of mock interface test
Several common implementation methods of mock interface test
2022-06-21 15:17:00 【m0_ sixty-one million eighty-three thousand four hundred and ni】
What is an interface Mock test
Mock Test definition
Mock Testing is in the process of testing , For some complex objects that are not easy to construct or obtain , Use a mock object to create a test method for testing
Mock Test application scenarios
- The test interface needs to rely on the return value of the third-party interface for logical processing , For example, the payment interface of the bank
- Simulate abnormal data return , For example, you need to return special characters 、 Different lengths 、 Type format content
- Front and back end development parallel work mode , When the back-end interface is not completed , Front end calls Mock Data is adjusted
- Isolation environment , Ensure data security and correctness , about POST、PUT、DELETE Wait for the request to be isolated , Prevent other unknown errors in modifying data
Mock Several common implementation methods
- fiddler/Charles Tools
- Mock Server-Moco
- Self development Mock platform
adopt fiddler
stay fiddler Interface ---- Find out what you want mock The interface of
With URI:/api/v1/my/index For example ,fiddler Of web session Interface find the following interface /api/v1/my/index This interface
Save interface response data
Right click on the interface , Choose Save -> Respond to -> Response subject , Save to the specified folder :my_index.json

Modifying data
Modify the saved to local my_index.json file , Add special characters to the user name .
add to /api/v1/my/index To mock The rules
stay web session Find the corresponding request in the panel , Then drag it to or select the interface and click Add to AutoResponder The palette , stay RuleEditor Middle click “Find a file…”, Select local json Path to file , Click save .



Activation rules
Check enable rule and request delivery , Activation rules .

Refresh the page
Refresh the browser ( Or direct access interface ), And you can see the effect , I want to modify other data later , Just modify the locally saved json File can .
Cancel mock
You only need to use the auto response rule list area interface , Uncheck it

Mock Server-Moco
Server-Moco explain
- Moco Is a simple framework to build a simulation server ( Tools ), Can simulate http、https、socket Such agreement
- be based on Java Open source project developed ,Github Address :https://github.com/dreamhead/moco
- principle :Moco According to some configuration , Start a real HTTP service ( Will listen to a local port ). When an initiated request meets a certain condition , The specified response data will be returned , Need modification
Environment building
- Download and install : rely on JDK Environmental Science , Perform computer installation JDK, Proposed installation JDK1.8 edition
- Server-Moco Download address :https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner, Select the appropriate version , Download the latest version 1.3.0, Download it moco-runner-1.3.0-standalone.jar package


Moco Use
Start parameter description
java -jar <path-to-moco-runner> http -p <monitor-port> -c <configuration-file>
java -jar <path-to-moco-runner> http -p <monitor-port> -g <configuration-file>
- path-to-moco-runner:jar The path of the package
- monitor-port:http The port the service listens on
- configuration-file: Profile path
Mock A single request
Create a json file , be known as login.json, The contents are as follows :
[
{
“description”: “ Sign in ”,
“request”: {
“uri”: “/login”
},
“response”: {
“text”:“ Login successful ”
}}]
start-up Mock service
java -jar moco-runner-1.1.0-standalone.jar http -p 8082 -c login.json

- stay Postman Send a request :http://localhost:8082/login

Mock Multiple requests
Creating a json file , be known as home.json, The contents are as follows :
[
{
“description”: “ home page ”,
“request”: {
“uri”: “/home”
},
“response”: {
“text”:“ Enter the home page ”
}}]
Create a request management profile , be known as
config.json, The contents are as follows :[
{“include”: “login.json”},
{“include”: “home.json”}
]start-up Mock service
java -jar moco-runner-1.1.0-standalone.jar http -p 8082 -g config.json

- stay Postman Send a request :http://localhost:8082/login and http://localhost:8082/home

Common parameter configuration
Request mode , adopt method Parameters are defined
[
{
“description”: “ Sign in ”,
“request”: {
“uri”: “/login”,
“method”: “post”
},
“response”: {
“text”: “ Login successful ”
}
}
]Request parameters , adopt queries Parameters are defined
[
{
“description”: “ Sign in ”,
“request”: {
“uri”: “/login”,
“method”: “post”,
“queries”: {
“username”: “king”,
“pwd”: 123456
}
},
“response”: {
“text”: “ Login successful ”
}
}
]Request header , adopt headers Parameters are defined
[
{
“description”: “ Sign in ”,
“request”: {
“uri”: “/login”,
“method”: “post”,
“headers”: {
“appId”: “4275CCE0-CE16-4BCC-9AF5-31CA133AC9AD”
}
},
“response”: {
“text”: “ Login successful ”
}
}
]Form request body , adopt forms Parameters are defined
[
{
“description”: “ Sign in ”,
“request”: {
“uri”: “/login”,
“method”: “post”,
“forms”: {
“username”: “king”,
“password”: 123456
}
},
“response”: {
“text”: “ Login successful ”
}
}
]JSON Request body , adopt json Parameters are defined
[
{
“description”: “ Sign in ”,
“request”: {
“uri”: “/login”,
“method”: “post”,
“headers”: {
“Content-Type”: “application/json”
},
“json”: {
“username”: “king”,
“password”: 123456
}
},
“response”: {
“text”: “ Login successful ”
}
}
]HTTP Response status code , adopt status Parameters are defined
[
{
“description”: “ Sign in ”,
“request”: {
“uri”: “/login”,
“method”: “post”
},
“response”: {
“status”: 500,
“text”: “ Server exception !”
}
}
]JSON The response data , adopt json Parameters are defined
[
{
“description”: “ Sign in ”,
“request”: {
“uri”: “/login”,
“method”: “post”
},
“response”: {
“headers”: {
“Content-Type”: “application/json;charset=UTF-8”
},
“json”: {
“code”: “0”,
“msg”: “ Successful operation ”,
“data”: {
“userID”: 3175896456,
“token”: “17e6ad42cff592-0dfc3f6fa02aee-c343365-1fa400-17e6ad42d00891”
}
}
}
}
]
Self development Mock platform
A little
Use Mock Be careful
- When put mock After the interface is replaced with the actual interface , The test must also be repeated
- The test is over , Before going online, make sure , in order to mock And do the relevant code / Modification of configuration file
The above content is purely personal understanding , If there is any deficiency , Welcome to correct , Reprint please indicate the source !
If you think the article is good , Welcome to WeChat official account. , The official account of WeChat is regularly pushing the relevant test technology articles.
《 Front end interview questions of first-line large factories + Develop learning notes + The latest architecture explanation video + Handout of the event station project 》
【https://docs.qq.com/doc/DQVJzbnRIWWNtcVR4】 Full content open source sharing
边栏推荐
- The whole process of Netease cloud music API installation and deployment [details of local running projects and remote deployment]
- Nodejs process has too many open files
- Selection (042) - what is the output of the following code?
- Cmake upgrade
- LINQ extension methods - any() vs. where() vs. exists() - LINQ extension methods - any() vs. where() vs. exists()
- [go] goroutine pool
- Subshell
- Phantom star VR product details 34: Happy pitching
- [go] time package
- Is the switch network layer
猜你喜欢

Design of LVDS interface based on FPGA

Select everything between matching brackets in vs Code - select everything between matching brackets in vs Code
![[Yugong series] February 2022 wechat applet -app Subpackages and preloadrule of JSON configuration attribute](/img/94/a2447b3b00ad0d8fb990917531316d.jpg)
[Yugong series] February 2022 wechat applet -app Subpackages and preloadrule of JSON configuration attribute

Decipher Bishengyuan: is it tea that consumers buy, or is it IQ tax?

Application GDB debugging

Promotion guide for large enterprises: material preparation, PPT writing and on-site defense

2022 latest MySQL interview questions
![In 2021, China's deposit balance continued to grow, and the balance of RMB and foreign currency deposits reached a record high [figure]](/img/23/652f596091dde97031a27bdbccdd0f.jpg)
In 2021, China's deposit balance continued to grow, and the balance of RMB and foreign currency deposits reached a record high [figure]

Build an efficient and scalable result cache
![The whole process of Netease cloud music API installation and deployment [details of local running projects and remote deployment]](/img/3b/678fdf93cf6cc39caaec8e753af169.jpg)
The whole process of Netease cloud music API installation and deployment [details of local running projects and remote deployment]
随机推荐
JS interview questions: variable promotion function promotion, scope chain error prone questions
Online keyboard key detection tool
Clickhouse cluster installation has too many dry goods
ARP interaction process
Exit() function, macro exit_ Success, macro exit_ Difference between failure, exit() and return
C语言的指针
马拦过河卒
Phantom star VR product details 32: Infinite War
Num in tensorflow basiclstmcell_ What are units- What is num_ units in tensorflow BasicLSTMCell?
100% troubleshooting and analysis of Alibaba cloud hard disk
Link storage structure of simulated design disk file
Build an efficient and scalable result cache
Complete and detailed installation steps for kubeneter version 1.7
Not only products, FAW Toyota can give you "all-round" peace of mind
NPM package management configuration file [package.json and node\u modules configuration details and how to develop their own packages and publish them on NPM]
57 common mistakes in golang development
JS written test question: asynchronous
Use OpenCV to decompose the video into single frame pictures and synthesize the video with pictures
Reasonably set the number of threads 【 rpm 】
LINQ extension methods - any() vs. where() vs. exists() - LINQ extension methods - any() vs. where() vs. exists()