In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use stub and mock in the Go language to achieve unit testing. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
1. Mock example:
What mock does is to create a struct, then implement the functions in the required interface, and when testing, replace the called interface with the struct from the test mock.
The replacement used by mock is to replace the entire interface (Note: the interface can be an input parameter or a variable in struct, but the replacement needs to be completed before testing).
Examples are as follows:
GetType function is originally based on different input parameters yu, different operations, addition, subtraction and assignment to zero.
After mock, specifying the GetType type does only one operation "addition".
Output:
As you can see from the output, there are three operations before mock, and after mock replacement, it becomes an operational addition.
2. Stub example:
If you use stub for unit testing, the code is intrusive, and the corresponding function needs to be made into a callback function, so that subsequent test code can replace this function with the function you want to specify.
The example is as follows: (note: this example is relatively simple, just to demonstrate the use of stub. )
GetType function is originally based on different input parameters, many different operations, addition, subtraction and assignment to zero.
After Stub, specifying the GetType type does only one operation addition.
Output:
Before piling: the function function will do different operations according to the values of the input parameters, and output different results, which are: 0; addition: 2: 5 = 7; subtraction: 5-1 = 4.
After piling: the pile function StubFunc returns only one operation "addition", so no matter what the type of input parameter is, the test case is operated according to addition, namely: 1: 3 = 4; 2: 5 = 7; 5: 1 = 6
III. Comparison between mock and stub
1) both mock and stub are implemented by substitution, with dependencies in the functions being tested, but mock uses interface substitution and stub uses function substitution.
2) the implementation of mock is not intrusive to the functional code, and stub is more intrusive. When implementing functional functions, you need to set some callback functions for testing, which is the so-called pile here.
3) for the alternative control method, if mock wants to support different outputs, it needs to implement different branch code in advance, and even needs to define different mock structures to implement. This mock code will become a maximum set that supports all logical branches, and the mock code complexity will become higher. Stub is good at controlling different branches of stub functions, because stub replaces functions, so as long as you need to use this output again, define a function, and this function can even be anonymous.
Based on the above analysis, we can design a way to combine stub and mock, as shown in the following example:
The code under test is consistent with that in the mock example. Two variables are added to the mock code: callback function and whether to use callback function. In the test case, the output of pile function is controlled by implementing different stub functions.
Output:
From the results of the test case run, when the stub function is "addition", the first two use cases will perform the "addition" operation; when the stub is "subtraction", the last use case will perform the "subtraction" operation.
The above is how to use stub and mock to achieve unit testing in the Go language. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.