version: 1.10

package testing

import "testing"

Overview

Package testing provides support for automated testing of Go packages. It is
intended to be used in concert with the ``go test’’ command, which automates
execution of any function of the form

  1. func TestXxx(*testing.T)

where Xxx does not start with a lowercase letter. The function name serves to
identify the test routine.

Within these functions, use the Error, Fail or related methods to signal
failure.

To write a new test suite, create a file whose name ends _test.go that contains
the TestXxx functions as described here. Put the file in the same package as the
one being tested. The file will be excluded from regular package builds but will
be included when the go test'' command is run. For more detail, rungo help
test’’ and ``go help testflag’’.

Tests and benchmarks may be skipped if not applicable with a call to the Skip
method of T and B:

  1. func TestTimeConsuming(t *testing.T) {
  2. if testing.Short() {
  3. t.Skip("skipping test in short mode.")
  4. }
  5. ...
  6. }

Benchmarks

Functions of the form

  1. func BenchmarkXxx(*testing.B)

are considered benchmarks, and are executed by the “go test” command when its
-bench flag is provided. Benchmarks are run sequentially.

For a description of the testing flags, see
https://golang.org/cmd/go/#hdr-Description_of_testing_flags.

A sample benchmark function looks like this:

  1. func BenchmarkHello(b *testing.B) {
  2. for i := 0; i < b.N; i++ {
  3. fmt.Sprintf("hello")
  4. }
  5. }

The benchmark function must run the target code b.N times. During benchmark
execution, b.N is adjusted until the benchmark function lasts long enough to be
timed reliably. The output

  1. BenchmarkHello 10000000 282 ns/op

means that the loop ran 10000000 times at a speed of 282 ns per loop.

If a benchmark needs some expensive setup before running, the timer may be
reset:

  1. func BenchmarkBigLen(b *testing.B) {
  2. big := NewBig()
  3. b.ResetTimer()
  4. for i := 0; i < b.N; i++ {
  5. big.Len()
  6. }
  7. }

If a benchmark needs to test performance in a parallel setting, it may use the
RunParallel helper function; such benchmarks are intended to be used with the go
test -cpu flag:

  1. func BenchmarkTemplateParallel(b *testing.B) {
  2. templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
  3. b.RunParallel(func(pb *testing.PB) {
  4. var buf bytes.Buffer
  5. for pb.Next() {
  6. buf.Reset()
  7. templ.Execute(&buf, "World")
  8. }
  9. })
  10. }

Examples

The package also runs and verifies example code. Example functions may include a
concluding line comment that begins with “Output:” and is compared with the
standard output of the function when the tests are run. (The comparison ignores
leading and trailing space.) These are examples of an example:

  1. func ExampleHello() {
  2. fmt.Println("hello")
  3. // Output: hello
  4. }
  5. func ExampleSalutations() {
  6. fmt.Println("hello, and")
  7. fmt.Println("goodbye")
  8. // Output:
  9. // hello, and
  10. // goodbye
  11. }

The comment prefix “Unordered output:” is like “Output:”, but matches any line
order:

  1. func ExamplePerm() {
  2. for _, value := range Perm(4) {
  3. fmt.Println(value)
  4. }
  5. // Unordered output: 4
  6. // 2
  7. // 1
  8. // 3
  9. // 0
  10. }

Example functions without output comments are compiled but not executed.

The naming convention to declare examples for the package, a function F, a type
T and method M on type T are:

  1. func Example() { ... }
  2. func ExampleF() { ... }
  3. func ExampleT() { ... }
  4. func ExampleT_M() { ... }

Multiple example functions for a package/type/function/method may be provided by
appending a distinct suffix to the name. The suffix must start with a lower-case
letter.

  1. func Example_suffix() { ... }
  2. func ExampleF_suffix() { ... }
  3. func ExampleT_suffix() { ... }
  4. func ExampleT_M_suffix() { ... }

The entire test file is presented as the example when it contains a single
example function, at least one other function, type, variable, or constant
declaration, and no test or benchmark functions.

Subtests and Sub-benchmarks

The Run methods of T and B allow defining subtests and sub-benchmarks, without
having to define separate functions for each. This enables uses like
table-driven benchmarks and creating hierarchical tests. It also provides a way
to share common setup and tear-down code:

  1. func TestFoo(t *testing.T) {
  2. // <setup code>
  3. t.Run("A=1", func(t *testing.T) { ... })
  4. t.Run("A=2", func(t *testing.T) { ... })
  5. t.Run("B=1", func(t *testing.T) { ... })
  6. // <tear-down code>
  7. }

