testing
packagestandard libraryThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
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 automatesexecution of any function of the form
func TestXxx(*testing.T)
where Xxx does not start with a lowercase letter. The function nameserves to identify the test routine.
Within these functions, useT.Error,T.Fail or related methods to signal failure.
To write a new test suite, create a file thatcontains the TestXxx functions as described here,and give that file a name ending in "_test.go".The file will be excluded from regularpackage builds but will be included when the "go test" command is run.
The test file can be in the same package as the one being tested,or in a corresponding package with the suffix "_test".
If the test file is in the same package, it may refer to unexportedidentifiers within the package, as in this example:
package absimport "testing"func TestAbs(t *testing.T) { got := Abs(-1) if got != 1 { t.Errorf("Abs(-1) = %d; want 1", got) }}If the file is in a separate "_test" package, the package being testedmust be imported explicitly and only its exported identifiers may be used.This is known as "black box" testing.
package abs_testimport ("testing""path_to_pkg/abs")func TestAbs(t *testing.T) { got := abs.Abs(-1) if got != 1 { t.Errorf("Abs(-1) = %d; want 1", got) }}For more detail, rungo help test andgo help testflag.
Benchmarks¶
Functions of the form
func BenchmarkXxx(*testing.B)
are considered benchmarks, and are executed by the "go test" command whenits -bench flag is provided. Benchmarks are run sequentially.
For a description of the testing flags, seego help testflag.
A sample benchmark function looks like this:
func BenchmarkRandInt(b *testing.B) { for b.Loop() { rand.Int() }}The output
BenchmarkRandInt-8 68453040 17.8 ns/op
means that the body of the loop ran 68453040 times at a speed of 17.8 ns per loop.
Only the body of the loop is timed, so benchmarks may do expensivesetup before calling b.Loop, which will not be counted toward thebenchmark measurement:
func BenchmarkBigLen(b *testing.B) { big := NewBig() for b.Loop() { big.Len() }}If a benchmark needs to test performance in a parallel setting, it may usethe RunParallel helper function; such benchmarks are intended to be used withthe go test -cpu flag:
func BenchmarkTemplateParallel(b *testing.B) { templ := template.Must(template.New("test").Parse("Hello, {{.}}!")) b.RunParallel(func(pb *testing.PB) { var buf bytes.Buffer for pb.Next() { buf.Reset() templ.Execute(&buf, "World") } })}A detailed specification of the benchmark results format is giveninhttps://go.dev/design/14313-benchmark-format.
There are standard tools for working with benchmark results atgolang.org/x/perf/cmd.In particular,golang.org/x/perf/cmd/benchstat performsstatistically robust A/B comparisons.
b.N-style benchmarks¶
Prior to the introduction ofB.Loop, benchmarks were written in adifferent style using B.N. For example:
func BenchmarkRandInt(b *testing.B) { for range b.N { rand.Int() }}In this style of benchmark, the benchmark function must runthe target code b.N times. The benchmark function is calledmultiple times with b.N adjusted until the benchmark functionlasts long enough to be timed reliably. This also means any setupdone before the loop may be run several times.
If a benchmark needs some expensive setup before running, the timershould be explicitly reset:
func BenchmarkBigLen(b *testing.B) { big := NewBig() b.ResetTimer() for range b.N { big.Len() }}New benchmarks should prefer usingB.Loop, which is more robustand more efficient.
Examples¶
The package also runs and verifies example code. Example functions mayinclude a concluding line comment that begins with "Output:" and is compared withthe standard output of the function when the tests are run. (The comparisonignores leading and trailing space.) These are examples of an example:
func ExampleHello() { fmt.Println("hello") // Output: hello}func ExampleSalutations() { fmt.Println("hello, and") fmt.Println("goodbye") // Output: // hello, and // goodbye}The comment prefix "Unordered output:" is like "Output:", but matches anyline order:
func ExamplePerm() { for _, value := range Perm(5) { fmt.Println(value) } // Unordered output: 4 // 2 // 1 // 3 // 0}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 andmethod M on type T are:
func Example() { ... }func ExampleF() { ... }func ExampleT() { ... }func ExampleT_M() { ... }Multiple example functions for a package/type/function/method may be provided byappending a distinct suffix to the name. The suffix must start with alower-case letter.
func Example_suffix() { ... }func ExampleF_suffix() { ... }func ExampleT_suffix() { ... }func ExampleT_M_suffix() { ... }The entire test file is presented as the example when it contains a singleexample function, at least one other function, type, variable, or constantdeclaration, and no test or benchmark functions.
Fuzzing¶
'go test' and the testing package support fuzzing, a testing technique wherea function is called with randomly generated inputs to find bugs notanticipated by unit tests.
Functions of the form
func FuzzXxx(*testing.F)
are considered fuzz tests.
For example:
func FuzzHex(f *testing.F) { for _, seed := range [][]byte{{}, {0}, {9}, {0xa}, {0xf}, {1, 2, 3, 4}} { f.Add(seed) } f.Fuzz(func(t *testing.T, in []byte) { enc := hex.EncodeToString(in) out, err := hex.DecodeString(enc) if err != nil { t.Fatalf("%v: decode: %v", in, err) } if !bytes.Equal(in, out) { t.Fatalf("%v: not equal after round trip: %v", in, out) } })}A fuzz test maintains a seed corpus, or a set of inputs which are run bydefault, and can seed input generation. Seed inputs may be registered bycallingF.Add or by storing files in the directory testdata/fuzz/<Name>(where <Name> is the name of the fuzz test) within the package containingthe fuzz test. Seed inputs are optional, but the fuzzing engine may findbugs more efficiently when provided with a set of small seed inputs with goodcode coverage. These seed inputs can also serve as regression tests for bugsidentified through fuzzing.
The function passed toF.Fuzz within the fuzz test is considered the fuzztarget. A fuzz target must accept a*T parameter, followed by one or moreparameters for random inputs. The types of arguments passed toF.Add mustbe identical to the types of these parameters. The fuzz target may signalthat it's found a problem the same way tests do: by callingT.Fail (or anymethod that calls it likeT.Error orT.Fatal) or by panicking.
When fuzzing is enabled (by setting the -fuzz flag to a regular expressionthat matches a specific fuzz test), the fuzz target is called with argumentsgenerated by repeatedly making random changes to the seed inputs. Onsupported platforms, 'go test' compiles the test executable with fuzzingcoverage instrumentation. The fuzzing engine uses that instrumentation tofind and cache inputs that expand coverage, increasing the likelihood offinding bugs. If the fuzz target fails for a given input, the fuzzing enginewrites the inputs that caused the failure to a file in the directorytestdata/fuzz/<Name> within the package directory. This file later serves asa seed input. If the file can't be written at that location (for example,because the directory is read-only), the fuzzing engine writes the file tothe fuzz cache directory within the build cache instead.
When fuzzing is disabled, the fuzz target is called with the seed inputsregistered withF.Add and seed inputs from testdata/fuzz/<Name>. In thismode, the fuzz test acts much like a regular test, with subtests startedwithF.Fuzz instead ofT.Run.
Seehttps://go.dev/doc/fuzz for documentation about fuzzing.
Skipping¶
Tests or benchmarks may be skipped at run time with a call toT.Skip orB.Skip:
func TestTimeConsuming(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } ...}TheT.Skip method can be used in a fuzz target if the input is invalid,but should not be considered a failing input. For example:
func FuzzJSONMarshaling(f *testing.F) { f.Fuzz(func(t *testing.T, b []byte) { var v interface{} if err := json.Unmarshal(b, &v); err != nil { t.Skip() } if _, err := json.Marshal(v); err != nil { t.Errorf("Marshal: %v", err) } })}Subtests and Sub-benchmarks¶
TheT.Run andB.Run methods allow defining subtests and sub-benchmarks,without having to define separate functions for each. This enables useslike table-driven benchmarks and creating hierarchical tests.It also provides a way to share common setup and tear-down code:
func TestFoo(t *testing.T) { // <setup code> t.Run("A=1", func(t *testing.T) { ... }) t.Run("A=2", func(t *testing.T) { ... }) t.Run("B=1", func(t *testing.T) { ... }) // <tear-down code>}Each subtest and sub-benchmark has a unique name: the combination of the nameof the top-level test and the sequence of names passed to Run, separated byslashes, with an optional trailing sequence number for disambiguation.
The argument to the -run, -bench, and -fuzz command-line flags is an unanchored regularexpression that matches the test's name. For tests with multiple slash-separatedelements, such as subtests, the argument is itself slash-separated, withexpressions matching each name element in turn. Because it is unanchored, anempty expression matches any string.For example, using "matching" to mean "whose name contains":
go test -run '' # Run all tests.go test -run Foo # Run top-level tests matching "Foo", such as "TestFooBar".go test -run Foo/A= # For top-level tests matching "Foo", run subtests matching "A=".go test -run /A=1 # For all top-level tests, run subtests matching "A=1".go test -fuzz FuzzFoo # Fuzz the target matching "FuzzFoo"
The -run argument can also be used to run a specific value in the seedcorpus, for debugging. For example:
go test -run=FuzzFoo/9ddb952d9814
The -fuzz and -run flags can both be set, in order to fuzz a target butskip the execution of all other tests.
Subtests can also be used to control parallelism. A parent test will onlycomplete once all of its subtests complete. In this example, all tests arerun in parallel with each other, and only with each other, regardless ofother top-level tests that may be defined:
func TestGroupedParallel(t *testing.T) { for _, tc := range tests { t.Run(tc.Name, func(t *testing.T) { t.Parallel() ... }) }}Run does not return until parallel subtests have completed, providing a wayto clean up after a group of parallel tests:
func TestTeardownParallel(t *testing.T) { // This Run will not return until the parallel tests finish. t.Run("group", func(t *testing.T) { t.Run("Test1", parallelTest1) t.Run("Test2", parallelTest2) t.Run("Test3", parallelTest3) }) // <tear-down code>}Main¶
It is sometimes necessary for a test or benchmark program to do extra setup or teardownbefore or after it executes. It is also sometimes necessary to controlwhich code runs on the main thread. To support these and other cases,if a test file contains a function:
func TestMain(m *testing.M)
then the generated test will call TestMain(m) instead of running the tests or benchmarksdirectly. TestMain runs in the main goroutine and can do whatever setupand teardown is necessary around a call to m.Run. m.Run will return an exitcode that may be passed toos.Exit. If TestMain returns, the test wrapperwill pass the result of m.Run toos.Exit itself.
When TestMain is called, flag.Parse has not been run. If TestMain depends oncommand-line flags, including those of the testing package, it should callflag.Parse explicitly. Command line flags are always parsed by the time testor benchmark functions run.
A simple implementation of TestMain is:
func TestMain(m *testing.M) {// call flag.Parse() here if TestMain uses flagsm.Run()}TestMain is a low-level primitive and should not be necessary for casualtesting needs, where ordinary test functions suffice.
Index¶
- func AllocsPerRun(runs int, f func()) (avg float64)
- func CoverMode() string
- func Coverage() float64
- func Init()
- func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, ...)
- func RegisterCover(c Cover)
- func RunBenchmarks(matchString func(pat, str string) (bool, error), ...)
- func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool)
- func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool)
- func Short() bool
- func Testing() bool
- func Verbose() bool
- type B
- func (c *B) Attr(key, value string)
- func (c *B) Chdir(dir string)
- func (c *B) Cleanup(f func())
- func (c *B) Context() context.Context
- func (b *B) Elapsed() time.Duration
- func (c *B) Error(args ...any)
- func (c *B) Errorf(format string, args ...any)
- func (c *B) Fail()
- func (c *B) FailNow()
- func (c *B) Failed() bool
- func (c *B) Fatal(args ...any)
- func (c *B) Fatalf(format string, args ...any)
- func (c *B) Helper()
- func (c *B) Log(args ...any)
- func (c *B) Logf(format string, args ...any)
- func (b *B) Loop() bool
- func (c *B) Name() string
- func (c *B) Output() io.Writer
- func (b *B) ReportAllocs()
- func (b *B) ReportMetric(n float64, unit string)
- func (b *B) ResetTimer()
- func (b *B) Run(name string, f func(b *B)) bool
- func (b *B) RunParallel(body func(*PB))
- func (b *B) SetBytes(n int64)
- func (b *B) SetParallelism(p int)
- func (c *B) Setenv(key, value string)
- func (c *B) Skip(args ...any)
- func (c *B) SkipNow()
- func (c *B) Skipf(format string, args ...any)
- func (c *B) Skipped() bool
- func (b *B) StartTimer()
- func (b *B) StopTimer()
- func (c *B) TempDir() string
- type BenchmarkResult
- type Cover
- type CoverBlock
- type F
- func (f *F) Add(args ...any)
- func (c *F) Attr(key, value string)
- func (c *F) Chdir(dir string)
- func (c *F) Cleanup(f func())
- func (c *F) Context() context.Context
- func (c *F) Error(args ...any)
- func (c *F) Errorf(format string, args ...any)
- func (f *F) Fail()
- func (c *F) FailNow()
- func (c *F) Failed() bool
- func (c *F) Fatal(args ...any)
- func (c *F) Fatalf(format string, args ...any)
- func (f *F) Fuzz(ff any)
- func (f *F) Helper()
- func (c *F) Log(args ...any)
- func (c *F) Logf(format string, args ...any)
- func (c *F) Name() string
- func (c *F) Output() io.Writer
- func (c *F) Setenv(key, value string)
- func (c *F) Skip(args ...any)
- func (c *F) SkipNow()
- func (c *F) Skipf(format string, args ...any)
- func (f *F) Skipped() bool
- func (c *F) TempDir() string
- type InternalBenchmark
- type InternalExample
- type InternalFuzzTarget
- type InternalTest
- type M
- type PB
- type T
- func (c *T) Attr(key, value string)
- func (t *T) Chdir(dir string)
- func (c *T) Cleanup(f func())
- func (c *T) Context() context.Context
- func (t *T) Deadline() (deadline time.Time, ok bool)
- func (c *T) Error(args ...any)
- func (c *T) Errorf(format string, args ...any)
- func (c *T) Fail()
- func (c *T) FailNow()
- func (c *T) Failed() bool
- func (c *T) Fatal(args ...any)
- func (c *T) Fatalf(format string, args ...any)
- func (c *T) Helper()
- func (c *T) Log(args ...any)
- func (c *T) Logf(format string, args ...any)
- func (c *T) Name() string
- func (c *T) Output() io.Writer
- func (t *T) Parallel()
- func (t *T) Run(name string, f func(t *T)) bool
- func (t *T) Setenv(key, value string)
- func (c *T) Skip(args ...any)
- func (c *T) SkipNow()
- func (c *T) Skipf(format string, args ...any)
- func (c *T) Skipped() bool
- func (c *T) TempDir() string
- type TB
Examples¶
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
funcAllocsPerRun¶added ingo1.1
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 asa warm-up. The average number of allocations over the specified number ofruns will then be measured and returned.
AllocsPerRun setsruntime.GOMAXPROCS to 1 during its measurement and will restoreit before returning.
funcCoverMode¶added ingo1.8
func CoverMode()string
CoverMode reports what the test coverage mode is set to. Thevalues are "set", "count", or "atomic". The return value will beempty if test coverage is not enabled.
funcCoverage¶added ingo1.4
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 onecan 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'.
funcInit¶added ingo1.13
func Init()
Init registers testing flags. These flags are automatically registered bythe "go test" command before running test functions, so Init is only neededwhen calling functions such as Benchmark without using "go test".
Init is not safe to call concurrently. It has no effect if it was already called.
funcMain¶
func Main(matchString func(pat, strstring) (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 othersystems that simulate "go test" using Main, but Main sometimes cannot be updated asnew functionality is added to the testing package.Systems simulating "go test" should be updated to useMainStart.
funcRegisterCover¶added ingo1.2
func RegisterCover(cCover)
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.
funcRunBenchmarks¶
func RunBenchmarks(matchString func(pat, strstring) (bool,error), benchmarks []InternalBenchmark)
RunBenchmarks is an internal function but exported because it is cross-package;it is part of the implementation of the "go test" command.
funcRunExamples¶
func RunExamples(matchString func(pat, strstring) (bool,error), examples []InternalExample) (okbool)
RunExamples is an internal function but exported because it is cross-package;it is part of the implementation of the "go test" command.
funcRunTests¶
func RunTests(matchString func(pat, strstring) (bool,error), tests []InternalTest) (okbool)
RunTests is an internal function but exported because it is cross-package;it is part of the implementation of the "go test" command.
Types¶
typeB¶
type B struct {Nint// contains filtered or unexported fields}B is a type passed toBenchmark functions to manage benchmarktiming and control the number of iterations.
A benchmark ends when its Benchmark function returns or calls any of the methodsB.FailNow,B.Fatal,B.Fatalf,B.SkipNow,B.Skip, orB.Skipf.Those methods must be called only from the goroutine running the Benchmark function.The other reporting methods, such as the variations ofB.Log andB.Error,may be called simultaneously from multiple goroutines.
Like in tests, benchmark logs are accumulated during executionand dumped to standard output when done. Unlike in tests, benchmark logsare always printed, so as not to hide output whose existence may beaffecting benchmark results.
func (*B)Attr¶added ingo1.25.0
func (c *B) Attr(key, valuestring)
Attr emits a test attribute associated with this test.
The key must not contain whitespace.The value must not contain newlines or carriage returns.
The meaning of different attribute keys is left up tocontinuous integration systems and test frameworks.
Test attributes are emitted immediately in the test log,but they are intended to be treated as unordered.
func (*B)Chdir¶added ingo1.24.0
func (c *B) Chdir(dirstring)
Chdir callsos.Chdir and uses Cleanup to restore the currentworking directory to its original value after the test. On Unix, italso sets PWD environment variable for the duration of the test.
Because Chdir affects the whole process, it cannot be usedin parallel tests or tests with parallel ancestors.
func (*B)Cleanup¶added ingo1.14
func (c *B) Cleanup(f func())
Cleanup registers a function to be called when the test (or subtest) and all itssubtests complete. Cleanup functions will be called in last added,first called order.
func (*B)Context¶added ingo1.24.0
Context returns a context that is canceled just beforeCleanup-registered functions are called.
Cleanup functions can wait for any resourcesthat shut down oncontext.Context.Done before the test or benchmark completes.
func (*B)Elapsed¶added ingo1.20
Elapsed returns the measured elapsed time of the benchmark.The duration reported by Elapsed matches the one measured byB.StartTimer,B.StopTimer, andB.ResetTimer.
func (*B)FailNow¶
func (c *B) FailNow()
FailNow marks the function as having failed and stops its executionby callingruntime.Goexit (which then runs all deferred calls in thecurrent goroutine).Execution will continue at the next test or benchmark.FailNow must be called from the goroutine running thetest or benchmark function, not from other goroutinescreated during the test. Calling FailNow does not stopthose other goroutines.
func (*B)Helper¶added ingo1.9
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.
func (*B)Log¶
func (c *B) Log(args ...any)
Log formats its arguments using default formatting, analogous tofmt.Println,and records the text in the error log. For tests, the text will be printed only ifthe test fails or the -test.v flag is set. For benchmarks, the text is alwaysprinted to avoid having performance depend on the value of the -test.v flag.It is an error to call Log after a test or benchmark returns.
func (*B)Logf¶
Logf formats its arguments according to the format, analogous tofmt.Printf, andrecords the text in the error log. A final newline is added if not provided. Fortests, the text will be printed only if the test fails or the -test.v flag isset. For benchmarks, the text is always printed to avoid having performancedepend on the value of the -test.v flag.It is an error to call Logf after a test or benchmark returns.
func (*B)Loop¶added ingo1.24.0
Loop returns true as long as the benchmark should continue running.
A typical benchmark is structured like:
func Benchmark(b *testing.B) {... setup ...for b.Loop() {... code to measure ...}... cleanup ...}Loop resets the benchmark timer the first time it is called in a benchmark,so any setup performed prior to starting the benchmark loop does not counttoward the benchmark measurement. Likewise, when it returns false, it stopsthe timer so cleanup code is not measured.
Within the body of a "for b.Loop() { ... }" loop, arguments to andresults from function calls within the loop are kept alive, preventingthe compiler from fully optimizing away the loop body. Currently, this isimplemented by disabling inlining of functions called in a b.Loop loop.This applies only to calls syntactically between the curly braces of the loop,and the loop condition must be written exactly as "b.Loop()". Optimizationsare performed as usual in any functions called by the loop.
After Loop returns false, b.N contains the total number of iterations thatran, so the benchmark may use b.N to compute other average metrics.
Prior to the introduction of Loop, benchmarks were expected to contain anexplicit loop from 0 to b.N. Benchmarks should either use Loop or contain aloop to b.N, but not both. Loop offers more automatic management of thebenchmark timer, and runs each benchmark function only once per measurement,whereas b.N-based benchmarks must run the benchmark function (and anyassociated setup and cleanup) several times.
Example¶
package mainimport ("math/rand/v2""testing")// ExBenchmark shows how to use b.Loop in a benchmark.//// (If this were a real benchmark, not an example, this would be named// BenchmarkSomething.)func ExBenchmark(b *testing.B) {// Generate a large random slice to use as an input.// Since this is done before the first call to b.Loop(),// it doesn't count toward the benchmark time.input := make([]int, 128<<10)for i := range input {input[i] = rand.Int()}// Perform the benchmark.for b.Loop() {// Normally, the compiler would be allowed to optimize away the call// to sum because it has no side effects and the result isn't used.// However, inside a b.Loop loop, the compiler ensures function calls// aren't optimized away.sum(input)}// Outside the loop, the timer is stopped, so we could perform// cleanup if necessary without affecting the result.}func sum(data []int) int {total := 0for _, value := range data {total += value}return total}func main() {testing.Benchmark(ExBenchmark)}func (*B)Name¶added ingo1.8
func (c *B) Name()string
Name returns the name of the running (sub-) test or benchmark.
The name will include the name of the test along with the names ofany nested sub-tests. If two sibling sub-tests have the same name,Name will append a suffix to guarantee the returned name is unique.
func (*B)Output¶added ingo1.25.0
Output returns a Writer that writes to the same test output stream as TB.Log.The output is indented like TB.Log lines, but Output does notadd source locations or newlines. The output is internally linebuffered, and a call to TB.Log or the end of the test will implicitlyflush the buffer, followed by a newline. After a test function and all itsparents return, neither Output nor the Write method may be called.
func (*B)ReportAllocs¶added ingo1.1
func (b *B) ReportAllocs()
ReportAllocs enables malloc statistics for this benchmark.It is equivalent to setting -test.benchmem, but it only affects thebenchmark function that calls ReportAllocs.
func (*B)ReportMetric¶added ingo1.13
ReportMetric adds "n unit" to the reported benchmark results.If the metric is per-iteration, the caller should divide by b.N,and by convention units should end in "/op".ReportMetric overrides any previously reported value for the same unit.ReportMetric panics if unit is the empty string or if unit containsany whitespace.If unit is a unit normally reported by the benchmark framework itself(such as "allocs/op"), ReportMetric will override that metric.Setting "ns/op" to 0 will suppress that built-in metric.
Example¶
package mainimport ("cmp""slices""testing")func main() {// This reports a custom benchmark metric relevant to a// specific algorithm (in this case, sorting).testing.Benchmark(func(b *testing.B) {var compares int64for b.Loop() {s := []int{5, 4, 3, 2, 1}slices.SortFunc(s, func(a, b int) int {compares++return cmp.Compare(a, b)})}// This metric is per-operation, so divide by b.N and// report it as a "/op" unit.b.ReportMetric(float64(compares)/float64(b.N), "compares/op")// This metric is per-time, so divide by b.Elapsed and// report it as a "/ns" unit.b.ReportMetric(float64(compares)/float64(b.Elapsed().Nanoseconds()), "compares/ns")})}Example (Parallel)¶
package mainimport ("cmp""slices""sync/atomic""testing")func main() {// This reports a custom benchmark metric relevant to a// specific algorithm (in this case, sorting) in parallel.testing.Benchmark(func(b *testing.B) {var compares atomic.Int64b.RunParallel(func(pb *testing.PB) {for pb.Next() {s := []int{5, 4, 3, 2, 1}slices.SortFunc(s, func(a, b int) int {// Because RunParallel runs the function many// times in parallel, we must increment the// counter atomically to avoid racing writes.compares.Add(1)return cmp.Compare(a, b)})}})// NOTE: Report each metric once, after all of the parallel// calls have completed.// This metric is per-operation, so divide by b.N and// report it as a "/op" unit.b.ReportMetric(float64(compares.Load())/float64(b.N), "compares/op")// This metric is per-time, so divide by b.Elapsed and// report it as a "/ns" unit.b.ReportMetric(float64(compares.Load())/float64(b.Elapsed().Nanoseconds()), "compares/ns")})}func (*B)ResetTimer¶
func (b *B) ResetTimer()
ResetTimer zeroes the elapsed benchmark time and memory allocation countersand deletes user-reported metrics.It does not affect whether the timer is running.
func (*B)Run¶added ingo1.7
Run benchmarks f as a subbenchmark with the given name. It reportswhether there were any failures.
A subbenchmark is like any other benchmark. A benchmark that calls Run atleast once will not be measured itself and will be called once with N=1.
func (*B)RunParallel¶added ingo1.3
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 fornon-CPU-bound benchmarks, callB.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 anygoroutine-local state and then iterate until pb.Next returns false.It should not use theB.StartTimer,B.StopTimer, orB.ResetTimer functions,because they have global effect. It should also not callB.Run.
RunParallel reports ns/op values as wall time for the benchmark as a whole,not the sum of wall time or CPU time over each parallel goroutine.
Example¶
package mainimport ("bytes""testing""text/template")func main() {// Parallel benchmark for text/template.Template.Execute on a single object.testing.Benchmark(func(b *testing.B) {templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))// RunParallel will create GOMAXPROCS goroutines// and distribute work among them.b.RunParallel(func(pb *testing.PB) {// Each goroutine has its own bytes.Buffer.var buf bytes.Bufferfor pb.Next() {// The loop body is executed b.N times total across all goroutines.buf.Reset()templ.Execute(&buf, "World")}})})}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)SetParallelism¶added ingo1.3
SetParallelism sets the number of goroutines used byB.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)Setenv¶added ingo1.17
func (c *B) Setenv(key, valuestring)
Setenv callsos.Setenv and uses Cleanup torestore the environment variable to its original valueafter the test.
Because Setenv affects the whole process, it cannot be usedin parallel tests or tests with parallel ancestors.
func (*B)Skip¶added ingo1.1
func (c *B) Skip(args ...any)
Skip is equivalent to Log followed by SkipNow.
func (*B)SkipNow¶added ingo1.1
func (c *B) SkipNow()
SkipNow marks the test as having been skipped and stops its executionby callingruntime.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 fromother goroutines created during the test. Calling SkipNow does not stopthose other goroutines.
func (*B)Skipped¶added ingo1.1
func (c *B) Skipped()bool
Skipped reports whether the test was skipped.
func (*B)StartTimer¶
func (b *B) StartTimer()
StartTimer starts timing a test. This function is called automaticallybefore a benchmark starts, but it can also be used to resume timing aftera call toB.StopTimer.
func (*B)StopTimer¶
func (b *B) StopTimer()
StopTimer stops timing a test. This can be used to pause the timerwhile performing steps that you don't want to measure.
func (*B)TempDir¶added ingo1.15
func (c *B) TempDir()string
TempDir returns a temporary directory for the test to use.The directory is automatically removed when the test andall its subtests complete.Each subsequent call to TempDir returns a unique directory;if the directory creation fails, TempDir terminates the test by calling Fatal.
typeBenchmarkResult¶
type BenchmarkResult struct {Nint// The number of iterations.Ttime.Duration// The total time taken.Bytesint64// Bytes processed in one iteration.MemAllocsuint64// The total number of memory allocations.MemBytesuint64// The total number of bytes allocated.// Extra records additional metrics reported by ReportMetric.Extra map[string]float64}BenchmarkResult contains the results of a benchmark run.
funcBenchmark¶
func Benchmark(f func(b *B))BenchmarkResult
Benchmark benchmarks a single function. It is useful for creatingcustom benchmarks that do not use the "go test" command.
If f depends on testing flags, thenInit must be used to registerthose flags before calling Benchmark and before callingflag.Parse.
If f calls Run, the result will be an estimate of running all itssubbenchmarks that don't call Run in sequence in a single benchmark.
func (BenchmarkResult)AllocedBytesPerOp¶added ingo1.1
func (rBenchmarkResult) AllocedBytesPerOp()int64
AllocedBytesPerOp returns the "B/op" metric,which is calculated as r.MemBytes / r.N.
func (BenchmarkResult)AllocsPerOp¶added ingo1.1
func (rBenchmarkResult) AllocsPerOp()int64
AllocsPerOp returns the "allocs/op" metric,which is calculated as r.MemAllocs / r.N.
func (BenchmarkResult)MemString¶added ingo1.1
func (rBenchmarkResult) MemString()string
MemString returns r.AllocedBytesPerOp and r.AllocsPerOp in the same format as 'go test'.
func (BenchmarkResult)NsPerOp¶
func (rBenchmarkResult) NsPerOp()int64
NsPerOp returns the "ns/op" metric.
func (BenchmarkResult)String¶
func (rBenchmarkResult) String()string
String returns a summary of the benchmark results.It follows the benchmark result line format fromhttps://golang.org/design/14313-benchmark-format, not including thebenchmark name.Extra metrics override built-in metrics of the same name.String does not include allocs/op or B/op, since those are reportedbyBenchmarkResult.MemString.
typeCover¶added ingo1.2
type Cover struct {ModestringCounters map[string][]uint32Blocks map[string][]CoverBlockCoveredPackagesstring}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.
typeCoverBlock¶added ingo1.2
type CoverBlock struct {Line0uint32// Line number for block start.Col0uint16// Column number for block start.Line1uint32// Line number for block end.Col1uint16// Column number for block end.Stmtsuint16// Number of statements included in this block.}CoverBlock records the coverage data for a single basic block.The fields are 1-indexed, as in an editor: The opening line ofthe file is number 1, for example. Columns are measuredin bytes.NOTE: This struct is internal to the testing infrastructure and may change.It is not covered (yet) by the Go 1 compatibility guidelines.
typeF¶added ingo1.18
type F struct {// contains filtered or unexported fields}F is a type passed to fuzz tests.
Fuzz tests run generated inputs against a provided fuzz target, which canfind and report potential bugs in the code being tested.
A fuzz test runs the seed corpus by default, which includes entries providedbyF.Add and entries in the testdata/fuzz/<FuzzTestName> directory. Afterany necessary setup and calls toF.Add, the fuzz test must then callF.Fuzz to provide the fuzz target. See the testing package documentationfor an example, and see theF.Fuzz andF.Add method documentation fordetails.
*F methods can only be called beforeF.Fuzz. Once the test isexecuting the fuzz target, only*T methods can be used. The only *F methodsthat are allowed in theF.Fuzz function areF.Failed andF.Name.
func (*F)Add¶added ingo1.18
Add will add the arguments to the seed corpus for the fuzz test. This will bea no-op if called after or within the fuzz target, and args must match thearguments for the fuzz target.
func (*F)Attr¶added ingo1.25.0
func (c *F) Attr(key, valuestring)
Attr emits a test attribute associated with this test.
The key must not contain whitespace.The value must not contain newlines or carriage returns.
The meaning of different attribute keys is left up tocontinuous integration systems and test frameworks.
Test attributes are emitted immediately in the test log,but they are intended to be treated as unordered.
func (*F)Chdir¶added ingo1.24.0
func (c *F) Chdir(dirstring)
Chdir callsos.Chdir and uses Cleanup to restore the currentworking directory to its original value after the test. On Unix, italso sets PWD environment variable for the duration of the test.
Because Chdir affects the whole process, it cannot be usedin parallel tests or tests with parallel ancestors.
func (*F)Cleanup¶added ingo1.18
func (c *F) Cleanup(f func())
Cleanup registers a function to be called when the test (or subtest) and all itssubtests complete. Cleanup functions will be called in last added,first called order.
func (*F)Context¶added ingo1.24.0
Context returns a context that is canceled just beforeCleanup-registered functions are called.
Cleanup functions can wait for any resourcesthat shut down oncontext.Context.Done before the test or benchmark completes.
func (*F)Error¶added ingo1.18
func (c *F) Error(args ...any)
Error is equivalent to Log followed by Fail.
func (*F)Fail¶added ingo1.18
func (f *F) Fail()
Fail marks the function as having failed but continues execution.
func (*F)FailNow¶added ingo1.18
func (c *F) FailNow()
FailNow marks the function as having failed and stops its executionby callingruntime.Goexit (which then runs all deferred calls in thecurrent goroutine).Execution will continue at the next test or benchmark.FailNow must be called from the goroutine running thetest or benchmark function, not from other goroutinescreated during the test. Calling FailNow does not stopthose other goroutines.
func (*F)Failed¶added ingo1.18
func (c *F) Failed()bool
Failed reports whether the function has failed.
func (*F)Fatal¶added ingo1.18
func (c *F) Fatal(args ...any)
Fatal is equivalent to Log followed by FailNow.
func (*F)Fuzz¶added ingo1.18
Fuzz runs the fuzz function, ff, for fuzz testing. If ff fails for a set ofarguments, those arguments will be added to the seed corpus.
ff must be a function with no return value whose first argument is*T andwhose remaining arguments are the types to be fuzzed.For example:
f.Fuzz(func(t *testing.T, b []byte, i int) { ... })The following types are allowed: []byte, string, bool, byte, rune, float32,float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64.More types may be supported in the future.
ff must not call any*F methods, e.g.F.Log,F.Error,F.Skip. Usethe corresponding*T method instead. The only*F methods that are allowed inthe F.Fuzz function areF.Failed andF.Name.
This function should be fast and deterministic, and its behavior should notdepend on shared state. No mutable input arguments, or pointers to them,should be retained between executions of the fuzz function, as the memorybacking them may be mutated during a subsequent invocation. ff must notmodify the underlying data of the arguments provided by the fuzzing engine.
When fuzzing, F.Fuzz does not return until a problem is found, time runs out(set with -fuzztime), or the test process is interrupted by a signal. F.Fuzzshould be called exactly once, unlessF.Skip orF.Fail is called beforehand.
func (*F)Helper¶added ingo1.18
func (f *F) 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.
func (*F)Log¶added ingo1.18
func (c *F) Log(args ...any)
Log formats its arguments using default formatting, analogous tofmt.Println,and records the text in the error log. For tests, the text will be printed only ifthe test fails or the -test.v flag is set. For benchmarks, the text is alwaysprinted to avoid having performance depend on the value of the -test.v flag.It is an error to call Log after a test or benchmark returns.
func (*F)Logf¶added ingo1.18
Logf formats its arguments according to the format, analogous tofmt.Printf, andrecords the text in the error log. A final newline is added if not provided. Fortests, the text will be printed only if the test fails or the -test.v flag isset. For benchmarks, the text is always printed to avoid having performancedepend on the value of the -test.v flag.It is an error to call Logf after a test or benchmark returns.
func (*F)Name¶added ingo1.18
func (c *F) Name()string
Name returns the name of the running (sub-) test or benchmark.
The name will include the name of the test along with the names ofany nested sub-tests. If two sibling sub-tests have the same name,Name will append a suffix to guarantee the returned name is unique.
func (*F)Output¶added ingo1.25.0
Output returns a Writer that writes to the same test output stream as TB.Log.The output is indented like TB.Log lines, but Output does notadd source locations or newlines. The output is internally linebuffered, and a call to TB.Log or the end of the test will implicitlyflush the buffer, followed by a newline. After a test function and all itsparents return, neither Output nor the Write method may be called.
func (*F)Setenv¶added ingo1.18
func (c *F) Setenv(key, valuestring)
Setenv callsos.Setenv and uses Cleanup torestore the environment variable to its original valueafter the test.
Because Setenv affects the whole process, it cannot be usedin parallel tests or tests with parallel ancestors.
func (*F)Skip¶added ingo1.18
func (c *F) Skip(args ...any)
Skip is equivalent to Log followed by SkipNow.
func (*F)SkipNow¶added ingo1.18
func (c *F) SkipNow()
SkipNow marks the test as having been skipped and stops its executionby callingruntime.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 fromother goroutines created during the test. Calling SkipNow does not stopthose other goroutines.
func (*F)TempDir¶added ingo1.18
func (c *F) TempDir()string
TempDir returns a temporary directory for the test to use.The directory is automatically removed when the test andall its subtests complete.Each subsequent call to TempDir returns a unique directory;if the directory creation fails, TempDir terminates the test by calling Fatal.
typeInternalBenchmark¶
InternalBenchmark is an internal type but exported because it is cross-package;it is part of the implementation of the "go test" command.
typeInternalExample¶
typeInternalFuzzTarget¶added ingo1.18
InternalFuzzTarget is an internal type but exported because it iscross-package; it is part of the implementation of the "go test" command.
typeInternalTest¶
InternalTest is an internal type but exported because it is cross-package;it is part of the implementation of the "go test" command.
typeM¶added ingo1.4
type M struct {// contains filtered or unexported fields}M is a type passed to a TestMain function to run the actual tests.
funcMainStart¶added ingo1.4
func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, 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.
typePB¶added ingo1.3
type PB struct {// contains filtered or unexported fields}A PB is used by RunParallel for running parallel benchmarks.
typeT¶
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.
A test ends when its Test function returns or calls any of the methodsT.FailNow,T.Fatal,T.Fatalf,T.SkipNow,T.Skip, orT.Skipf. Those methods, as well astheT.Parallel method, must be called only from the goroutine running theTest function.
The other reporting methods, such as the variations ofT.Log andT.Error,may be called simultaneously from multiple goroutines.
func (*T)Attr¶added ingo1.25.0
func (c *T) Attr(key, valuestring)
Attr emits a test attribute associated with this test.
The key must not contain whitespace.The value must not contain newlines or carriage returns.
The meaning of different attribute keys is left up tocontinuous integration systems and test frameworks.
Test attributes are emitted immediately in the test log,but they are intended to be treated as unordered.
func (*T)Chdir¶added ingo1.24.0
Chdir callsos.Chdir and uses Cleanup to restore the currentworking directory to its original value after the test. On Unix, italso sets PWD environment variable for the duration of the test.
Because Chdir affects the whole process, it cannot be usedin parallel tests or tests with parallel ancestors.
func (*T)Cleanup¶added ingo1.14
func (c *T) Cleanup(f func())
Cleanup registers a function to be called when the test (or subtest) and all itssubtests complete. Cleanup functions will be called in last added,first called order.
func (*T)Context¶added ingo1.24.0
Context returns a context that is canceled just beforeCleanup-registered functions are called.
Cleanup functions can wait for any resourcesthat shut down oncontext.Context.Done before the test or benchmark completes.
func (*T)Deadline¶added ingo1.15
Deadline reports the time at which the test binary will haveexceeded the timeout specified by the -timeout flag.
The ok result is false if the -timeout flag indicates “no timeout” (0).
func (*T)FailNow¶
func (c *T) FailNow()
FailNow marks the function as having failed and stops its executionby callingruntime.Goexit (which then runs all deferred calls in thecurrent goroutine).Execution will continue at the next test or benchmark.FailNow must be called from the goroutine running thetest or benchmark function, not from other goroutinescreated during the test. Calling FailNow does not stopthose other goroutines.
func (*T)Helper¶added ingo1.9
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.
func (*T)Log¶
func (c *T) Log(args ...any)
Log formats its arguments using default formatting, analogous tofmt.Println,and records the text in the error log. For tests, the text will be printed only ifthe test fails or the -test.v flag is set. For benchmarks, the text is alwaysprinted to avoid having performance depend on the value of the -test.v flag.It is an error to call Log after a test or benchmark returns.
func (*T)Logf¶
Logf formats its arguments according to the format, analogous tofmt.Printf, andrecords the text in the error log. A final newline is added if not provided. Fortests, the text will be printed only if the test fails or the -test.v flag isset. For benchmarks, the text is always printed to avoid having performancedepend on the value of the -test.v flag.It is an error to call Logf after a test or benchmark returns.
func (*T)Name¶added ingo1.8
func (c *T) Name()string
Name returns the name of the running (sub-) test or benchmark.
The name will include the name of the test along with the names ofany nested sub-tests. If two sibling sub-tests have the same name,Name will append a suffix to guarantee the returned name is unique.
func (*T)Output¶added ingo1.25.0
Output returns a Writer that writes to the same test output stream as TB.Log.The output is indented like TB.Log lines, but Output does notadd source locations or newlines. The output is internally linebuffered, and a call to TB.Log or the end of the test will implicitlyflush the buffer, followed by a newline. After a test function and all itsparents return, neither Output nor the Write method may be called.
func (*T)Parallel¶
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 inparallel with each other.
func (*T)Run¶added ingo1.7
Run runs f as a subtest of t called name. It runs f in a separate goroutineand 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 callsmust return before the outer test function for t returns.
func (*T)Setenv¶added ingo1.17
Setenv calls os.Setenv(key, value) and uses Cleanup torestore the environment variable to its original valueafter the test.
Because Setenv affects the whole process, it cannot be usedin parallel tests or tests with parallel ancestors.
func (*T)Skip¶added ingo1.1
func (c *T) Skip(args ...any)
Skip is equivalent to Log followed by SkipNow.
func (*T)SkipNow¶added ingo1.1
func (c *T) SkipNow()
SkipNow marks the test as having been skipped and stops its executionby callingruntime.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 fromother goroutines created during the test. Calling SkipNow does not stopthose other goroutines.
func (*T)Skipped¶added ingo1.1
func (c *T) Skipped()bool
Skipped reports whether the test was skipped.
func (*T)TempDir¶added ingo1.15
func (c *T) TempDir()string
TempDir returns a temporary directory for the test to use.The directory is automatically removed when the test andall its subtests complete.Each subsequent call to TempDir returns a unique directory;if the directory creation fails, TempDir terminates the test by calling Fatal.
typeTB¶added ingo1.2
type TB interface {Attr(key, valuestring)Cleanup(func())Error(args ...any)Errorf(formatstring, args ...any)Fail()FailNow()Failed()boolFatal(args ...any)Fatalf(formatstring, args ...any)Helper()Log(args ...any)Logf(formatstring, args ...any)Name()stringSetenv(key, valuestring)Chdir(dirstring)Skip(args ...any)SkipNow()Skipf(formatstring, args ...any)Skipped()boolTempDir()stringContext()context.ContextOutput()io.Writer// contains filtered or unexported methods}
Source Files¶
Directories¶
| Path | Synopsis |
|---|---|
Package fstest implements support for testing implementations and users of file systems. | Package fstest implements support for testing implementations and users of file systems. |
internal | |
testdeps Package testdeps provides access to dependencies needed by test execution. | Package testdeps provides access to dependencies needed by test execution. |
Package iotest implements Readers and Writers useful mainly for testing. | Package iotest implements Readers and Writers useful mainly 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 slogtest implements support for testing implementations of log/slog.Handler. | Package slogtest implements support for testing implementations of log/slog.Handler. |
Package synctest provides support for testing concurrent code. | Package synctest provides support for testing concurrent code. |