Only sacrifice more ambition, dare to call the sun and moon in a new day
The weather yesterday was so changeable that it was sunny in the morning and rainy in the afternoon, unlike the city with only a handful of rainy days a year it had become familiar with. However, no matter how bad the weather, it seemed that the participants in the square could not quench the enthusiasm of the centennial ceremony, Yu Yourong Yan.
Format of unit tests
If, for various reasons, we need to conduct unit tests against a part of our code, for example, the code to be tested is written in XXX.go, then our test file should be named xxx_test.go, the test method should start with TestXxx, and the input parameter type should be one or only one. T, the benchmark has one and only one parameter type, which is *testing.B, and TestMain, which is similar to the hook function, has a parameter type of *testing.
1. Basic tests
A basic test case is a generic test case, and here is an example using a generic basic test.
//demo.go package main func Add(a int, b int) int { return a + b } //demo_test.go package main import "testing" func TestAdd(t *testing.T) { if res := Add(1, 2); res ! = 3 { t.Errorf("1 + 2 should be 3, but got %d", res) } }Copy the code
2. Sub-tests
The so-called sub-test is to share the related conditions of this function in a basic test case, and write a test case separately for various possible situations, without writing a new function to test.
func TestAdd(t *testing.T) { if res := Add(1, 2); res ! If (1, 2) {t. run ("test1", func(t * test.t) {if Add(1, 2)! = 3 { t.Fatal("fail") } }) t.Run("test2", func(t *testing.T) { if Add(1, 2) ! = 4 { t.Fatal("fail") } }) }Copy the code
3. Benchmarking
Sometimes there are certain requirements for the efficiency of some code execution, we may use the unit test method of benchmark test. Different from the common test method, the function name format is BenchmarkXxx, and the parameter type is * testing.b.
func BenchmarkAdd(b *testing.B) { for i := 0; i < b.N; I++ {FMT.Sprintf("hello")}} goos: Linux goarch: amd64 PKG: code/test Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz BenchmarkAdd BenchmarkAdd-12 26346091 46.98 ns/op PASSCopy the code
4. Hook function tests
In some cases, we need to initialize some related resources before executing the related unit tests. The latter is to execute some programs after the completion of the execution, which acts as a hook function. In this case, we need to use the unit tests with the parameter type * testing.m.
Func setup() {fmt.println (" start testing......" )} func teardown() {fmt.println (" end test......" ) } func TestAdd(t *testing.T) { if res := Add(1, 2); res ! = 3 { t.Errorf("1+2 should be 3 but got %d", Res)}} func TestMain(m * testing.m) {setup() code := m.run () teardown() os.exit (code)} // The result of the execution is to start the test...... === RUN TestAdd -- -- PASS: TestAdd (0.00s) PASS End the test......Copy the code
Unit test command
Go is a systematic language. It can be called a systematic language, and command line support is a must, so unit testing commands need to be mastered as well.
// Run all unit tests under the current package go test // Run the results of all unit tests go test -v // View the unit test case coverage go test-cover // Run the specified unit test case go test-run TestXxx -vCopy the code
summary
The content of go unit test is probably summarized in these, there may be no design, the subsequent supplement according to the situation, I hope the knowledge points of unit test can help people in need.