testing

Imports

Imports #

"flag"
"fmt"
"os"
"runtime"
"time"
"bytes"
"fmt"
"io"
"os"
"strings"
"time"
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"

Constants & Variables

benchTime var #

var benchTime = *ast.CallExpr

chatty var #

Report as tests are run; default is silent for success.

var chatty = *ast.CallExpr

cpuList var #

var cpuList []int

cpuListStr var #

var cpuListStr = *ast.CallExpr

cpuProfile var #

var cpuProfile = *ast.CallExpr

match var #

var match = *ast.CallExpr

matchBenchmarks var #

var matchBenchmarks = *ast.CallExpr

memProfile var #

var memProfile = *ast.CallExpr

memProfileRate var #

var memProfileRate = *ast.CallExpr

parallel var #

var parallel = *ast.CallExpr

short var #

The short flag requests that tests run more quickly, but its functionality is provided by test writers themselves. The testing package is just its home. The all.bash installation script sets it to make installation more efficient, but by default the flag is off so a plain "go test" will do a full test of the package.

var short = *ast.CallExpr

timeout var #

var timeout = *ast.CallExpr

timer var #

var timer *time.Timer

Structs

B struct #

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

type B struct {
common
N int
benchmark InternalBenchmark
bytes int64
timerOn bool
result BenchmarkResult
}

BenchmarkResult struct #

The results of a benchmark run.

type BenchmarkResult struct {
N int
T time.Duration
Bytes int64
}

InternalBenchmark struct #

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

type InternalBenchmark struct {
Name string
F func(b *B)
}

InternalExample struct #

type InternalExample struct {
Name string
F func()
Output string
}

InternalTest struct #

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

type InternalTest struct {
Name string
F func(*T)
}

T struct #

T is a type passed to Test functions to manage test state and support formatted test logs. Logs are accumulated during execution and dumped to standard error when done.

type T struct {
common
name string
startParallel chan bool
}

common struct #

common holds the elements common between T and B and captures common methods such as Errorf.

type common struct {
output []byte
failed bool
start time.Time
duration time.Duration
self any
signal chan any
}

Functions

Error method #

Error is equivalent to Log() followed by Fail().

func (c *common) Error(args ...any)

Errorf method #

Errorf is equivalent to Logf() followed by Fail().

func (c *common) Errorf(format string, args ...any)

Fail method #

Fail marks the function as having failed but continues execution.

func (c *common) Fail()

FailNow method #

FailNow marks the function as having failed and stops its execution. Execution will continue at the next Test.

func (c *common) FailNow()

Failed method #

Failed reports whether the function has failed.

func (c *common) Failed() bool

Fatal method #

Fatal is equivalent to Log() followed by FailNow().

func (c *common) Fatal(args ...any)

Fatalf method #

Fatalf is equivalent to Logf() followed by FailNow().

func (c *common) Fatalf(format string, args ...any)

Log method #

Log formats its arguments using default formatting, analogous to Println(), and records the text in the error log.

func (c *common) Log(args ...any)

Logf method #

Logf formats its arguments according to the format, analogous to Printf(), and records the text in the error log.

func (c *common) Logf(format string, args ...any)

Main function #

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

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

NsPerOp method #

func (r BenchmarkResult) NsPerOp() int64

Parallel method #

Parallel signals that this test is to be run in parallel with (and only with) other parallel tests in this CPU group.

func (t *T) Parallel()

ResetTimer method #

ResetTimer sets the elapsed benchmark time to zero. It does not affect whether the timer is running.

func (b *B) ResetTimer()

RunBenchmarks function #

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

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

RunExamples function #

func RunExamples(examples []InternalExample) (ok bool)

RunTests function #

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

SetBytes method #

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 *B) SetBytes(n int64)

Short function #

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

func Short() bool

StartTimer method #

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 *B) StartTimer()

StopTimer method #

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 *B) StopTimer()

String method #

func (r BenchmarkResult) String() string

after function #

after runs after all testing.

func after()

alarm function #

alarm is called if the timeout expires.

func alarm()

before function #

before runs before all testing.

func before()

decorate function #

decorate inserts the final newline if needed and indentation tabs for formatting. If addFileLine is true, it also prefixes the string with the file and line of the call site.

func decorate(s string, addFileLine bool) string

launch method #

launch launches the benchmark function. It gradually increases the number of benchmark iterations until the benchmark runs for a second in order to get a reasonable measurement. It prints timing information in this form testing.BenchmarkHello 100000 19 ns/op launch is run by the fun function as a separate goroutine.

func (b *B) launch()

log method #

log generates the output. It's always at the same stack depth.

func (c *common) log(s string)

max function #

func max(x int, y int) int

mbPerSec method #

func (r BenchmarkResult) mbPerSec() float64

min function #

func min(x int, y int) int

nsPerOp method #

func (b *B) nsPerOp() int64

parseCpuList function #

func parseCpuList()

report method #

func (t *T) report()

roundDown10 function #

roundDown10 rounds a number down to the nearest power of 10.

func roundDown10(n int) int

roundUp function #

roundUp rounds x up to a number of the form [1eX, 2eX, 5eX].

func roundUp(n int) int

run method #

run times the benchmark function in a separate goroutine.

func (b *B) run() BenchmarkResult

runN method #

runN runs a single benchmark for the specified number of iterations.

func (b *B) runN(n int)

startAlarm function #

startAlarm starts an alarm if requested.

func startAlarm()

stopAlarm function #

stopAlarm turns off the alarm.

func stopAlarm()

tRunner function #

func tRunner(t *T, test *InternalTest)

trimOutput method #

trimOutput shortens the output from a benchmark, which can be very long.

func (b *B) trimOutput()

Generated with Arrow