Package testing
import "testing"
Package testing provides support for automated testing of Go packages. It is intended to be used in concert with the “gotest” utility, which automates execution of any function of the form
func TestXxx(*testing.T)
where Xxx can be any alphanumeric string (but the first letter must not be in [a-z]) and serves to identify the test routine. These TestXxx routines should be declared within the package they are testing.
Functions of the form
func BenchmarkXxx(*testing.B)
are considered benchmarks, and are executed by gotest when the -test.bench flag is provided.
A sample benchmark function looks like this:
func BenchmarkHello(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf("hello")
}
}
The benchmark package will vary b.N until the benchmark function lasts long enough to be timed reliably. The output
testing.BenchmarkHello 500000 4076 ns/op
means that the loop ran 500000 times at a speed of 4076 ns per loop.
If a benchmark needs some expensive setup before running, the timer may be stopped:
func BenchmarkBigLen(b *testing.B) {
b.StopTimer()
big := NewBig()
b.StartTimer()
for i := 0; i < b.N; i++ {
big.Len()
}
}
Package files
benchmark.go testing.gofunc Main
func Main(matchString func(pat, str string) (bool, os.Error), tests []InternalTest, benchmarks []InternalBenchmark)
An internal function but exported because it is cross-package; part of the implementation of gotest.
func RunBenchmarks
func RunBenchmarks(matchString func(pat, str string) (bool, os.Error), benchmarks []InternalBenchmark)
An internal function but exported because it is cross-package; part of the implementation of gotest.
func RunTests
func RunTests(matchString func(pat, str string) (bool, os.Error), tests []InternalTest)
func Short
func Short() bool
Short reports whether the -test.short flag is set.
type B
B is a type passed to Benchmark functions to manage benchmark timing and to specify the number of iterations to run.
type B struct {
N int
// contains filtered or unexported fields
}
func (*B) ResetTimer
func (b *B) ResetTimer()
ResetTimer sets the elapsed benchmark time to zero. It does not affect whether the timer is running.
func (*B) SetBytes
func (b *B) SetBytes(n int64)
SetBytes records the number of bytes processed in a single operation. If this is called, the benchmark will report ns/op and MB/s.
func (*B) StartTimer
func (b *B) StartTimer()
StartTimer starts timing a test. This function is called automatically before a benchmark starts, but it can also used to resume timing after a call to StopTimer.
func (*B) StopTimer
func (b *B) StopTimer()
StopTimer stops timing a test. This can be used to pause the timer while performing complex initialization that you don't want to measure.
type BenchmarkResult
The results of a benchmark run.
type BenchmarkResult struct {
N int // The number of iterations.
Ns int64 // The total time taken.
Bytes int64 // Bytes processed in one iteration.
}
func Benchmark
func Benchmark(f func(b *B)) BenchmarkResult
Benchmark benchmarks a single function. Useful for creating custom benchmarks that do not use gotest.
func (BenchmarkResult) NsPerOp
func (r BenchmarkResult) NsPerOp() int64
func (BenchmarkResult) String
func (r BenchmarkResult) String() string
type InternalBenchmark
An internal type but exported because it is cross-package; part of the implementation of gotest.
type InternalBenchmark struct {
Name string
F func(b *B)
}
type InternalTest
An internal type but exported because it is cross-package; part of the implementation of gotest.
type InternalTest struct {
Name string
F func(*T)
}
type T
T is a type passed to Test functions to manage test state and support formatted test logs. Logs are accumulated during execution and dumped to standard error when done.
type T struct {
// contains filtered or unexported fields
}
func (*T) Error
func (t *T) Error(args ...interface{})
Error is equivalent to Log() followed by Fail().
func (*T) Errorf
func (t *T) Errorf(format string, args ...interface{})
Errorf is equivalent to Logf() followed by Fail().
func (*T) Fail
func (t *T) Fail()
Fail marks the Test function as having failed but continues execution.
func (*T) FailNow
func (t *T) FailNow()
FailNow marks the Test function as having failed and stops its execution. Execution will continue at the next Test.
func (*T) Failed
func (t *T) Failed() bool
Failed returns whether the Test function has failed.
func (*T) Fatal
func (t *T) Fatal(args ...interface{})
Fatal is equivalent to Log() followed by FailNow().
func (*T) Fatalf
func (t *T) Fatalf(format string, args ...interface{})
Fatalf is equivalent to Logf() followed by FailNow().
func (*T) Log
func (t *T) Log(args ...interface{})
Log formats its arguments using default formatting, analogous to Print(), and records the text in the error log.
func (*T) Logf
func (t *T) Logf(format string, args ...interface{})
Logf formats its arguments according to the format, analogous to Printf(), and records the text in the error log.
Need more packages? The Package Dashboard provides a list of goinstallable packages.
Subdirectories
| Name | Synopsis | |
|---|---|---|
| .. | ||
| iotest | Package iotest implements Readers and Writers useful only for testing. | |
| quick | Package quick implements utility functions to help with black box testing. | |
| script | Package script aids in the testing of code that uses channels. |