Each subtest and sub-benchmark has a unique name: the combination of the name of
the top-level test and the sequence of names passed to Run, separated by
slashes, with an optional trailing sequence number for disambiguation.

The argument to the -run and -bench command-line flags is an unanchored regular
expression that matches the test’s name. For tests with multiple slash-separated
elements, such as subtests, the argument is itself slash-separated, with
expressions matching each name element in turn. Because it is unanchored, an
empty expression matches any string. For example, using “matching” to mean
“whose name contains”:

  1. go test -run '' # Run all tests.
  2. go test -run Foo # Run top-level tests matching "Foo", such as "TestFooBar".
  3. go test -run Foo/A= # For top-level tests matching "Foo", run subtests matching "A=".
  4. go test -run /A=1 # For all top-level tests, run subtests matching "A=1".

Subtests can also be used to control parallelism. A parent test will only
complete once all of its subtests complete. In this example, all tests are run
in parallel with each other, and only with each other, regardless of other
top-level tests that may be defined:

  1. func TestGroupedParallel(t *testing.T) {
  2. for _, tc := range tests {
  3. tc := tc // capture range variable
  4. t.Run(tc.Name, func(t *testing.T) {
  5. t.Parallel()
  6. ...
  7. })
  8. }
  9. }

Run does not return until parallel subtests have completed, providing a way to
clean up after a group of parallel tests:

  1. func TestTeardownParallel(t *testing.T) {
  2. // This Run will not return until the parallel tests finish.
  3. t.Run("group", func(t *testing.T) {
  4. t.Run("Test1", parallelTest1)
  5. t.Run("Test2", parallelTest2)
  6. t.Run("Test3", parallelTest3)
  7. })
  8. // <tear-down code>
  9. }

Main

It is sometimes necessary for a test program to do extra setup or teardown
before or after testing. It is also sometimes necessary for a test to control
which code runs on the main thread. To support these and other cases, if a test
file contains a function:

  1. func TestMain(m *testing.M)

then the generated test will call TestMain(m) instead of running the tests
directly. TestMain runs in the main goroutine and can do whatever setup and
teardown is necessary around a call to m.Run. It should then call os.Exit with
the result of m.Run. When TestMain is called, flag.Parse has not been run. If
TestMain depends on command-line flags, including those of the testing package,
it should call flag.Parse explicitly.

A simple implementation of TestMain is:

  1. func TestMain(m *testing.M) {
  2. // call flag.Parse() here if TestMain uses flags
  3. os.Exit(m.Run())
  4. }

Index

Examples

Package files

allocs.go benchmark.go cover.go example.go match.go testing.go

func AllocsPerRun

  1. func AllocsPerRun(runs int, f func()) (avg float64)

AllocsPerRun returns the average number of allocations during calls to f.
Although the return value has type float64, it will always be an integral value.

To compute the number of allocations, the function will first be run once as a
warm-up. The average number of allocations over the specified number of runs
will then be measured and returned.

AllocsPerRun sets GOMAXPROCS to 1 during its measurement and will restore it
before returning.

func CoverMode

  1. func CoverMode() string

CoverMode reports what the test coverage mode is set to. The values are “set”,
“count”, or “atomic”. The return value will be empty if test coverage is not
enabled.

func Coverage

  1. func Coverage() float64

Coverage reports the current code coverage as a fraction in the range [0, 1]. If
coverage is not enabled, Coverage returns 0.

When running a large set of sequential test cases, checking Coverage after each
one can be useful for identifying which test cases exercise new code paths. It
is not a replacement for the reports generated by ‘go test -cover’ and ‘go tool
cover’.

func Main

  1. func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample)

Main is an internal function, part of the implementation of the “go test”
command. It was exported because it is cross-package and predates “internal”
packages. It is no longer used by “go test” but preserved, as much as possible,
for other systems that simulate “go test” using Main, but Main sometimes cannot
be updated as new functionality is added to the testing package. Systems
simulating “go test” should be updated to use MainStart.

func RegisterCover

  1. func RegisterCover(c Cover)

RegisterCover records the coverage data accumulators for the tests. NOTE: This
function is internal to the testing infrastructure and may change. It is not
covered (yet) by the Go 1 compatibility guidelines.

func RunBenchmarks

  1. func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark)

An internal function but exported because it is cross-package; part of the
implementation of the “go test” command.

func RunExamples

  1. func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool)

An internal function but exported because it is cross-package; part of the
implementation of the “go test” command.

func RunTests

  1. func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool)

An internal function but exported because it is cross-package; part of the
implementation of the “go test” command.

func Short

  1. func Short() bool

Short reports whether the -test.short flag is set.

func Verbose

  1. func Verbose() bool

