This is a creation in Article, where the information may have evolved or changed.
Brief introduction
- Test
Package testing provides support for automated testing of Go packages. It is intended to being used in concert with the ' Go Test ' command, which automates execution of any function of the form.
TestingThe package provides automated test support for GO packages. This is and
Go TestCommand echoes the function,
Go TestThe command executes automatically so that it conforms to the format
*testing
The function.
Functions of the form
*testing.B)
is considered benchmarks, and was executed by the ' Go Test ' command when >its-bench flag is provided. Benchmarks is run sequentially.
A function conforming to the format (see above) is considered a performance test program when carrying -bench= "." (Parameters must be there!) ) to execute the *go Test command, the performance test program is executed sequentially.
Simple instance Code
Lib.go
package libfuncintintint) { z = x + y return}typestruct { int ;}func (this * ForTest) Loops() { for i:=0 ; i < 10000 ; i++ { this.num++ }}
Lib_test.go
PackageLibImport "Testing"typeAddArraystruct{Resultint; Add_1int; Add_2int;}funcBenchmarkloops (b *testing. B) {varTest fortest ptr: = &test//Must be cycled B.N times. This number B.N will be adjusted in the run so that the appropriate time consumption can eventually be achieved. Convenient to calculate reasonable data. (Lest the data be all 0) forI=0; I<B.N; i++ {ptr. Loops ()}}//test concurrency EfficiencyfuncBenchmarkloopsparallel (b *testing. B) {B.runparallel (func(Pb *testing. PB) {varTest fortest ptr: = &test forPb. Next () {ptr. Loops ()}}}funcTestadd (T * testing. T) {varTest_data =[3] AddArray {{2,1,1}, {5,2,3}, {4,2,2}, } for_, V: =RangeTest_data {ifV.result! = Add (v.add_1, v.add_2) {T.errorf ("Add (%d,%d)! =%d \ n", V.add_1, V.add_2, V.result); } }}
Execute go test-bench= "." After the result:
// 表示测试全部通过>PASS // Benchmark 名字 - CPU 循环次数 平均每次执行时间 BenchmarkLoops-2 100000 20628 ns/op BenchmarkLoopsParallel-2 100000 10412 ns/op // 哪个目录下执行go test 累计耗时ok swap/lib 2.279s
Go Testing Library testing. T and testing. B Introduction
Testing. T
- Failed to determine interface
Fail
Failed to continue
FailNow
Failure termination
- Print Information interface
Log
Data flow ( cout
similar)
Logf
Format ( printf
similar)
SkipNow
Skip Current Test
Skiped
Detect if skipping
Integrated Interface Generation :
Error / Errorf
Report error continue [ Log / Logf + Fail
]
Fatel / Fatelf
Report error terminated [ Log / Logf + FailNow
]
Skip / Skipf
Report and Skip [ Log / Logf + SkipNow
]
Testing. B
- First, testing. B owns testing. all interfaces of T.
SetBytes( i uint64)
Count the memory consumption if you need to.
SetParallelism(p int)
Develop parallel numbers.
StartTimer / StopTimer / ResertTimer
Operation Timer
Testing. PB
Next()
Interface. Determine if the loop continues