Documentation
¶
Overview ¶
Package testing provides support for automated testing of Go packages.
Index ¶
- func AllocsPerRun(runs int, f func()) (avg float64)
- func CoverMode() string
- func Init()
- func Short() bool
- func Testing() bool
- func Verbose() bool
- type B
- func (c *B) Cleanup(f func())
- func (c *B) Error(args ...interface{})
- func (c *B) Errorf(format string, args ...interface{})
- func (c *B) Fail()
- func (c *B) FailNow()
- func (c *B) Failed() bool
- func (c *B) Fatal(args ...interface{})
- func (c *B) Fatalf(format string, args ...interface{})
- func (c *B) Helper()
- func (c *B) Log(args ...interface{})
- func (c *B) Logf(format string, args ...interface{})
- func (c *B) Name() string
- func (b *B) ReportAllocs()
- 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 (c *B) Setenv(key, value string)
- func (c *B) Skip(args ...interface{})
- func (c *B) SkipNow()
- func (c *B) Skipf(format string, args ...interface{})
- func (c *B) Skipped() bool
- func (b *B) StartTimer()
- func (b *B) StopTimer()
- func (c *B) TempDir() string
- type BenchmarkResult
- type F
- func (f *F) Add(args ...interface{})
- func (c *F) Cleanup(f func())
- func (c *F) Error(args ...interface{})
- func (c *F) Errorf(format string, args ...interface{})
- func (c *F) Fail()
- func (c *F) FailNow()
- func (c *F) Failed() bool
- func (c *F) Fatal(args ...interface{})
- func (c *F) Fatalf(format string, args ...interface{})
- func (f *F) Fuzz(ff interface{})
- func (c *F) Helper()
- func (c *F) Log(args ...interface{})
- func (c *F) Logf(format string, args ...interface{})
- func (c *F) Name() string
- func (c *F) Setenv(key, value string)
- func (c *F) Skip(args ...interface{})
- func (c *F) SkipNow()
- func (c *F) Skipf(format string, args ...interface{})
- func (c *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) Cleanup(f func())
- func (t *T) Deadline() (deadline time.Time, ok bool)
- func (c *T) Error(args ...interface{})
- func (c *T) Errorf(format string, args ...interface{})
- func (c *T) Fail()
- func (c *T) FailNow()
- func (c *T) Failed() bool
- func (c *T) Fatal(args ...interface{})
- func (c *T) Fatalf(format string, args ...interface{})
- func (c *T) Helper()
- func (c *T) Log(args ...interface{})
- func (c *T) Logf(format string, args ...interface{})
- func (c *T) Name() string
- func (t *T) Parallel()
- func (t *T) Run(name string, f func(t *T)) bool
- func (c *T) Setenv(key, value string)
- func (c *T) Skip(args ...interface{})
- func (c *T) SkipNow()
- func (c *T) Skipf(format string, args ...interface{})
- func (c *T) Skipped() bool
- func (c *T) TempDir() string
- type TB
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllocsPerRun ¶ added in v0.21.0
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.
Not implemented.
func CoverMode ¶ added in v0.22.0
func CoverMode() string
CoverMode reports what the test coverage mode is set to.
Test coverage is not supported; this returns the empty string.
func Init ¶ added in v0.20.0
func Init()
Init registers testing flags. It has no effect if it has already run.
Types ¶
type B ¶ added in v0.8.0
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.
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 output 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) Cleanup ¶ added in v0.22.0
func (c *B) Cleanup(f func())
Cleanup registers a function to be called when the test (or subtest) and all its subtests complete. Cleanup functions will be called in last added, first called order.
func (*B) Error ¶ added in v0.8.0
func (c *B) Error(args ...interface{})
Error is equivalent to Log followed by Fail.
func (*B) Errorf ¶ added in v0.8.0
func (c *B) Errorf(format string, args ...interface{})
Errorf is equivalent to Logf followed by Fail.
func (*B) Fail ¶ added in v0.8.0
func (c *B) Fail()
Fail marks the function as having failed but continues execution.
func (*B) FailNow ¶ added in v0.8.0
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).
func (*B) Failed ¶ added in v0.8.0
func (c *B) Failed() bool
Failed reports whether the function has failed.
func (*B) Fatal ¶ added in v0.8.0
func (c *B) Fatal(args ...interface{})
Fatal is equivalent to Log followed by FailNow.
func (*B) Fatalf ¶ added in v0.8.0
func (c *B) Fatalf(format string, args ...interface{})
Fatalf is equivalent to Logf followed by FailNow.
func (*B) Helper ¶ added in v0.17.0
func (c *B) Helper()
Helper is not implemented, it is only provided for compatibility.
func (*B) Log ¶ added in v0.8.0
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 ¶ added in v0.8.0
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 ¶ added in v0.8.0
func (c *B) Name() string
Name returns the name of the running test or benchmark.
func (*B) ReportAllocs ¶ added in v0.21.0
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 ¶ added in v0.17.0
func (b *B) ResetTimer()
ResetTimer zeroes the elapsed benchmark time and memory allocation counters and deletes user-reported metrics.
func (*B) Run ¶ added in v0.17.0
Run benchmarks f as a subbenchmark with the given name. It reports true if the subbenchmark succeeded.
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) SetBytes ¶ added in v0.17.0
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) Setenv ¶ added in v0.27.0
func (c *B) Setenv(key, value string)
Setenv calls os.Setenv(key, value) and uses Cleanup to restore the environment variable to its original value after the test.
func (*B) Skip ¶ added in v0.8.0
func (c *B) Skip(args ...interface{})
Skip is equivalent to Log followed by SkipNow.
func (*B) SkipNow ¶ added in v0.8.0
func (c *B) SkipNow()
SkipNow marks the test as having been skipped and stops its execution by calling runtime.Goexit.
func (*B) Skipf ¶ added in v0.8.0
func (c *B) Skipf(format string, args ...interface{})
Skipf is equivalent to Logf followed by SkipNow.
func (*B) Skipped ¶ added in v0.8.0
func (c *B) Skipped() bool
Skipped reports whether the test was skipped.
func (*B) StartTimer ¶ added in v0.20.0
func (b *B) StartTimer()
StartTimer starts timing a test. This function is called automatically before a benchmark starts, but it can also be used to resume timing after a call to StopTimer.
func (*B) StopTimer ¶ added in v0.20.0
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.
func (*B) TempDir ¶ added in v0.23.0
func (c *B) TempDir() string
TempDir returns a temporary directory for the test to use. The directory is automatically removed by Cleanup when the test and all its subtests complete. Each subsequent call to t.TempDir returns a unique directory; if the directory creation fails, TempDir terminates the test by calling Fatal.
type BenchmarkResult ¶ added in v0.20.0
type BenchmarkResult struct { N int // The number of iterations. T time.Duration // The total time taken. Bytes int64 // Bytes processed in one iteration. MemAllocs uint64 // The total number of memory allocations. MemBytes uint64 // The total number of bytes allocated. }
BenchmarkResult contains the results of a benchmark run.
func Benchmark ¶ added in v0.20.0
func Benchmark(f func(b *B)) BenchmarkResult
Benchmark benchmarks a single function. It is 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 ¶ added in v0.20.0
func (r BenchmarkResult) AllocedBytesPerOp() int64
AllocedBytesPerOp returns the "B/op" metric, which is calculated as r.MemBytes / r.N.
func (BenchmarkResult) AllocsPerOp ¶ added in v0.20.0
func (r BenchmarkResult) AllocsPerOp() int64
AllocsPerOp returns the "allocs/op" metric, which is calculated as r.MemAllocs / r.N.
func (BenchmarkResult) MemString ¶ added in v0.26.0
func (r BenchmarkResult) MemString() string
MemString returns r.AllocedBytesPerOp and r.AllocsPerOp in the same format as 'go test'.
func (BenchmarkResult) NsPerOp ¶ added in v0.20.0
func (r BenchmarkResult) NsPerOp() int64
NsPerOp returns the "ns/op" metric.
func (BenchmarkResult) String ¶ added in v0.22.0
func (r BenchmarkResult) String() string
String returns a summary of the benchmark results. It follows the benchmark result line format from https://golang.org/design/14313-benchmark-format, not including the benchmark name. Extra metrics override built-in metrics of the same name. String does not include allocs/op or B/op, since those are reported by MemString.
type F ¶ added in v0.23.0
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 can find and report potential bugs in the code being tested.
A fuzz test runs the seed corpus by default, which includes entries provided by (*F).Add and entries in the testdata/fuzz/<FuzzTestName> directory. After any necessary setup and calls to (*F).Add, the fuzz test must then call (*F).Fuzz to provide the fuzz target. See the testing package documentation for an example, and see the F.Fuzz and F.Add method documentation for details.
*F methods can only be called before (*F).Fuzz. Once the test is executing the fuzz target, only (*T) methods can be used. The only *F methods that are allowed in the (*F).Fuzz function are (*F).Failed and (*F).Name.
func (*F) Add ¶ added in v0.23.0
func (f *F) Add(args ...interface{})
Add will add the arguments to the seed corpus for the fuzz test. This will be a no-op if called after or within the fuzz target, and args must match the arguments for the fuzz target.
func (*F) Cleanup ¶ added in v0.23.0
func (c *F) Cleanup(f func())
Cleanup registers a function to be called when the test (or subtest) and all its subtests complete. Cleanup functions will be called in last added, first called order.
func (*F) Error ¶ added in v0.23.0
func (c *F) Error(args ...interface{})
Error is equivalent to Log followed by Fail.
func (*F) Errorf ¶ added in v0.23.0
func (c *F) Errorf(format string, args ...interface{})
Errorf is equivalent to Logf followed by Fail.
func (*F) Fail ¶ added in v0.23.0
func (c *F) Fail()
Fail marks the function as having failed but continues execution.
func (*F) FailNow ¶ added in v0.23.0
func (c *F) 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).
func (*F) Failed ¶ added in v0.23.0
func (c *F) Failed() bool
Failed reports whether the function has failed.
func (*F) Fatal ¶ added in v0.23.0
func (c *F) Fatal(args ...interface{})
Fatal is equivalent to Log followed by FailNow.
func (*F) Fatalf ¶ added in v0.23.0
func (c *F) Fatalf(format string, args ...interface{})
Fatalf is equivalent to Logf followed by FailNow.
func (*F) Fuzz ¶ added in v0.23.0
func (f *F) Fuzz(ff interface{})
Fuzz runs the fuzz function, ff, for fuzz testing. If ff fails for a set of arguments, those arguments will be added to the seed corpus.
ff must be a function with no return value whose first argument is *T and whose 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. Use the corresponding *T method instead. The only *F methods that are allowed in the (*F).Fuzz function are (*F).Failed and (*F).Name.
This function should be fast and deterministic, and its behavior should not depend on shared state. No mutatable input arguments, or pointers to them, should be retained between executions of the fuzz function, as the memory backing them may be mutated during a subsequent invocation. ff must not modify 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.Fuzz should be called exactly once, unless F.Skip or F.Fail is called beforehand.
func (*F) Helper ¶ added in v0.23.0
func (c *F) Helper()
Helper is not implemented, it is only provided for compatibility.
func (*F) Log ¶ added in v0.23.0
func (c *F) 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 (*F) Logf ¶ added in v0.23.0
func (c *F) 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 (*F) Name ¶ added in v0.23.0
func (c *F) Name() string
Name returns the name of the running test or benchmark.
func (*F) Setenv ¶ added in v0.27.0
func (c *F) Setenv(key, value string)
Setenv calls os.Setenv(key, value) and uses Cleanup to restore the environment variable to its original value after the test.
func (*F) Skip ¶ added in v0.23.0
func (c *F) Skip(args ...interface{})
Skip is equivalent to Log followed by SkipNow.
func (*F) SkipNow ¶ added in v0.23.0
func (c *F) SkipNow()
SkipNow marks the test as having been skipped and stops its execution by calling runtime.Goexit.
func (*F) Skipf ¶ added in v0.23.0
func (c *F) Skipf(format string, args ...interface{})
Skipf is equivalent to Logf followed by SkipNow.
func (*F) Skipped ¶ added in v0.23.0
func (c *F) Skipped() bool
Skipped reports whether the test was skipped.
func (*F) TempDir ¶ added in v0.23.0
func (c *F) TempDir() string
TempDir returns a temporary directory for the test to use. The directory is automatically removed by Cleanup when the test and all its subtests complete. Each subsequent call to t.TempDir returns a unique directory; if the directory creation fails, TempDir terminates the test by calling Fatal.
type InternalBenchmark ¶ added in v0.15.0
InternalBenchmark is an internal type but exported because it is cross-package; it is part of the implementation of the "go test" command.
type InternalExample ¶ added in v0.15.0
type InternalFuzzTarget ¶ added in v0.23.0
InternalFuzzTarget is an internal type but exported because it is cross-package; it is part of the implementation of the "go test" command.
type InternalTest ¶ added in v0.15.0
InternalTest is a reference to a test that should be called during a test suite run.
type M ¶
type M struct { // tests is a list of the test names to execute Tests []InternalTest Benchmarks []InternalBenchmark // contains filtered or unexported fields }
M is a test suite.
func MainStart ¶ added in v0.15.0
func MainStart(deps interface{}, 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.
type PB ¶ added in v0.22.0
type PB struct { }
A PB is used by RunParallel for running parallel benchmarks.
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 output when done.
func (*T) Cleanup ¶ added in v0.22.0
func (c *T) Cleanup(f func())
Cleanup registers a function to be called when the test (or subtest) and all its subtests complete. Cleanup functions will be called in last added, first called order.
func (*T) Deadline ¶ added in v0.33.0
Deadline reports the time at which the test binary will have exceeded the timeout specified by the -timeout flag.
The ok result is false if the -timeout flag indicates “no timeout” (0). For now tinygo always return 0, false.
Not Implemented.
func (*T) Error ¶
func (c *T) Error(args ...interface{})
Error is equivalent to Log followed by Fail.
func (*T) Errorf ¶ added in v0.8.0
func (c *T) Errorf(format string, args ...interface{})
Errorf is equivalent to Logf followed by Fail.
func (*T) Fail ¶
func (c *T) Fail()
Fail marks the function as having failed but continues execution.
func (*T) FailNow ¶ added in v0.8.0
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).
func (*T) Failed ¶ added in v0.8.0
func (c *T) Failed() bool
Failed reports whether the function has failed.
func (*T) Fatal ¶ added in v0.8.0
func (c *T) Fatal(args ...interface{})
Fatal is equivalent to Log followed by FailNow.
func (*T) Fatalf ¶ added in v0.8.0
func (c *T) Fatalf(format string, args ...interface{})
Fatalf is equivalent to Logf followed by FailNow.
func (*T) Helper ¶ added in v0.17.0
func (c *T) Helper()
Helper is not implemented, it is only provided for compatibility.
func (*T) Log ¶ added in v0.8.0
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 ¶ added in v0.8.0
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 ¶ added in v0.8.0
func (c *T) Name() string
Name returns the name of the running test or benchmark.
func (*T) Parallel ¶ added in v0.21.0
func (t *T) Parallel()
Parallel is not implemented, it is only provided for compatibility.
func (*T) Run ¶ added in v0.17.0
Run runs f as a subtest of t called name. It waits until the subtest is finished and returns whether the subtest succeeded.
func (*T) Setenv ¶ added in v0.27.0
func (c *T) Setenv(key, value string)
Setenv calls os.Setenv(key, value) and uses Cleanup to restore the environment variable to its original value after the test.
func (*T) Skip ¶ added in v0.8.0
func (c *T) Skip(args ...interface{})
Skip is equivalent to Log followed by SkipNow.
func (*T) SkipNow ¶ added in v0.8.0
func (c *T) SkipNow()
SkipNow marks the test as having been skipped and stops its execution by calling runtime.Goexit.
func (*T) Skipf ¶ added in v0.8.0
func (c *T) Skipf(format string, args ...interface{})
Skipf is equivalent to Logf followed by SkipNow.
func (*T) Skipped ¶ added in v0.8.0
func (c *T) Skipped() bool
Skipped reports whether the test was skipped.
func (*T) TempDir ¶ added in v0.23.0
func (c *T) TempDir() string
TempDir returns a temporary directory for the test to use. The directory is automatically removed by Cleanup when the test and all its subtests complete. Each subsequent call to t.TempDir returns a unique directory; if the directory creation fails, TempDir terminates the test by calling Fatal.
type TB ¶ added in v0.8.0
type TB interface { Cleanup(func()) Error(args ...interface{}) Errorf(format string, args ...interface{}) Fail() FailNow() Failed() bool Fatal(args ...interface{}) Fatalf(format string, args ...interface{}) Helper() Log(args ...interface{}) Logf(format string, args ...interface{}) Name() string Setenv(key, value string) Skip(args ...interface{}) SkipNow() Skipf(format string, args ...interface{}) Skipped() bool TempDir() string }
TB is the interface common to T and B.