Verbose reports whether the -test.v flag is set.

type B

  1. type B struct {
  2. N int
  3. // contains filtered or unexported fields
  4. }

B is a type passed to Benchmark functions to manage benchmark timing and to
specify the number of iterations to run.

A benchmark ends when its Benchmark function returns or calls any of the methods
FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods must be called
only from the goroutine running the Benchmark function. The other reporting
methods, such as the variations of Log and Error, may be called simultaneously
from multiple goroutines.

Like in tests, benchmark logs are accumulated during execution and dumped to
standard error when done. Unlike in tests, benchmark logs are always printed, so
as not to hide output whose existence may be affecting benchmark results.

func (*B) Error

  1. func (c *B) Error(args ...interface{})

Error is equivalent to Log followed by Fail.

func (*B) Errorf

  1. func (c *B) Errorf(format string, args ...interface{})

Errorf is equivalent to Logf followed by Fail.

func (*B) Fail

  1. func (c *B) Fail()

Fail marks the function as having failed but continues execution.

func (*B) FailNow

  1. func (c *B) FailNow()

FailNow marks the function as having failed and stops its execution by calling
runtime.Goexit (which then runs all deferred calls in the current goroutine).
Execution will continue at the next test or benchmark. FailNow must be called
from the goroutine running the test or benchmark function, not from other
goroutines created during the test. Calling FailNow does not stop those other
goroutines.

func (*B) Failed

  1. func (c *B) Failed() bool

Failed reports whether the function has failed.

func (*B) Fatal

  1. func (c *B) Fatal(args ...interface{})

Fatal is equivalent to Log followed by FailNow.

func (*B) Fatalf

  1. func (c *B) Fatalf(format string, args ...interface{})

Fatalf is equivalent to Logf followed by FailNow.

func (*B) Helper

  1. func (c *B) Helper()

Helper marks the calling function as a test helper function. When printing file
and line information, that function will be skipped. Helper may be called
simultaneously from multiple goroutines. Helper has no effect if it is called
directly from a TestXxx/BenchmarkXxx function or a subtest/sub-benchmark
function.

func (*B) Log

  1. func (c *B) Log(args ...interface{})

Log formats its arguments using default formatting, analogous to Println, and
records the text in the error log. For tests, the text will be printed only if
the test fails or the -test.v flag is set. For benchmarks, the text is always
printed to avoid having performance depend on the value of the -test.v flag.

func (*B) Logf

  1. func (c *B) Logf(format string, args ...interface{})

Logf formats its arguments according to the format, analogous to Printf, and
records the text in the error log. A final newline is added if not provided. For
tests, the text will be printed only if the test fails or the -test.v flag is
set. For benchmarks, the text is always printed to avoid having performance
depend on the value of the -test.v flag.

func (*B) Name

  1. func (c *B) Name() string

Name returns the name of the running test or benchmark.

func (*B) ReportAllocs

  1. func (b *B) ReportAllocs()

ReportAllocs enables malloc statistics for this benchmark. It is equivalent to
setting -test.benchmem, but it only affects the benchmark function that calls
ReportAllocs.

func (*B) ResetTimer

  1. func (b *B) ResetTimer()

ResetTimer zeros the elapsed benchmark time and memory allocation counters. It
does not affect whether the timer is running.

func (*B) Run

  1. func (b *B) Run(name string, f func(b *B)) bool

Run benchmarks f as a subbenchmark with the given name. It reports whether there
were any failures.

A subbenchmark is like any other benchmark. A benchmark that calls Run at least
once will not be measured itself and will be called once with N=1.

func (*B) RunParallel

  1. func (b *B) RunParallel(body func(*PB))

RunParallel runs a benchmark in parallel. It creates multiple goroutines and
distributes b.N iterations among them. The number of goroutines defaults to
GOMAXPROCS. To increase parallelism for non-CPU-bound benchmarks, call
SetParallelism before RunParallel. RunParallel is usually used with the go test
-cpu flag.

The body function will be run in each goroutine. It should set up any
goroutine-local state and then iterate until pb.Next returns false. It should
not use the StartTimer, StopTimer, or ResetTimer functions, because they have
global effect. It should also not call Run.


Example:

  1. // Parallel benchmark for text/template.Template.Execute on a single object.
  2. testing.Benchmark(func(b *testing.B) {
  3. templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
  4. // RunParallel will create GOMAXPROCS goroutines
  5. // and distribute work among them.
  6. b.RunParallel(func(pb *testing.PB) {
  7. // Each goroutine has its own bytes.Buffer.
  8. var buf bytes.Buffer
  9. for pb.Next() {
  10. // The loop body is executed b.N times total across all goroutines.
  11. buf.Reset()
  12. templ.Execute(&buf, "World")
  13. }
  14. })
  15. })

