当前位置:网站首页>Mock test
Mock test
2022-04-23 16:40:00 【Sink the wine cup and fleeting time】
Mock test
Mock test
Mock It's during the test , For some that are not easy to construct / The object of acquisition , Create a Mock object To simulate the behavior of objects
Mock Method It is a common technology in unit testing , Its main function is to simulate some objects that are not easy to construct or more complex in application , This separates the test from objects outside the test boundary .
Mock Role of testing
During unit testing , Data may need to be obtained from multiple sources , And the process is complex

Class A From you to Class B and Class C Get data in , and Class C The data comes from Class D and Class E
You can use Mock To construct virtual Class B and Class C Object is used for Class A Test of , Because we just want to test class A Of Whether the behavior is in line with expectations , There is no need to test dependent objects
Mock Functions can be based on Interface / Data structure definition 、Mock Rule configuration 、Mock Expected configuration , Automatically generate simulation data , And users can flexibly construct interface data of various structures according to their needs .
In the project iteration process , May come across , The front-end personnel have completed the development task , The interface development of back-end personnel has not been completed yet .
This situation , Can pass postman、Apifox Wait for the test tool to build a Mock service , Conduct joint commissioning with front-end personnel first Mock test , Test some contents of the front end , such , When the backend interface is written and submitted to the test environment for testing , It will also reduce some on the front end bug.
But in general , Front end personnel will pass by themselves Mock Test your own code , There is no need for testers to Mock test .
When doing interface tests , If you call the interface of a third-party system , The third-party system does not provide a test environment , You need testers to use Mock technology , adopt Mock service , Create a simulated fake interface , According to the interface documentation , Preset the contents returned by various requests , In this way, in the test process , It won't be because some interface doesn't work , Testing that affects the whole business process .
Mock The advantages of testing
- Teams work in parallel
Development process , As long as both sides of the interaction define the interface , Teams can work in parallel , Processes do not affect each other , You don't have to wait for each other's progress , Just agree on the data specifications between each other ( Interface document ), You can use mock Build available interfaces , Then carry out development and self-test as soon as possible , Discover defects ahead of time - Test-driven development TDD (Test-Driven Development)
Unit tests are TDD The cornerstone of realization , and TDD We often encounter the situation that the collaborative module has not been developed yet , But there is. mock, Testers can access the test in advance , Provide test efficiency , When the interface is defined , The tester can create a Mock, Add the interface to the automated test environment , Create tests ahead of time . - Test coverage
The interface involves input parameters , Or complex business logic , Some scenarios cannot be operated by normal means , And by mock Virtual simulation , It can effectively increase the coverage - Isolation system
If an interface needs to return different values in different states , The common practice is to reproduce this state and then request the interface , When using some interfaces , Failure may be caused by improper operation timing or mode , Even pollute the back-end storage, such as databases , In order to prevent the system database from being polluted , You can adjust the interface to Mock Pattern , To ensure the purity of the database .
Mock Disadvantages of testing
Mock It's not everything , Use Mock There are also risks , It is necessary to determine whether to choose... According to the actual situation and specific needs of the project Mock.
If you use a lot during the test Mock,mock The test scenario lost its authenticity , It may lead to discovery only during subsequent systematic testing bug, It makes the defects found later , It may cause greater cost of subsequent repair
Mock client
The interface document has been docked , But the back-end interface has not been developed yet , Testers need to do unit tests , Or conduct interface automation test in advance , Need to use Mock The contents returned by various requests , have access to Python Medium Mock library
test Python Medium Mock library
install
pip install Mock
Mock Use
import unittest
from mock import Mock
def VerifyPhone():
''' Verify the user's mobile phone number '''
pass
class TestVerifyPhone(unittest.TestCase):
def test_verify_phone(self):
data = {
"code": "0000","msg": {
"result": "success","phoneinfo": " Mobile users "}}
VerifyPhone = Mock(return_value =data)
self.assertEqual("success",VerifyPhone()["msg"]["result"])
hypothesis , There is a function to check the user's mobile phone number (VerifyPhone), Not finished yet , But at this time, it needs to be tested in advance , By decoupling Wendy , We know what this function does , And the situation that the interface will return
Mobile phone number verification passed
{
"code": "0000",
"msg": {
"result": "success",
"phoneinfo": " Mobile users "
}
}
Mobile number format error
{
"code": "0002",
"msg": {
"result": "false",
"phoneinfo": " Mobile number format error "
}
}
Cell phone number already exists
{
"code": "0003",
"msg": {
"result": "false",
"phoneinfo": " Cell phone number already exists "
}
}
But obviously , This function is not available yet , So pass Mock(return_value =data), take Mock Result ( Equivalent to what the interface will return ), Assign variable VerifyPhone, In this assertion VerifyPhone()["msg"]["result"] Result , Namely "success", Assertion passed .
When the function development is completed ( Interface development completed ), Get rid of VerifyPhone = Mock(return_value =data) This line of code is enough . such , It has achieved the function of testing in advance , Without much modification , Cost savings
Mock Server side
The front-end personnel have completed the development task , And the back-end developers haven't finished the interface yet , Joint commissioning with front-end personnel is required for testing , You need to build Mock service
There are many ways , For example, through postman、Apifox Wait for the test tool to build a Mock service ,Mock An interface to test . Please refer to this article Use Postman Conduct Mock test
Recommended Apifox Conduct Mock The interface test ,Apifox Of Mock It has powerful functions ,Apifox The official documents are very detailed , I won't give you an example here
版权声明
本文为[Sink the wine cup and fleeting time]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231402128148.html
边栏推荐
- Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
- The most detailed knapsack problem!!!
- 【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
- 七朋元视界可信元宇宙社交体系满足多元化的消费以及社交需求
- Public variables of robotframework
- Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
- 如何建立 TikTok用户信任并拉动粉丝增长
- Six scenarios of cloud migration
- True math problems in 1959 college entrance examination
- OMNeT学习之新建工程
猜你喜欢

G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration

建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细
Xinwangda: HEV and Bev super fast charging fist products are shipped on a large scale

Solution of garbled code on idea console

Detailed explanation of file operation (2)

Creation of RAID disk array and RAID5

Download and install mongodb

Selenium IDE and XPath installation of chrome plug-in

Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot

Postman batch production body information (realize batch modification of data)
随机推荐
Hypermotion cloud migration helped China Unicom. Qingyun completed the cloud project of a central enterprise and accelerated the cloud process of the group's core business system
JSP learning 2
JMeter setting environment variable supports direct startup by entering JMeter in any terminal directory
The first line and the last two lines are frozen when paging
Public variables of robotframework
∑GL-透视投影矩阵的推导
Construction of promtail + Loki + grafana log monitoring system
Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
文件系统读写性能测试实战
File upload and download of robot framework
True math problems in 1959 college entrance examination
◰GL-阴影贴图核心步骤
MySQL master-slave synchronization pit avoidance version tutorial
Install MySQL on MAC
Oak-d raspberry pie cloud project [with detailed code]
各大框架都在使用的Unsafe类,到底有多神奇?
Knowledge points and examples of [seven input / output systems]
Summary according to classification in sail software
Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons
Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification