Documentation
¶
Overview ¶
Package xtest provides fatal-by-default test helpers.
Naming convention:
Require* Fatal. Calls tb.Fatalf and stops the current test (or subtest).
The default. Use when continuing past a failure would test
nothing real (failed setup, nil response, blown budget).
Assert* Soft. Calls tb.Errorf and continues, so one run reports every
violation. Combine with t.Run subtests. Use only when the
checks are genuinely independent.
Eventually / Never / WaitFor* Polling helpers; fail on timeout.
Golden* Golden-file helpers.
Parameters of type testing.TB are named tb (not t).
Index ¶
- Constants
- Variables
- func AllocsPerRun(runs int, fn func()) float64
- func Eventually(tb testing.TB, timeout time.Duration, cond func() bool)
- func EventuallyE(tb testing.TB, timeout time.Duration, fn func() error) error
- func EventuallyEvery(tb testing.TB, timeout, interval time.Duration, cond func() bool)
- func EventuallyEveryE(tb testing.TB, timeout, interval time.Duration, fn func() error) error
- func ExpectFatal(t *testing.T, testName, envVar, wantSubstr string, fn func(t *testing.T))
- func GoldenJSON(tb testing.TB, name string, got any)
- func GoldenJSONWithUpdate(tb testing.TB, name string, got any, update bool)
- func Never(tb testing.TB, duration time.Duration, cond func() bool)
- func RequireAllocsBetween(tb testing.TB, runs int, minimum, maximum float64, fn func())
- func RequireGoroutinesAtMost(tb testing.TB, baseline int, timeout time.Duration, allowedDelta int)
- func RequireMaxAlloc(tb testing.TB, runs int, limit float64, fn func())
- func RequireNoGoroutineLeak(tb testing.TB, baseline int, timeout time.Duration)
- func RequireZeroAlloc(tb testing.TB, runs int, fn func())
- func RunParallel(tb testing.TB, count int, fn func(index int) error)
- func WaitForAtLeast(tb testing.TB, counter *atomic.Int32, minimum int32, timeout time.Duration)
- func WaitForAtMost(tb testing.TB, counter *atomic.Int32, maximum int32, timeout time.Duration)
- func WaitForSignal(tb testing.TB, signal <-chan struct{}, timeout time.Duration)
- func WaitForValue[T comparable](tb testing.TB, timeout time.Duration, want T, get func() T) T
- type Gate
- type Latch
Constants ¶
const DefaultPollInterval = 2 * time.Millisecond
Variables ¶
var UpdateGolden = flag.Bool("xtest.update", false, "rewrite golden files")
Functions ¶
func AllocsPerRun ¶
func EventuallyEvery ¶
func EventuallyEveryE ¶
func ExpectFatal ¶
ExpectFatal isolates subprocess crash testing. It runs fn in a child process and asserts that it fails with wantSubstr in its output. It is used to test test-helpers (verifying that they correctly call t.Fatal).
func GoldenJSONWithUpdate ¶
func RequireAllocsBetween ¶
func RequireGoroutinesAtMost ¶
RequireGoroutinesAtMost waits for goroutines to settle at or below baseline+allowedDelta. The allowance is useful because the testing runtime and unrelated parallel tests may transiently create goroutines.
func RequireNoGoroutineLeak ¶
RequireNoGoroutineLeak waits until the goroutine count returns to baseline.
func RequireZeroAlloc ¶
func RunParallel ¶
RunParallel starts count workers behind one barrier, waits for all of them, and reports the first worker error after every worker has stopped.
The callback must return an error instead of calling Fatal from a worker; this keeps failure reporting deterministic and guarantees the parent test does not continue while worker goroutines are still running.
func WaitForAtLeast ¶
func WaitForAtMost ¶
func WaitForSignal ¶
WaitForSignal waits for a one-shot signal and fails with a useful timeout.
func WaitForValue ¶
func WaitForValue[T comparable](tb testing.TB, timeout time.Duration, want T, get func() T) T
Types ¶
type Gate ¶
type Gate struct {
// contains filtered or unexported fields
}
Gate provides a concurrent barrier for thundering-herd tests.