func (*B) SetBytes

  1. 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) SetParallelism

  1. func (b *B) SetParallelism(p int)

SetParallelism sets the number of goroutines used by RunParallel to
p*GOMAXPROCS. There is usually no need to call SetParallelism for CPU-bound
benchmarks. If p is less than 1, this call will have no effect.

func (*B) Skip

  1. func (c *B) Skip(args ...interface{})

Skip is equivalent to Log followed by SkipNow.

func (*B) SkipNow

  1. func (c *B) SkipNow()

SkipNow marks the test as having been skipped and stops its execution by calling
runtime.Goexit. If a test fails (see Error, Errorf, Fail) and is then skipped,
it is still considered to have failed. Execution will continue at the next test
or benchmark. See also FailNow. SkipNow must be called from the goroutine
running the test, not from other goroutines created during the test. Calling
SkipNow does not stop those other goroutines.

func (*B) Skipf

  1. func (c *B) Skipf(format string, args ...interface{})

Skipf is equivalent to Logf followed by SkipNow.

func (*B) Skipped

  1. func (c *B) Skipped() bool

Skipped reports whether the test was skipped.

func (*B) StartTimer

  1. 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

  1. 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

  1. type BenchmarkResult struct {
  2. N int // The number of iterations.
  3. T time.Duration // The total time taken.
  4. Bytes int64 // Bytes processed in one iteration.
  5. MemAllocs uint64 // The total number of memory allocations.
  6. MemBytes uint64 // The total number of bytes allocated.
  7. }

The results of a benchmark run.

func Benchmark

  1. func Benchmark(f func(b *B)) BenchmarkResult

Benchmark benchmarks a single function. Useful for creating custom benchmarks
that do not use the “go test” command.

If f calls Run, the result will be an estimate of running all its subbenchmarks
that don’t call Run in sequence in a single benchmark.

func (BenchmarkResult) AllocedBytesPerOp

  1. func (r BenchmarkResult) AllocedBytesPerOp() int64

AllocedBytesPerOp returns r.MemBytes / r.N.

func (BenchmarkResult) AllocsPerOp

  1. func (r BenchmarkResult) AllocsPerOp() int64

AllocsPerOp returns r.MemAllocs / r.N.

func (BenchmarkResult) MemString

  1. func (r BenchmarkResult) MemString() string

MemString returns r.AllocedBytesPerOp and r.AllocsPerOp in the same format as
‘go test’.

func (BenchmarkResult) NsPerOp

  1. func (r BenchmarkResult) NsPerOp() int64

func (BenchmarkResult) String

  1. func (r BenchmarkResult) String() string

type Cover

  1. type Cover struct {
  2. Mode string
  3. Counters map[string][]uint32
  4. Blocks map[string][]CoverBlock
  5. CoveredPackages string
  6. }

Cover records information about test coverage checking. NOTE: This struct is
internal to the testing infrastructure and may change. It is not covered (yet)
by the Go 1 compatibility guidelines.

type CoverBlock

  1. type CoverBlock struct {
  2. Line0 uint32
  3. Col0 uint16
  4. Line1 uint32
  5. Col1 uint16
  6. Stmts uint16
  7. }

CoverBlock records the coverage data for a single basic block. NOTE: This struct
is internal to the testing infrastructure and may change. It is not covered
(yet) by the Go 1 compatibility guidelines.

type InternalBenchmark

  1. type InternalBenchmark struct {
  2. Name string
  3. F func(b *B)
  4. }

An internal type but exported because it is cross-package; part of the
implementation of the “go test” command.

type InternalExample

  1. type InternalExample struct {
  2. Name string
  3. F func()
  4. Output string
  5. Unordered bool
  6. }

type InternalTest

  1. type InternalTest struct {
  2. Name string
  3. F func(*T)
  4. }

An internal type but exported because it is cross-package; part of the
implementation of the “go test” command.

type M

  1. type M struct {
  2. // contains filtered or unexported fields
  3. }

M is a type passed to a TestMain function to run the actual tests.

func MainStart

  1. func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) *M

MainStart is meant for use by tests generated by ‘go test’. It is not meant to
be called directly and is not subject to the Go 1 compatibility document. It may
change signature from release to release.

func (*M) Run

  1. func (m *M) Run() int

Run runs the tests. It returns an exit code to pass to os.Exit.

type PB

  1. type PB struct {
  2. // contains filtered or unexported fields
  3. }

A PB is used by RunParallel for running parallel benchmarks.

