testsetup

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddSetup

func AddSetup(t *testing.T, f func(t *testing.T))

Add adds a setup function to the list of setup functions to be run when `testsetup.RunSetup` is called.

It must be called before `testsetup.RunSetup`.

Functions added are removed after the current test finishes using `t.Cleanup`. This ensures that setup functions added only affect the current test and its children.

func AssertFatalContains

func AssertFatalContains(t *testing.T, f func(), contains string)

AssertFatalContains is a helper to run a block of code with FatailIfError being mocked to panic with the given message so we can actually assert on it.

This is useful for testing test utilities.

func FatalIfError

func FatalIfError(t *testing.T, err error)

FatalIfError is a helper function that calls t.Fatalf if err is not nil.

It also formats the error with "%+v" to include the stack traces.

func FatalIfErrorElseReturn

func FatalIfErrorElseReturn[T any](t *testing.T, err error, val T) T

func ModifyLet

func ModifyLet[A any](t *testing.T, l LetWrapper[A], f func(t *testing.T, super func() A) A)

ModifyLet modifies the value of the LetWrapper for the current test.

It receives a function that can access the previous constructor through the `super` function.

It must be called before RunSetup. Once setup starts, Let values may already be built, so calling ModifyLet (including from a setup function) fails the test instead of silently keeping a stale cached value.

func ModifyLetValue

func ModifyLetValue[A immutable](t *testing.T, l LetWrapper[A], a A)

ModifyLetValue is a variant of ModifyLet that uses a constant value.

It must be called before RunSetup; see ModifyLet.

func RunSetup

func RunSetup(t *testing.T)

RunSetup runs setup functions added by `testsetup.AddSetup`.

Setup callbacks must not call ModifyLet or ModifyLetValue; those helpers must run before setup starts.

A Let can only be evaluated after RunSetup starts and before its lifecycle cleanup runs. Cached Let values are reset by that cleanup, after teardown callbacks registered by setup functions or the test body.

Types

type LetWrapper

type LetWrapper[A any] func() A

LetWrapper represents a value that is lazy loaded when requested for.

See `testsetup.Let` and `testsetup.LetValue` for creating instances of `LetWrapper`.

func Let

func Let[A any](t *testing.T, constructor func(t *testing.T) A) LetWrapper[A]

Let creates a LetWrapper that lazily loads the value when requested for by calling the provider constructor.

A Let can only be evaluated while RunSetup is active. Calls before RunSetup or after the final cleanup fail the test with the same "Must call RunSetup" error as a call before RunSetup.

RunSetup cleanup stops new constructions and waits for constructions already in flight before the final reset. Cached reads may complete while cleanup waits; starting construction after cleanup begins fails. Concurrent evaluation is supported. Recursive construction (a Let whose constructor evaluates itself) deadlocks.

func LetValue

func LetValue[A immutable](t *testing.T, x A) LetWrapper[A]

LetValue is a shorthand for Let that uses a constant value.

func (LetWrapper[A]) Eager

func (l LetWrapper[A]) Eager(t *testing.T) LetWrapper[A]

Eager forces the value to be loaded eagerly for the current test.

Shorthand for:

AddSetup(t, func(t *testing.T) { letWrapper() })

Jump to

Keyboard shortcuts

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