test

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2025 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package test contains helper functions for standard Go testing.

The ExpectEquality() is the most basic and probably the most useful function. It compares like-typed variables for equality and returns true if they match. ExpectInequality() is the inverse function.

The ExpectFailure() and ExpectSuccess() functions test for failure and success. These two functions work with bool or error and special handling for nil.

All functions a return a boolean to indicate whether the test has passed. This allows the user to control larger test procedures that have several stages.

The tags argument for every public function allows the test to be tagged with additional information. Each tag will appear at the beginning of any error message separated by a colon. For example,

v := false
testFile = "my test data"
lineNo = 100
test.ExpectSuccess(t, v, testFile, lineNo)

will fail with the following:

my test data: 100: a success value is expected for type bool

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DemandEquality added in v0.33.0

func DemandEquality[T comparable](t *testing.T, v T, expectedValue T, tags ...any)

DemandEquality is used to test equality between one value and another. If the test fails it is a testing fatility

This is particular useful if the values being tested are used in further tests and so must be correct. For example, testing that the lengths of two slices are equal before iterating over them in unison

func DemandFailure added in v0.35.0

func DemandFailure(t *testing.T, v any, tags ...any)

DemandFailure is used to test for a value which indicates an 'unsuccessful' value for the type. See ExpectFailure() for more information on failure values

func DemandImplements added in v0.35.0

func DemandImplements[T comparable](t *testing.T, instance any, implements T, tags ...any) bool

DemandImplements tests whether an instance is an implementation of type T

func DemandSuccess added in v0.35.0

func DemandSuccess(t *testing.T, v any, tags ...any)

DemandSuccess is used to test for a value which indicates an 'successful' value for the type. See ExpectSucess() for more information on success values

func ExpectApproximate added in v0.35.2

func ExpectApproximate[T Approximate](t *testing.T, value T, expectedValue T, tolerance float64, tags ...any) bool

ExpectApproximate is used to test approximate equality between one value and another.

Tolerance represents a percentage. For example, 0.5 is tolerance of +/- 50%. If the tolerance value is negative then the positive equivalent is used.

func ExpectEquality added in v0.25.0

func ExpectEquality[T comparable](t *testing.T, value T, expectedValue T, tags ...any) bool

ExpectEquality is used to test equality between one value and another

func ExpectFailure added in v0.25.0

func ExpectFailure(t *testing.T, v any, tags ...any) bool

ExpectFailure tests for an 'unsucessful value for the value's type.

Types bool and error are treated thus:

bool == false
error != nil

If type is nil then the test will fail

Any other type will fatally fail - only bool, error and nil are supported

func ExpectInequality added in v0.25.0

func ExpectInequality[T comparable](t *testing.T, value T, expectedValue T, tags ...any) bool

ExpectInequality is used to test inequality between one value and another. In other words, the test does not want to succeed if the values are equal

func ExpectSuccess added in v0.25.0

func ExpectSuccess(t *testing.T, v any, tags ...any) bool

ExpectSuccess tests for a 'sucessful' value for the value's type.

Types bool and error are treated thus:

bool == true
error == nil

If type is nil then the test will succeed

Any other type will fatally fail - only bool, error and nil are supported

Types

type Approximate added in v0.40.0

type Approximate interface {
	~float32 | ~float64 | ~int
}

Approximate constraint used by ExpectApproximate() function

Jump to

Keyboard shortcuts

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