Documentation ¶
Overview ¶
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() } }
Index ¶
- func Main(matchString func(pat, str string) (bool, os.Error), tests []InternalTest, ...)
- func RunBenchmarks(matchString func(pat, str string) (bool, os.Error), ...)
- func RunTests(matchString func(pat, str string) (bool, os.Error), tests []InternalTest)
- func Short() bool
- type B
- type BenchmarkResult
- type InternalBenchmark
- type InternalTest
- type T
- func (t *T) Error(args ...interface{})
- func (t *T) Errorf(format string, args ...interface{})
- func (t *T) Fail()
- func (t *T) FailNow()
- func (t *T) Failed() bool
- func (t *T) Fatal(args ...interface{})
- func (t *T) Fatalf(format string, args ...interface{})
- func (t *T) Log(args ...interface{})
- func (t *T) Logf(format string, args ...interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func 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.
Types ¶
type B ¶
type B struct { N int // contains filtered or unexported fields }
B is a type passed to Benchmark functions to manage benchmark timing and to specify the number of iterations to run.
func (*B) ResetTimer ¶
func (b *B) ResetTimer()
ResetTimer stops the timer and sets the elapsed benchmark time to zero.
func (*B) SetBytes ¶
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.
type BenchmarkResult ¶
type BenchmarkResult struct { N int // The number of iterations. Ns int64 // The total time taken. Bytes int64 // Bytes processed in one iteration. }
The results of a benchmark run.
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 InternalTest ¶
An internal type but exported because it is cross-package; part of the implementation of gotest.
type T ¶
type T struct {
// contains filtered or unexported fields
}
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.
func (*T) Error ¶
func (t *T) Error(args ...interface{})
Error is equivalent to Log() 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) Fatal ¶
func (t *T) Fatal(args ...interface{})
Fatal is equivalent to Log() followed by FailNow().
Directories ¶
Path | Synopsis |
---|---|
Package iotest implements Readers and Writers useful only for testing.
|
Package iotest implements Readers and Writers useful only for testing. |
Package quick implements utility functions to help with black box testing.
|
Package quick implements utility functions to help with black box testing. |
Package script aids in the testing of code that uses channels.
|
Package script aids in the testing of code that uses channels. |