package tdtest

import "cuelang.org/go/internal/tdtest"

Package tdtest provides support for table-driven testing.

Features include automatically updating of test values, automatic error message generation, and singling out single tests to run.

Auto updating fields is only supported for fields that are scalar types: string, bool, int*, and uint*. If the field is a string, the "actual" value may be any Go value that can meaningfully be printed with fmt.Sprint.

Index

Variables

var UpdateTests = false

UpdateTests defines whether tests should be updated by default. This can be overridden on an individual basis using T.Update.

Functions

func Run

func Run[TC any](t *testing.T, table []TC, fn func(t *T, tc *TC))

Run runs the given function for each (selected) element in the table. TC must be a struct type. If that has a string field named "name", that value will be used to name the associated subtest.

Types

type T

type T struct {
	*testing.T
	// contains filtered or unexported fields
}

T is a single test case representing an element in a table. It embeds *testing.T, so all functions of testing.T are available.

func (*T) Equal

func (t *T) Equal(actual, field any, msgAndArgs ...any)

Equal compares two fields.

For auto updating to work, field must reference a field in the test case directly.

func (*T) Select

func (t *T) Select(tests ...any)

Select species which tests to run. The test may be an int, in which case it selects the table entry to run, or a string, which is matched against the last path of the test. An empty list runs all tests.

func (*T) Update

func (t *T) Update(enable bool)

Update specifies whether to update the Go structs in case of discrepancies. It overrides the default setting.

Source Files

tdtest.go update.go

Version
v0.11.2
Published
Jan 22, 2025
Platform
linux/amd64
Imports
15 packages
Last checked
2 hours ago

Tools for package owners.