testt

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package testt (for test tools), provides a couple of useful helpers for common test patterns. To be used as a optional companion of the assert/check library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Context

func Context(t testing.TB) context.Context

Context creates a context and attaches its cancellation function to the test execution's Cleanup. Given the execution of tests, this means that the context is canceled *after* the test functions defers have run.

func ContextWithTimeout

func ContextWithTimeout(t testing.TB, dur time.Duration) context.Context

ContextWithTimeout creates a context with the specified timeout, and attaches the cancellation to the test execution's cleanup.

func GoroutinesAtMost added in v0.15.0

func GoroutinesAtMost(t testing.TB, limit int, wait time.Duration)

GoroutinesAtMost fails the test (with Fatal) unless the number of running goroutines drops to limit or below within wait. Goroutines wind down asynchronously after a cancellation, so this polls until the deadline rather than checking once.

func Log

func Log(t testing.TB, args ...any)

Log calls t.Log with the given arguments *if* the test has failed.

func Logf

func Logf(t testing.TB, format string, args ...any)

Logf calls t.Log with the given arguments *if* the test has failed.

func Must added in v0.10.0

func Must[T any](out T, err error) func(t testing.TB) T

Must is used to capture the output of a function that returns an error and an arbitry value and simplify call sites in test code. The function that returns makes a fatal assertion if the error is non-nil, and returns the object.

func NoGoroutineLeak added in v0.15.0

func NoGoroutineLeak(t testing.TB, wait time.Duration) func()

NoGoroutineLeak records the current number of goroutines and returns a function that checks, waiting up to wait, that the count has returned to that baseline. Use it as:

defer testt.NoGoroutineLeak(t, 5*time.Second)()

func Ticker

func Ticker(t testing.TB, dur time.Duration) *time.Ticker

Ticker creates a new time.Ticker with the specified interval, and manages the cleanup of the ticker.

func Timer

func Timer(t testing.TB, dur time.Duration) *time.Timer

Timer creates a new time.Timer with the specified starting duration, and manages the cleanup of the timer.

func WithJustifiedParallelism added in v0.15.0

func WithJustifiedParallelism(t *testing.T, reason string)

WithJustifiedParallelism marks the test as parallel by calling t.Parallel(), but only if the caller supplies a non-empty reason explaining why concurrency is worthwhile for this test.

This checks reason only checked for presence: an empty string, fails the test immediately via t.Fatal and does not call t.Parallel.

Types

This section is empty.

Jump to

Keyboard shortcuts

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