Documentation
¶
Overview ¶
Package tst provides a collection of small, focused helpers designed to make Go tests leaner and more readable. It aims for a minimal learning curve by providing intuitive functions for common testing patterns like error handling, value unwrapping, and deep equality checks.
Index ¶
- func Be(ok bool, t Test)
- func Do[V any](v V, err error) func(t Test) V
- func Do2[V1, V2 any](v1 V1, v2 V2, err error) func(t Test) (V1, V2)
- func DoB[V any](v V, ok bool) func(t Test) V
- func Err(errorSubMessage string, err error, t Test)
- func Go(t PTest) context.Context
- func Is[T any](want, got T, t Test, opts ...cmp.Option)
- func IsSubString(want, got string, t Test)
- func Ko(t Test)
- func LoadGolden[V any](name string, t Test) V
- func No(err error, t Test)
- func RecordGolden(name string, v any, t Test)
- func With[T any](val *T, temp T, t Test)
- type PTest
- type Test
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Be ¶
Be stops the test immediately (t.Fatalf) if ok is false.
Example:
tst.Be(len(list) > 0, t)
func Do ¶
Do unwraps a result and stops the test immediately (t.Fatalf) if an error occurred.
Example:
f := tst.Do(os.Open("file.txt"))(t)
defer f.Close()
func Do2 ¶
Do2 is like Do, but for functions that return two values and an error.
Example:
v1, v2 := tst.Do2(returnsTwoValuesAndError())(t)
func DoB ¶
DoB unwraps a result and stops the test immediately (t.Fatalf) if ok is false.
Example:
val := tst.DoB(syncMap.Load("foo"))(t)
func Err ¶
Err checks if the provided error is not nil and contains an optional message.
It calls t.Fatalf if err is nil, and t.Errorf if the message doesn't match. If no message is passed, it just checks that an error occurred.
Example:
tst.Err("permission denied", err, t)
func Go ¶
Go is a shorthand for t.Parallel() and returns the test context.
Example:
ctx := tst.Go(t)
func Is ¶
Is checks that want matches got via cmp.Diff using the options provided. It calls t.Errorf if there's a mismatch. Errors are compared with cmpopts.EquateErrors by default.
Options can be found in both cmp and cmpopts packages.
Example:
tst.Is(want, got, t)
func IsSubString ¶
IsSubString is a specialized version of Is to check that want is a substring of got.
func Ko ¶
func Ko(t Test)
Ko stops the test immediately (t.Fatalf) if the test has already failed. This is useful to prevent cascading errors if a previous check failed.
Example:
tst.Is(want, got, t) tst.Ko(t) // Stop here if Is failed.
func LoadGolden ¶
LoadGolden loads a previously recorded golden file.
func No ¶
No stops the test immediately (t.Fatalf) if the provided error is not nil.
Example:
tst.No(err, t)
func RecordGolden ¶
RecordGolden records a golden file and makes the test fail after printing its value.
Types ¶
type PTest ¶
PTest is an abstraction over *testing.T that includes Parallel and Context.