func (*PB) Next

  1. func (pb *PB) Next() bool

Next reports whether there are more iterations to execute.

type T

  1. type T struct {
  2. // contains filtered or unexported fields
  3. }

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 output
when done.

A test ends when its Test function returns or calls any of the methods FailNow,
Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods, as well as the Parallel
method, must be called only from the goroutine running the Test function.

The other reporting methods, such as the variations of Log and Error, may be
called simultaneously from multiple goroutines.

func (*T) Error

  1. func (c *T) Error(args ...interface{})

Error is equivalent to Log followed by Fail.

func (*T) Errorf

  1. func (c *T) Errorf(format string, args ...interface{})

Errorf is equivalent to Logf followed by Fail.

func (*T) Fail

  1. func (c *T) Fail()

Fail marks the function as having failed but continues execution.

func (*T) FailNow

  1. func (c *T) FailNow()

FailNow marks the function as having failed and stops its execution by calling
runtime.Goexit (which then runs all deferred calls in the current goroutine).
Execution will continue at the next test or benchmark. FailNow must be called
from the goroutine running the test or benchmark function, not from other
goroutines created during the test. Calling FailNow does not stop those other
goroutines.

func (*T) Failed

  1. func (c *T) Failed() bool

Failed reports whether the function has failed.

func (*T) Fatal

  1. func (c *T) Fatal(args ...interface{})

Fatal is equivalent to Log followed by FailNow.

func (*T) Fatalf

  1. func (c *T) Fatalf(format string, args ...interface{})

Fatalf is equivalent to Logf followed by FailNow.

func (*T) Helper

  1. func (c *T) Helper()

Helper marks the calling function as a test helper function. When printing file
and line information, that function will be skipped. Helper may be called
simultaneously from multiple goroutines. Helper has no effect if it is called
directly from a TestXxx/BenchmarkXxx function or a subtest/sub-benchmark
function.

func (*T) Log

  1. func (c *T) Log(args ...interface{})

Log formats its arguments using default formatting, analogous to Println, and
records the text in the error log. For tests, the text will be printed only if
the test fails or the -test.v flag is set. For benchmarks, the text is always
printed to avoid having performance depend on the value of the -test.v flag.

func (*T) Logf

  1. func (c *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. A final newline is added if not provided. For
tests, the text will be printed only if the test fails or the -test.v flag is
set. For benchmarks, the text is always printed to avoid having performance
depend on the value of the -test.v flag.

func (*T) Name

  1. func (c *T) Name() string

Name returns the name of the running test or benchmark.

func (*T) Parallel

  1. func (t *T) Parallel()

Parallel signals that this test is to be run in parallel with (and only with)
other parallel tests. When a test is run multiple times due to use of
-test.count or -test.cpu, multiple instances of a single test never run in
parallel with each other.

func (*T) Run

  1. func (t *T) Run(name string, f func(t *T)) bool

Run runs f as a subtest of t called name. It runs f in a separate goroutine and
blocks until f returns or calls t.Parallel to become a parallel test. Run
reports whether f succeeded (or at least did not fail before calling
t.Parallel).

Run may be called simultaneously from multiple goroutines, but all such calls
must return before the outer test function for t returns.

func (*T) Skip

  1. func (c *T) Skip(args ...interface{})

Skip is equivalent to Log followed by SkipNow.

func (*T) SkipNow

  1. func (c *T) SkipNow()

SkipNow marks the test as having been skipped and stops its execution by calling
runtime.Goexit. If a test fails (see Error, Errorf, Fail) and is then skipped,
it is still considered to have failed. Execution will continue at the next test
or benchmark. See also FailNow. SkipNow must be called from the goroutine
running the test, not from other goroutines created during the test. Calling
SkipNow does not stop those other goroutines.

func (*T) Skipf

  1. func (c *T) Skipf(format string, args ...interface{})

Skipf is equivalent to Logf followed by SkipNow.

func (*T) Skipped

  1. func (c *T) Skipped() bool

Skipped reports whether the test was skipped.

type TB

  1. type TB interface {
  2. Error(args ...interface{})
  3. Errorf(format string, args ...interface{})
  4. Fail()
  5. FailNow()
  6. Failed() bool
  7. Fatal(args ...interface{})
  8. Fatalf(format string, args ...interface{})
  9. Log(args ...interface{})
  10. Logf(format string, args ...interface{})
  11. Name() string
  12. Skip(args ...interface{})
  13. SkipNow()
  14. Skipf(format string, args ...interface{})
  15. Skipped() bool
  16. Helper()
  17. // contains filtered or unexported methods
  18. }

TB is the interface common to T and B.

Subdirectories