gotesting

package
v2.0.0-beta.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunM

func RunM(m *testing.M) int

RunM runs the tests and benchmarks using CI visibility.

Types

type B

type B testing.B

B is a type alias for testing.B to provide additional methods for CI visibility.

func GetBenchmark

func GetBenchmark(t *testing.B) *B

GetBenchmark is a helper to return *gotesting.B from *testing.B. Internally, it is just a (*gotesting.B)(b) cast.

func (*B) Context

func (ddb *B) Context() context.Context

Context returns the CI Visibility context of the Test span. This may be used to create test's children spans useful for integration tests.

func (*B) Elapsed

func (ddb *B) Elapsed() time.Duration

Elapsed returns the measured elapsed time of the benchmark. The duration reported by Elapsed matches the one measured by StartTimer, StopTimer, and ResetTimer.

func (*B) Error

func (ddb *B) Error(args ...any)

Error is equivalent to Log followed by Fail.

func (*B) Errorf

func (ddb *B) Errorf(format string, args ...any)

Errorf is equivalent to Logf followed by Fail.

func (*B) Fail

func (ddb *B) Fail()

Fail marks the function as having failed but continues execution.

func (*B) FailNow

func (ddb *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) Fatal

func (ddb *B) Fatal(args ...any)

Fatal is equivalent to Log followed by FailNow.

func (*B) Fatalf

func (ddb *B) Fatalf(format string, args ...any)

Fatalf is equivalent to Logf followed by FailNow.

func (*B) ReportAllocs

func (ddb *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) ReportMetric

func (ddb *B) ReportMetric(n float64, unit string)

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 contains any 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.

func (*B) ResetTimer

func (ddb *B) ResetTimer()

ResetTimer zeroes the elapsed benchmark time and memory allocation counters and deletes user-reported metrics. It does not affect whether the timer is running.

func (*B) Run

func (ddb *B) Run(name string, f func(*testing.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

func (ddb *B) RunParallel(body func(*testing.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.

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.

func (*B) SetBytes

func (ddb *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

func (ddb *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

func (ddb *B) Skip(args ...any)

Skip is equivalent to Log followed by SkipNow.

func (*B) SkipNow

func (ddb *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. 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

func (ddb *B) Skipf(format string, args ...any)

Skipf is equivalent to Logf followed by SkipNow.

func (*B) StartTimer

func (ddb *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

func (ddb *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 M

type M testing.M

M is a wrapper around testing.M to provide instrumentation.

func (*M) Run

func (ddm *M) Run() int

Run initializes CI Visibility, instruments tests and benchmarks, and runs them.

type T

type T testing.T

T is a type alias for testing.T to provide additional methods for CI visibility.

func GetTest

func GetTest(t *testing.T) *T

GetTest is a helper to return *gotesting.T from *testing.T. Internally, it is just a (*gotesting.T)(t) cast.

func (*T) Context

func (ddt *T) Context() context.Context

Context returns the CI Visibility context of the Test span. This may be used to create test's children spans useful for integration tests.

func (*T) Deadline

func (ddt *T) Deadline() (deadline time.Time, ok bool)

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

func (*T) Error

func (ddt *T) Error(args ...any)

Error is equivalent to Log followed by Fail.

func (*T) Errorf

func (ddt *T) Errorf(format string, args ...any)

Errorf is equivalent to Logf followed by Fail.

func (*T) Fail

func (ddt *T) Fail()

Fail marks the function as having failed but continues execution.

func (*T) FailNow

func (ddt *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) Fatal

func (ddt *T) Fatal(args ...any)

Fatal is equivalent to Log followed by FailNow.

func (*T) Fatalf

func (ddt *T) Fatalf(format string, args ...any)

Fatalf is equivalent to Logf followed by FailNow.

func (*T) Parallel

func (ddt *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

func (ddt *T) Run(name string, f func(*testing.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) Setenv

func (ddt *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. Because Setenv affects the whole process, it cannot be used in parallel tests or tests with parallel ancestors.

func (*T) Skip

func (ddt *T) Skip(args ...any)

Skip is equivalent to Log followed by SkipNow.

func (*T) SkipNow

func (ddt *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. 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

func (ddt *T) Skipf(format string, args ...any)

Skipf is equivalent to Logf followed by SkipNow.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL