Overview
Thetesting package 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 test functions.
Types
T
TheT type is passed to test functions to manage test state and support formatted test logs.
FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf.
Methods
Equivalent to Log followed by Fail.
Equivalent to Logf followed by Fail.
Marks the function as having failed but continues execution.
Marks the function as having failed and stops its execution by calling runtime.Goexit.
Reports whether the function has failed.
Equivalent to Log followed by FailNow.
Equivalent to Logf followed by FailNow.
Marks the calling function as a test helper function. When printing file and line information, that function will be skipped.
Formats its arguments using default formatting, analogous to fmt.Println, and records the text in the error log.
Formats its arguments according to the format, analogous to fmt.Printf, and records the text in the error log.
Returns the name of the running test or subtest.
Signals that this test is to be run in parallel with (and only with) other parallel tests.
Runs f as a subtest of t called name. It returns whether the subtest succeeded.
Equivalent to Log followed by SkipNow.
Marks the test as having been skipped and stops its execution.
Equivalent to Logf followed by SkipNow.
Reports whether the test was skipped.
Returns a temporary directory for the test to use. The directory is automatically removed when the test completes.
Registers a function to be called when the test and all its subtests complete.
Calls os.Setenv and uses Cleanup to restore the environment variable to its original value after the test.
B
TheB type is passed to benchmark functions to manage benchmark timing and control the number of iterations.
Methods
Reports whether the benchmark should continue running iterations. Modern benchmarks should use Loop instead of iterating over b.N.
Resets the benchmark timer. This can be used to ignore expensive setup before the benchmark loop.
Starts timing a test. This function is called automatically before a benchmark starts.
Stops timing a test. This can be used to pause the timer while performing complex initialization.
Runs a benchmark in parallel. It creates multiple goroutines and distributes iterations among them.
Records the number of bytes processed in a single operation.
M
TheM type is a type passed to a TestMain function to run the actual tests.
Methods
Runs the tests. It returns an exit code to pass to os.Exit.
Functions
Reports whether the -test.short flag is set.
Reports whether the -test.v flag is set.
Reports whether the current code is being run in a test. This will report true in programs created by “go test”, false in programs created by “go build”.
Reports what the test coverage mode is set to. The values are “set”, “count”, or “atomic”.