Recently has been engaged in various tests, and then this weekend to turn over the book, found that the book on the carrot on the test is written in a very full.
This book is before the CU (chinaunix.net) forum. Activities to get prizes (as well as the author's autograph), brought back have not read.
Because it was written too thin and thick.
Referring to his book and the official website of the document, and then the test aspects of the thing is still a bit of meaning.
This article mainly tells these points:
I. Several different forms of testing
Functional testing:
Testxxxx (t *testing. T
Benchmark test:
Benchmarkxxxx (b *testing. B
Sample test:
Example_xxx ()
The main function for testing:
Testmain (M *testing. M
//
Func Testmain (M *testing. M) {
Flag. Parse ()
Os. Exit (M.run ())
// }
Two. Resource collection in the test
Three. Test coverage
I. Several different forms of testing
Functional Testing:
Testxxxx (t *testing. T
Biased to test the specific source code in a source file function implementation.
Basic is in the same package location.
Go test to run.
Run all the tests under the package
Go test-v prj/pkg
Just test the function test function that matches the name of the package.
Go test-v-run=xxfunc prj/pkg
Control test Run time:
-timeout: Timeout control
Go test-timeout 100ms prj/pkg//can represent time with 1h20s
(Time unit: H/M min/s/ms msec/us μs/ns nanoseconds)
-short: Whether to shorten the test time
You can add this if in the test code.
If testing. Short () {
}else{
}
Then add the-short parameter to the Run command, such as:
Go Test-short prj/pkg
concurrency control:
The test function is not executed concurrently by default. If you want to execute concurrently. In addition to setting Gomaxprocs,
Runtime. Gomaxprocs (runtime. NUMCPU ())
Also start with the function you want to test, plus
T.parallel ()
Func testparallel (t *testing. T) {
T.parallel ()
//......
}
Go Test-parallel 8
Benchmark test:
Benchmarkxxxx (b *testing. B
Run:
Go Test benxxx_test.go-bench= "."
Go Test-bench=xxfunc
Go Test-bench=xxfunc prj/pkg
Go's benchmark default is to execute this function as much as possible in 1s of time. The length can be extended by-benchtime this parameter.
Go Test benxxx_test.go-benchtime=2s
The relevant source code in the standard package:
var benchtime = flag. Duration ("Test.benchtime", 1*time. Second, "Approximate run time for each benchmark")
//Run the benchmark for at least the specified amount of time.
d: = *benchtime for
!b.failed && b.duration < D && N < 1e9 {
Example of a test function template:
Func benchmarkxxxx (b *testing. B) {
var (
//Initialize
)
B.stoptimer ()//stop test timing, because the default is to turn on timing.
//Some actions that do not need to be included in the test time
//...
B.starttimer ()//Start test timing
////////////////////////////////
// Continue to Run tests
//such as: for i:=0;i<b.n;i++{//
...
}
//If there is a reset count time requirement
//Resttimer () resets the previously accumulated function execution time
to 0//continue to run the test
// ........
}
The difference between Benchmark and functional testing is that it focuses more on metrics such as performance.
Example:
Package main
Import (
"FMT"
"testing"
)
func benchmarkxxxx (b *testing. B) {for
I: = 0; i < B.N; i++ {
fmt. Println ("I:", i)
}
}
Test results:
30000 50806 Ns/op
OK _/e_/gotest/testing/bentest 2.179s
30000: Total run times
50806 ns/op: Average run time, averaging 50806 nanoseconds
You can also add t.setbytes (1111111) to get multiple MB of data per file system
/* Benchmark Example AUTHOR:XCL date:2015-11-22 */Package main import ("FMT" "Math/big" "testing") Func benchmarkxxxx (b *testing. B) {for i: = 0; i < B.N; i++ {fmt. Sprintf ("Hello%d", I)}} func Benchmarkbiglen (b *testing). B) {big: = Newbig () B.resettimer () for I: = 0; i < B.N; i++ {big. Bitlen ()}} func Newbig () *big. Int {x: = new (big. INT) X.mulrange (1, ten) return x}/*//Test Result: E:\gotest\testing\bentest>go test b_test.go-bench=. -benchmem-cpu=1,2,4,8 testing:warning:no tests to run PASS benchmarkxxxx 5000000 341 ns/op 2 b/op allocs/op BenchmarkXxxx-2 5000000 323 ns/op b/op 2 allocs/op BenchmarkXxxx-4 5000000 332 ns/op b/op 2 allocs/op Benchma RkXxxx-8 5000000 ns/op b/op 2 allocs/op Benchmarkbiglen 200000 9.85 Ns/op 0 B/op 0 allocs/op BenchmarkBigLen-2 200000000 9.86 ns/op 0 b/op 0 Allocs /op BenchmarkBigLen-4 100000000 10.0 ns/op 0 b/op 0 allocs/op BenchmarkBigLen-8
200000000 9.82 ns/op 0 b/op 0 allocs/op OK command-line-arguments 18.085s */
Sample test:
Example_xxx ()
To see if the output is the same as expected in the run
Func examplexxxx ()
Example:
Func Examplehello () {
Fmt. Println ("Hello")
Output:hello
}
Just below, add a line "//Output: Expected results" to compare the results.
It has some rules for sample function commands:
When the object being tested is a function:
Func Example () {...}//The object being tested is the entire package
Func Examplef () {...} The object being tested is a function
Func Examplet () {...} The object being tested is a type
Func examplet_m () {...}//The object being tested is a function of a type
After naming according to the preceding rules, you can also add suffixes in the following example to differentiate
Func Example_suffix () {...}
Func Examplef_suffix () {...}
Func Examplet_suffix () {...}
Func Examplet_m_suffix () {...}
two. Resource collection in the test
In the test run, you can add some parameters to collect monitoring resource usage. Then follow the tools provided by go for analysis.
-cpuprofile cpu.out//10 milliseconds per default, CPU usage
-memprofile mem.out//The allocation of heap memory during program operation
-memprofilerate N//memory allocation behavior, default per 512k bytes allocated, sampled once
-blockprofile block.out//record Goroutine blocking events
-blockprofilerate N//control record the number of nanoseconds goroutine blocking. Not set by default
It's the equivalent of-test.blockprofilerate=1, and every nanosecond has a record.
Take Cpuprofile as an example:
E:\gotest\testing\bentest>go Test b_test.go-bench=. -benchmem-cpu=1,2,4,8-cpuprofile cpu.out testing:warning:no tests to run PASS benchmarkxxxx 5000000 351 ns/op b/op 2 allocs/op BenchmarkXxxx-2 5000000 326 ns/op 2 b/op allocs/op BenchmarkXxxx-4 5000000 326 ns/op b/op 2 allocs/op BenchmarkXxxx-8 5000000 332 ns/op b/op 2 allocs/op Benchmark Biglen 200000000 9.91 ns/op 0 b/op 0 allocs/op BenchmarkBigLen-2 20000000 0 9.84 ns/op 0 b/op 0 allocs/op BenchmarkBigLen-4 100000000 10.2 NS /op 0 b/op 0 allocs/op BenchmarkBigLen-8 100000000 10.2 ns/op 0 B/op 0 allocs/op OK command-line-arguments 16.231s e:\gotest\testing\bentest>d the volume in IR drive E is the serial number of the doc volume is the 0e3d-2a1f E:\GOtest\testing\bentest directory 2015/11/22 11:59 <DIR>.
2015/11/22 11:59 <DIR>. 2015/11/22 11:44 1,423 b_test.go 2015/11/22 11:59 63,024 cpu.out 2015/11/22 11:59 3,932, Main.test.exe 3 files 3,996,607 bytes 2 directories 15,239,778,304 Available bytes
Note that an executable file called "Main.test.exe" and an output file of Cpu.out are generated.
They can be used for analysis.
Go tool pprof Main.test.exe cpu.out
Check running, CPU usage e:\gotest\testing\bentest>go tool pprof Main.test.exe cpu.out Entering interactive mode (type "Help" f
or commands) (pprof) Top10 12230ms of 16500ms Total (74.12%) Dropped nodes (cum <= 82.50ms) Showing top nodes out of (cum >= 1890ms) flat flat% sum% cum cum% 2910ms 17.64% 17.64% 4330ms 26.24% math/big.nat.bitlen 2850ms 17.27% 34.91% 7180ms 43.52% math/b Ig. (*int). Bitlen 1420ms 8.61% 43.52% 1420ms 8.61% math/big.bitlen 1090ms 6.61% 50.12% 1250ms 7.58% runtime.mal LOCGC 1020ms 6.18% 56.30% 3510ms 21.27% fmt. (*PP). doprintf 840ms 5.09% 61.39% 8020ms 48.61% command-line-arguments. Benchmarkbiglen 820ms 4.97% 66.36% 1090ms 6.61% fmt. (*fmt). Integer 550ms 3.33% 69.70% 550ms 3.33% runtime.memmove 400ms 2.42% 72.12% 400ms 2.42% Run Time.mSpan_Sweep.func1 330ms 2.00% 74.12% 1890ms 11.45% fmt.
(*PP). Printarg (Pprof) (pprof) help Commands: CMD [n] [--cum] [focus_regex]* [-ignore_regex]* produce a text report with the top N entries.
Include samples matching Focus_regex, and exclude Ignore_regex.
Add--cum to sort using cumulative data. Available Commands:callgrind Outputs A graph in callgrind format disasm Output annotated Assem Bly for functions matching regexp or address dot Outputs a graph in dot format EOG Vis Ualize graph through EOG evince visualize graph through evince gif Outputs a graph image In GIF format GV visualize graph through GV List Output annotated source for functions Matching regexp PDF Outputs a graph in PDF format peek Output callers/callees of Funct Ions matching regexp png Outputs a graph image in PNG format Proto Outputs the profile I
N Compressed protobuf formatPS Outputs a graph in PS format raw Outputs A text representation of the raw profile SVG Outputs a graph in SVG format tags Outputs all tags on the profile text out Puts top entries in text form top Outputs top entries in text form tree Outputs a text Rendering of call graph Web visualize graph through web browser weblist Output annotated s Ource in HTML for functions matching regexp or address peek Func_regex Display callers and callees of functions
Matching Func_regex.
......... (pprof) weblist (pprof) Web cannot find dot, has you installed Graphviz?Download and install a Graphviz, you can figure out.
The main function for testing:
Testmain (M *testing. M
Here's an example:
/* Testmain Example AUTHOR:XCL date:2015-11-22 */Package main import ("Flag" "Log" "OS" "testing") var WORDPTR = flag . String ("word", "foo", "a string") Func Testmain (M *testing. M) {flag. Parse () log. Println ("[Testmain] Word:", *wordptr) log. Println ("[Testmain] Run () front") Exitval: = M.run () log. Println ("[Testmain] Run ()") OS. Exit (Exitval)} func Test1 (t *testing. T) {log. Println ("[Test1] Running", *wordptr)}/* E:\gotest\testing\t2>go test t2_test.go-v-trace trace.out-word xcl ....
..
2015/11/22 18:11:43 [Testmain] word:xcl ... 2015/11/22 18:11:43 [Testmain] Run () before = = = Run Test1 2015/11/22 18:11:43 [Test1] running xcl ...---pass:test1 (0.00 s) PASS 2015/11/22 18:11:43 [Testmain] Run () OK command-line-arguments 0.101s E:\gotest\testing\t2>dir Drive E
The volume is the serial number of the doc volume is the 0e3d-2a1f E:\GOtest\testing\t2 directory 2015/11/22 18:11 <DIR>.
2015/11/22 18:11 <DIR>. 2015/11/22 18:11 3,666,944 Main.test.exe 2015/11/22 18:11 2,234 t2_test.go 2015/11/22 18:11 730 trace.out 3 Files 3,669 , 908 bytes 2 directories 15,177,601,024 free bytes E:\gotest\testing\t2>go tool trace Main.test.exe trace.out */
three. Test coverage:
Coverage refers to how much of the code being tested has been used in a test that has just been tested.
Go test-cover prj/pkg
Go Test PRJ/PKG-COVERPKG=PKG1,PKG2
Go Test prj/pkg-coverpkg=pkg1,pkg2-coverprofile=cover.out
Go Tool cover-html=cover.out
-func=cover.out//Output Test coverage summary information for each function
-html=cover.out//Translate profile contents into HTML and browse
-mode=cover.out//Design Profile compilation mode
-o=cover.out//...
-var=gocover//...
Reference: https://golang.org/pkg/testing/
https://golang.org/cmd/go/#hdr-description_of_testing_flags
Https://github.com/polaris1119/The-Golang-Standard-Library-by-Example/blob/master/chapter09/09.1.md
It <<go language concurrent programming Combat >> Test Chapter
In fact, the details of the relevant information. In addition, the go source package also has a lot of detailed examples.
First of all to this, http/websocket, such as how to test and performance optimization of another write.
blog:http://blog.csdn.ent/xcl168