LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package testing provides support for automated testing of Go packages. | 5 // Package testing provides support for automated testing of Go packages. |
6 // It is intended to be used in concert with the ``go test'' command, which auto
mates | 6 // It is intended to be used in concert with the ``go test'' command, which auto
mates |
7 // execution of any function of the form | 7 // execution of any function of the form |
8 // func TestXxx(*testing.T) | 8 // func TestXxx(*testing.T) |
9 // where Xxx can be any alphanumeric string (but the first letter must not be in | 9 // where Xxx can be any alphanumeric string (but the first letter must not be in |
10 // [a-z]) and serves to identify the test routine. | 10 // [a-z]) and serves to identify the test routine. |
11 // These TestXxx routines should be declared within the package they are testing
. | 11 // These TestXxx routines should be declared within the package they are testing
. |
12 // | 12 // |
13 // Functions of the form | 13 // Functions of the form |
14 // func BenchmarkXxx(*testing.B) | 14 // func BenchmarkXxx(*testing.B) |
15 // are considered benchmarks, and are executed by the "go test" command when | 15 // are considered benchmarks, and are executed by the "go test" command when |
16 // the -test.bench flag is provided. | 16 // the -test.bench flag is provided. Benchmarks are run sequentially. |
| 17 //· |
| 18 // For a description of the testing flags, see |
| 19 // http://golang.org/cmd/go/#Description_of_testing_flags. |
17 // | 20 // |
18 // A sample benchmark function looks like this: | 21 // A sample benchmark function looks like this: |
19 // func BenchmarkHello(b *testing.B) { | 22 // func BenchmarkHello(b *testing.B) { |
20 // for i := 0; i < b.N; i++ { | 23 // for i := 0; i < b.N; i++ { |
21 // fmt.Sprintf("hello") | 24 // fmt.Sprintf("hello") |
22 // } | 25 // } |
23 // } | 26 // } |
24 // | 27 // |
25 // The benchmark package will vary b.N until the benchmark function lasts | 28 // The benchmark package will vary b.N until the benchmark function lasts |
26 // long enough to be timed reliably. The output | 29 // long enough to be timed reliably. The output |
27 // testing.BenchmarkHello 10000000 282 ns/op | 30 // BenchmarkHello 10000000 282 ns/op |
28 // means that the loop ran 10000000 times at a speed of 282 ns per loop. | 31 // means that the loop ran 10000000 times at a speed of 282 ns per loop. |
29 // | 32 // |
30 // If a benchmark needs some expensive setup before running, the timer | 33 // If a benchmark needs some expensive setup before running, the timer |
31 // may be stopped: | 34 // may be reset: |
32 // func BenchmarkBigLen(b *testing.B) { | 35 // func BenchmarkBigLen(b *testing.B) { |
33 // b.StopTimer() | |
34 // big := NewBig() | 36 // big := NewBig() |
35 // b.StartTimer() | 37 // b.ResetTimer() |
36 // for i := 0; i < b.N; i++ { | 38 // for i := 0; i < b.N; i++ { |
37 // big.Len() | 39 // big.Len() |
38 // } | 40 // } |
39 // } | 41 // } |
40 // | 42 // |
41 // The package also runs and verifies example code. Example functions may | 43 // The package also runs and verifies example code. Example functions may |
42 // include a concluding comment that begins with "Output:" and is compared with | 44 // include a concluding comment that begins with "Output:" and is compared with |
43 // the standard output of the function when the tests are run, as in these | 45 // the standard output of the function when the tests are run, as in these |
44 // examples of an example: | 46 // examples of an example: |
45 // | 47 // |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 95 |
94 var ( | 96 var ( |
95 // The short flag requests that tests run more quickly, but its function
ality | 97 // The short flag requests that tests run more quickly, but its function
ality |
96 // is provided by test writers themselves. The testing package is just
its | 98 // is provided by test writers themselves. The testing package is just
its |
97 // home. The all.bash installation script sets it to make installation
more | 99 // home. The all.bash installation script sets it to make installation
more |
98 // efficient, but by default the flag is off so a plain "go test" will d
o a | 100 // efficient, but by default the flag is off so a plain "go test" will d
o a |
99 // full test of the package. | 101 // full test of the package. |
100 short = flag.Bool("test.short", false, "run smaller test suite to save t
ime") | 102 short = flag.Bool("test.short", false, "run smaller test suite to save t
ime") |
101 | 103 |
102 // Report as tests are run; default is silent for success. | 104 // Report as tests are run; default is silent for success. |
103 » chatty = flag.Bool("test.v", false, "verbose: print additiona
l output") | 105 » chatty = flag.Bool("test.v", false, "verbose: print additional
output") |
104 » match = flag.String("test.run", "", "regular expression to s
elect tests and examples to run") | 106 » match = flag.String("test.run", "", "regular expression to se
lect tests and examples to run") |
105 » memProfile = flag.String("test.memprofile", "", "write a memory p
rofile to the named file after execution") | 107 » memProfile = flag.String("test.memprofile", "", "write a memory pr
ofile to the named file after execution") |
106 » memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets run
time.MemProfileRate") | 108 » memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runt
ime.MemProfileRate") |
107 » cpuProfile = flag.String("test.cpuprofile", "", "write a cpu prof
ile to the named file during execution") | 109 » cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profi
le to the named file during execution") |
108 » contentionProfile = flag.String("test.cprofile", "", "write a contention
profile to the named file during execution") | 110 » blockProfile = flag.String("test.blockprofile", "", "write a gorouti
ne blocking profile to the named file after execution") |
109 » timeout = flag.Duration("test.timeout", 0, "if positive, sets
an aggregate time limit for all tests") | 111 » blockProfileRate = flag.Int("test.blockprofilerate", 1, "if >= 0, calls
runtime.SetBlockProfileRate()") |
110 » cpuListStr = flag.String("test.cpu", "", "comma-separated list of
number of CPUs to use for each test") | 112 » timeout = flag.Duration("test.timeout", 0, "if positive, sets a
n aggregate time limit for all tests") |
111 » parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "ma
ximum test parallelism") | 113 » cpuListStr = flag.String("test.cpu", "", "comma-separated list of
number of CPUs to use for each test") |
| 114 » parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "max
imum test parallelism") |
112 | 115 |
113 haveExamples bool // are there examples? | 116 haveExamples bool // are there examples? |
114 | 117 |
115 cpuList []int | 118 cpuList []int |
116 ) | 119 ) |
117 | 120 |
118 // common holds the elements common between T and B and | 121 // common holds the elements common between T and B and |
119 // captures common methods such as Errorf. | 122 // captures common methods such as Errorf. |
120 type common struct { | 123 type common struct { |
121 mu sync.RWMutex // guards output and failed | 124 mu sync.RWMutex // guards output and failed |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 fmt.Fprintf(os.Stderr, "testing: %s", err) | 415 fmt.Fprintf(os.Stderr, "testing: %s", err) |
413 return | 416 return |
414 } | 417 } |
415 if err := pprof.StartCPUProfile(f); err != nil { | 418 if err := pprof.StartCPUProfile(f); err != nil { |
416 fmt.Fprintf(os.Stderr, "testing: can't start cpu profile
: %s", err) | 419 fmt.Fprintf(os.Stderr, "testing: can't start cpu profile
: %s", err) |
417 f.Close() | 420 f.Close() |
418 return | 421 return |
419 } | 422 } |
420 // Could save f so after can call f.Close; not worth the effort. | 423 // Could save f so after can call f.Close; not worth the effort. |
421 } | 424 } |
422 » if *contentionProfile != "" { | 425 » if *blockProfile != "" && *blockProfileRate >= 0 { |
423 » » runtime.ContentionProfileEnabled = true | 426 » » runtime.SetBlockProfileRate(*blockProfileRate) |
424 } | 427 } |
425 } | 428 } |
426 | 429 |
427 // after runs after all testing. | 430 // after runs after all testing. |
428 func after() { | 431 func after() { |
429 if *cpuProfile != "" { | 432 if *cpuProfile != "" { |
430 pprof.StopCPUProfile() // flushes profile to disk | 433 pprof.StopCPUProfile() // flushes profile to disk |
431 } | 434 } |
432 if *memProfile != "" { | 435 if *memProfile != "" { |
433 f, err := os.Create(*memProfile) | 436 f, err := os.Create(*memProfile) |
434 if err != nil { | 437 if err != nil { |
435 fmt.Fprintf(os.Stderr, "testing: %s", err) | 438 fmt.Fprintf(os.Stderr, "testing: %s", err) |
436 return | 439 return |
437 } | 440 } |
438 if err = pprof.WriteHeapProfile(f); err != nil { | 441 if err = pprof.WriteHeapProfile(f); err != nil { |
439 fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *m
emProfile, err) | 442 fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *m
emProfile, err) |
440 } | 443 } |
441 f.Close() | 444 f.Close() |
442 } | 445 } |
443 » if *contentionProfile != "" { | 446 » if *blockProfile != "" && *blockProfileRate >= 0 { |
444 » » f, err := os.Create(*contentionProfile) | 447 » » f, err := os.Create(*blockProfile) |
445 if err != nil { | 448 if err != nil { |
446 fmt.Fprintf(os.Stderr, "testing: %s", err) | 449 fmt.Fprintf(os.Stderr, "testing: %s", err) |
447 return | 450 return |
448 } | 451 } |
449 » » if err = pprof.WriteContention(f); err != nil { | 452 » » if err = pprof.Lookup("block").WriteTo(f, 0); err != nil { |
450 » » » fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *c
ontentionProfile, err) | 453 » » » fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *b
lockProfile, err) |
451 } | 454 } |
452 f.Close() | 455 f.Close() |
453 } | 456 } |
454 } | 457 } |
455 | 458 |
456 var timer *time.Timer | 459 var timer *time.Timer |
457 | 460 |
458 // startAlarm starts an alarm if requested. | 461 // startAlarm starts an alarm if requested. |
459 func startAlarm() { | 462 func startAlarm() { |
460 if *timeout > 0 { | 463 if *timeout > 0 { |
(...skipping 20 matching lines...) Expand all Loading... |
481 for _, val := range strings.Split(*cpuListStr, ",") { | 484 for _, val := range strings.Split(*cpuListStr, ",") { |
482 cpu, err := strconv.Atoi(val) | 485 cpu, err := strconv.Atoi(val) |
483 if err != nil || cpu <= 0 { | 486 if err != nil || cpu <= 0 { |
484 fmt.Fprintf(os.Stderr, "testing: invalid value %
q for -test.cpu", val) | 487 fmt.Fprintf(os.Stderr, "testing: invalid value %
q for -test.cpu", val) |
485 os.Exit(1) | 488 os.Exit(1) |
486 } | 489 } |
487 cpuList = append(cpuList, cpu) | 490 cpuList = append(cpuList, cpu) |
488 } | 491 } |
489 } | 492 } |
490 } | 493 } |
LEFT | RIGHT |