This article is about the maintainability of unit test code. I don’t know if you’ve ever written a spaghetti-style unit test, which is structured like this. Frankly, I’ve written quite a few.
Most people write this for convenience: to initialize variables and to reuse them. But when the use case code is too long, and the single test happens to fail, it is difficult to find the specific reason, and it takes a lot of time to locate it when debugging.
Solutions
The Go community’s testing framework, has provided two more mature solutions.
Let’s look at the two separately.
GoConvey
|
|
The code, as above, is passable.
One of the special things about GoConvey is that it is executed in a tree, not from top to bottom. That is, it is a depth-first traversal and does not share variables, so that when When the integer is incremented
and When the integer is incremented again
are executed, the value of x is the value of 1 assigned by the upper level.
The above code is executed in the following order.
Given some integer...
->When the integer is incremented
->The value should be....
Given some integer...
->When the integer is incremented again
->The value should be....
testify assert suite
|
|
suite mainly through the following hook functions.
|
|
In this way, shared variables and destruction, etc. can be placed in the corresponding functions for processing, thus integrating a series of functions into a set of tests.
Summary
I personally prefer to use convey
, as long as you understand its tree-like execution pattern, you will find that the overall test code can be written much less and the structure is very clear. Through the tree organization, you can put test cases of the same topic in the same TestXXX
function, and then refine them in each Convey
function according to the conditions layer by layer, and finally pass in assertions through So
for verification.
This article does not talk about specific technical things, but mainly briefly introduces two unit testing frameworks, but the most important thing is to show that unit test code, also need to be valued code, but also need to be well organized code structure and use cases, unit tests are used to ensure the execution of the code itself, usually written after the frequency of change is not too high, if the use of spaghetti-style organization, after a long time It is very difficult to debug after a long time.
With the help of convey, you can subdivide the test case code into different functions without interfering with each other, which is very beneficial to maintainability.