1 // Package testing provides support for automated testing of Go ...
27 // The short flag requests that tests run more quickly, but its functionality
28 // is provided by test writers themselves. The testing package is just its
29 // home. The all.bash installation script sets it to make installation more
30 // efficient, but by default the flag is off so a plain "go test" will do a
31 // full test of the package.
32 short = flag.Bool("test.short", false, "run smaller test suite to save time")
34 // Report as tests are run; default is silent for success.
35 chatty = flag.Bool("test.v", false, "verbose: print additional output")
36 match = flag.String("test.run", "", "regular expression to select tests to run")
37 memProfile = flag.String("test.memprofile", "", "write a memory profile to the named file after execution")
38 memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runtime.MemProfileRate")
39 cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution")
40 timeout = flag.Duration("test.timeout", 0, "if positive, sets an aggregate time limit for all tests")
41 cpuListStr = flag.String("test.cpu", "", "comma-separated list of number of CPUs to use for each test")
42 parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "maximum test parallelism")
48 var benchTime = flag.Duration("test.benchtime", 1*time.Second, "approximate run time for each benchmark")
51 var matchBenchmarks = flag.String("test.bench", "", "regular expression to select benchmarks to run")
58 // An internal function but exported because it is cross-package; ...
59 func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample)
61 // An internal function but exported because it is cross-package; ...
62 func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark)
65 func RunExamples(examples []InternalExample) (ok bool)
68 func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool)
70 // Short reports whether the -test.short flag is set.
73 // after runs after all testing.
76 // alarm is called if the timeout expires.
79 // before runs before all testing.
82 // decorate inserts the final newline if needed and indentation ...
83 func decorate(s string, addFileLine bool) string
86 func max(x, y int) int
89 func min(x, y int) int
94 // roundDown10 rounds a number down to the nearest power of 10.
95 func roundDown10(n int) int
97 // roundUp rounds x up to a number of the form [1eX, 2eX, 5eX].
98 func roundUp(n int) int
100 // startAlarm starts an alarm if requested.
103 // stopAlarm turns off the alarm.
107 func tRunner(t *T, test *InternalTest)
111 // B is a type passed to Benchmark functions to manage benchmark ...
115 benchmark InternalBenchmark
118 result BenchmarkResult
121 // Error is equivalent to Log() followed by Fail().
122 func (c *B) Error(args ...interface{})
124 // Errorf is equivalent to Logf() followed by Fail().
125 func (c *B) Errorf(format string, args ...interface{})
127 // Fail marks the function as having failed but continues ...
130 // FailNow marks the function as having failed and stops its ...
131 func (c *B) FailNow()
133 // Failed reports whether the function has failed.
134 func (c *B) Failed() bool
136 // Fatal is equivalent to Log() followed by FailNow().
137 func (c *B) Fatal(args ...interface{})
139 // Fatalf is equivalent to Logf() followed by FailNow().
140 func (c *B) Fatalf(format string, args ...interface{})
142 // Log formats its arguments using default formatting, analogous ...
143 func (c *B) Log(args ...interface{})
145 // Logf formats its arguments according to the format, analogous ...
146 func (c *B) Logf(format string, args ...interface{})
148 // ResetTimer sets the elapsed benchmark time to zero. It does not ...
149 func (b *B) ResetTimer()
151 // SetBytes records the number of bytes processed in a single ...
152 func (b *B) SetBytes(n int64)
154 // StartTimer starts timing a test. This function is called ...
155 func (b *B) StartTimer()
157 // StopTimer stops timing a test. This can be used to pause the ...
158 func (b *B) StopTimer()
160 // launch launches the benchmark function. It gradually increases ...
163 // log generates the output. It's always at the same stack depth.
164 func (c *B) log(s string)
167 func (b *B) nsPerOp() int64
169 // run times the benchmark function in a separate goroutine.
170 func (b *B) run() BenchmarkResult
172 // runN runs a single benchmark for the specified number of ...
173 func (b *B) runN(n int)
175 // trimOutput shortens the output from a benchmark, which can be ...
176 func (b *B) trimOutput()
178 // The results of a benchmark run.
179 type BenchmarkResult struct {
180 N int // The number of iterations.
181 T time.Duration // The total time taken.
182 Bytes int64 // Bytes processed in one iteration.
185 // Benchmark benchmarks a single function. Useful for creating ...
186 func Benchmark(f func(b *B)) BenchmarkResult
189 func (r BenchmarkResult) NsPerOp() int64
192 func (r BenchmarkResult) String() string
195 func (r BenchmarkResult) mbPerSec() float64
197 // An internal type but exported because it is cross-package; part ...
198 type InternalBenchmark struct {
204 type InternalExample struct {
210 // An internal type but exported because it is cross-package; part ...
211 type InternalTest struct {
216 // T is a type passed to Test functions to manage test state and ...
219 name string // Name of test.
220 startParallel chan bool // Parallel tests will wait on this.
223 // Error is equivalent to Log() followed by Fail().
224 func (c *T) Error(args ...interface{})
226 // Errorf is equivalent to Logf() followed by Fail().
227 func (c *T) Errorf(format string, args ...interface{})
229 // Fail marks the function as having failed but continues ...
232 // FailNow marks the function as having failed and stops its ...
233 func (c *T) FailNow()
235 // Failed reports whether the function has failed.
236 func (c *T) Failed() bool
238 // Fatal is equivalent to Log() followed by FailNow().
239 func (c *T) Fatal(args ...interface{})
241 // Fatalf is equivalent to Logf() followed by FailNow().
242 func (c *T) Fatalf(format string, args ...interface{})
244 // Log formats its arguments using default formatting, analogous ...
245 func (c *T) Log(args ...interface{})
247 // Logf formats its arguments according to the format, analogous ...
248 func (c *T) Logf(format string, args ...interface{})
250 // Parallel signals that this test is to be run in parallel with ...
251 func (t *T) Parallel()
253 // log generates the output. It's always at the same stack depth.
254 func (c *T) log(s string)
259 // common holds the elements common between T and B and captures ...
261 output []byte // Output generated by test or benchmark.
262 failed bool // Test or benchmark has failed.
263 start time.Time // Time test or benchmark started
264 duration time.Duration
265 self interface{} // To be sent on signal channel when done.
266 signal chan interface{} // Output for serial tests.
269 // Error is equivalent to Log() followed by Fail().
270 func (c *common) Error(args ...interface{})
272 // Errorf is equivalent to Logf() followed by Fail().
273 func (c *common) Errorf(format string, args ...interface{})
275 // Fail marks the function as having failed but continues ...
276 func (c *common) Fail()
278 // FailNow marks the function as having failed and stops its ...
279 func (c *common) FailNow()
281 // Failed reports whether the function has failed.
282 func (c *common) Failed() bool
284 // Fatal is equivalent to Log() followed by FailNow().
285 func (c *common) Fatal(args ...interface{})
287 // Fatalf is equivalent to Logf() followed by FailNow().
288 func (c *common) Fatalf(format string, args ...interface{})
290 // Log formats its arguments using default formatting, analogous ...
291 func (c *common) Log(args ...interface{})
293 // Logf formats its arguments according to the format, analogous ...
294 func (c *common) Logf(format string, args ...interface{})
296 // log generates the output. It's always at the same stack depth.
297 func (c *common) log(s string)