This is a creation in Article, where the information may have evolved or changed.
The go language provides automated testing capabilities through the testing package. In-Package testing you can automatically run test functions that match the rules as long as you run the command go test.
Go Language Test Convention rules
1. General test func testxxx (*testing. T
Test line must start with test, XXX is string, first x must be uppercase [A-Z] Caption
In order to test the method and the readability of the method being tested, general xxx is the function name of the method being tested.
2. Performance test func benchmarkxxx (*testing. B
Performance test with benchmark mark, xxx ibid.
3. test File name conventions
Go language test file name convention rules must end in _test.go, placed under the same package, in order to facilitate code reading, the general go source file plus _test
such as source file My.go so test file if Your_test.go,her_test.go,my_test.go can, but the best is my_test.go, easy to read
example, source file My.go
Package MyFunc Add (x, y int) int {return x + y}
Create a My_test.go file that needs to be introduced testing
Package Myimport "Testing" func Testadd (t *testing. T) {If Add (1, 2)! = 3 {t.error ("test foo:addr failed")} else {T.Log ("Test foo:addr Pass")}}func Benchmarkadd (b *testing. B) {//If initialization is required, the more time-consuming operation can be this://B.stoptimer ()//.... A bunch of Operations//B.starttimer () for I: = 0; i < B.N; i++ {Add (1, 2)}}
Run test go, output:
PASS
OK github.com/my 0.010s
To run a performance test, execute the command
Go test-test.bench= ". *"
Output
PASS
Benchmarkadd 2000000000 0.72 Ns/op
OK github.com/my 1.528s
More test names, use Go help test
Go test is not very good very strong! ^_^
article source: http://blog.gcove.net/go%E8%AF%AD%E8%A8%80%E6%B5%8B%E8%AF%95test.html