Brief introduction of golang unit testing

Time:2020-10-19

There is built-in support for unit testing in golang. It is not necessary to introduce a third-party jar like Java to carry out testing. The following will introduce several tests supported by golang;

1、 Test type

There are three kinds of unit tests in golang: function test, benchmark test, sample test or sample function;
   the function test must appear with the function name of testXXX, the benchmark test must appear with the function name of benchmarkxxx, and the example function must appear with the function name of examplexxx.

//Function test
func TestXXX(t *testing.T){
}

//Benchmarking
func BenchmarkXXX(b * testing.B){
}

//Sample function
func ExampleXXX(){
}

2、 Test function

   function name: testxx, function test function prefixed with test
   parameter type: * testing. T

Function function:

func(a, b int) Mul {
  return b * b
}

Test function:

func TestMul(t *testing.T) {
arr := [...]int{2, 4, 6}
    expected := []int{8, 12}
for j := 0; j < 2; j++ {
       result := Add(arr[j], arr[j+1])
	   if result != expected[j] {
		 //Stop post order logic after failure
		  t.Fatalf("input is %d, the expected is %d, the actual %d", arr[j], expected[j], result)
		 //Continue with post order logic after failure
		//t.Errorf("input is %d, the expected is %d, the actual %d", arr[j], expected[j], result)
	   }
    }
}

2、 Benchmarking

   used for quantitative and comparative performance test of program functions;
   function name: benchmark XX, test function prefixed with test
   parameter type: * testing. B

func BenchmarkMul(b *testing.B) {
for i := 0; i < b.N; i++ {
	Mul(i, i)
    }
}

Implementation results:

goos: windows
goarch: amd64
pkg: solinx.co/LCache/test
BenchmarkMul-12    	1000000000	         0.323 ns/op
PASS
coverage: 100.0% of statements
ok      solinx.co/LCache/test   0.499s

The results were analyzed
Execution environment: Windows
Architecture: AMD64
Package path
Benchmarkmul-12: 12 logical CPUs in total
The execution time of the function is 0.499 seconds
Test code coverage: 100%

3、 Sample test

func ExampleMul() {
a := Mul(2, 2)
    fmt.Println(a)
//Output: 41
}

When the go test is executed, it will monitor whether the actual output is consistent with the expected result in the annotation. If the test is consistent, the test will pass, otherwise the test will fail;

Through:

Failed:

=== RUN   ExampleMul
--- FAIL: ExampleMul (0.00s)
got:
4
want:
41
FAIL
coverage: 100.0% of statements

Got: 4, what is needed is: 41 test failure;

4、 Introduction to go test parameter

                  
                  
                    
                – benchtime
                  
                 
                 
                  

Execute the test function that matches the test

go test -run Test

First article address: solinx
https://mp.weixin.qq.com/s/nqnXiOT_CfD6qWeE6xsrhw

Recommended Today

asp.net Application of regular expression

1. Balanced group / recursive matching (?’ Group ‘), which is called the corresponding content of group, and counts it on the stack;(?’- Group ‘), and count the corresponding content named group out of the stack(?!) Zero width negative look ahead assertion. Since there is no suffix expression, attempts to match always failRegular example:,{0,1}”5″:\[[^\[\]]*(((?’Open’\[)[^\[\]]*)+((?’-Open’\])[^\[\]]*)+)*(?(Open)(?!))\],{0,1} Test […]