testx

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 12 Imported by: 6 modules, 5 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

Overview

Package testx provides a comprehensive set of generic matchers and assertion utilities for Go testing.

It is designed to be highly extensible, type-safe, and easy to use alongside the standard `testing` package.

Core Assertions

The package provides two primary assertion functions:

  • `Expect`: Asserts that an actual value satisfies one or more matchers.
  • `ExpectPanic`: Asserts that a function panics, and optionally verifies the panic value against matchers.

Example:

func TestMath(t *testing.T) {
	result := 1 + 1
	testx.Expect(t, result, testx.Equal(2), testx.BeGt(0))

	testx.ExpectPanic(t, func() {
		panic("something went wrong")
	}, testx.Equal("something went wrong"))
}

Matchers

Matchers are the building blocks of assertions. A `Matcher[T]` evaluates an actual value of type `T` and returns whether it meets the expected criteria. The package includes a wide variety of built-in matchers:

  • Equality & Comparison: `Equal`, `Be`, `BeGt`, `BeLt`, `IsZero`, etc.
  • Collections: `HaveLen`, `HaveCap`, `HaveKey`, `Contains`, `EquivalentSlice`, etc.
  • Strings: `HavePrefix`, `HaveSuffix`, `ContainsSubString`, `MatchRegexp`.
  • Types & Interfaces: `BeAssignableTo`, `BeConvertibleTo`, `IsType`.
  • Errors: `IsError`, `AsError`, `ErrorEqual`, `Succeed`, `Failed`, etc.

Custom Matchers

You can easily create custom matchers using `NewMatcher` or `NewComparedMatcher`:

func BeEven() testx.Matcher[int] {
	return testx.NewMatcher("BeEven", func(actual int) bool {
		return actual%2 == 0
	})
}

BDD Integration

For a more expressive, Behavior-Driven Development style, consider using the `github.com/xoctopus/x/testx/bdd` sub-package, which builds upon the matchers defined here.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expect added in v0.2.0

func Expect[A any](t testing.TB, actual A, matchers ...Matcher[A])

Expect asserts that the actual value satisfies all the provided matchers. It fails the test if any matcher returns false.

func ExpectPanic added in v0.2.0

func ExpectPanic[A any](t testing.TB, f func(), matchers ...Matcher[A])

ExpectPanic asserts that the provided function f panics with a value of type A, and optionally asserts that the recovered panic value satisfies the provided matchers.

Example
package main

import (
	"fmt"

	. "github.com/xoctopus/x/testx"
	"github.com/xoctopus/x/testx/testutil"
)

type MockCodeErr int

func (MockCodeErr) Message() string { return "mock" }

func crash(i int) {
	switch i {
	case 0:
		return
	case 1:
		panic(fmt.Errorf("any"))
	}
}

func main() {
	t := &testutil.MockTB{}

	t.Reset()
	ExpectPanic[error](t, func() {
		panic("any")
	})
	fmt.Println(t.Output2())

	t.Reset()
	ExpectPanic[string](t, func() {
		crash(1)
	})
	fmt.Println(t.Output2())

	t.Reset()
	ExpectPanic[string](t, func() {})
	fmt.Println(t.Output2())

}
Output:
expect a panic of `error`, but got string

expect a panic of `string`, but got *errors.errorString

expect a panic of `string`, but f returned normally

func NewComparedMatcher added in v0.2.0

func NewComparedMatcher[Actual any, Expect any](name string, matcher ComparableMatchFunc[Actual, Expect]) internal.MatcherNewer[Actual, Expect]

NewComparedMatcher creates a matcher constructor that binds an expected value to a ComparableMatchFunc.

Types

type ComparableMatchFunc added in v0.5.0

type ComparableMatchFunc[Actual any, Expect any] func(Actual, Expect) bool

ComparableMatchFunc defines a function that compares an actual value against an expected value.

type MatchFunc added in v0.5.0

type MatchFunc[Actual any] func(Actual) bool

MatchFunc defines a function that evaluates an actual value and returns a boolean indicating success.

type Matcher added in v0.2.0

type Matcher[Actual any] = internal.Matcher[Actual]

Matcher is an alias for internal.Matcher.

func AsError added in v0.5.0

func AsError[T any](expect *T) Matcher[error]

AsError creates a matcher that asserts the actual error can be assigned to the expected error target (using errors.As).

func AsErrorType added in v0.5.0

func AsErrorType[T error]() Matcher[error]

AsErrorType creates a matcher that asserts the actual error can be assigned to a specific error type T.

func Be added in v0.2.0

func Be[T any](expect T) Matcher[T]

Be creates a matcher that asserts the actual value is strictly identical to the expected value.

func BeAssignableTo added in v0.2.0

func BeAssignableTo[T any]() Matcher[any]

BeAssignableTo creates a matcher that asserts the actual value is assignable to the type T.

func BeConvertibleTo added in v0.2.0

func BeConvertibleTo[T any]() Matcher[any]

BeConvertibleTo creates a matcher that asserts the actual value is convertible to the type T.

func BeFalse added in v0.2.0

func BeFalse() Matcher[bool]

BeFalse creates a matcher that asserts the actual boolean value is false.

func BeGt added in v0.5.0

func BeGt[T cmp.Ordered](expect T) Matcher[T]

BeGt creates a matcher that asserts the actual value is greater than the expected value.

func BeGte added in v0.5.0

func BeGte[T cmp.Ordered](expect T) Matcher[T]

BeGte creates a matcher that asserts the actual value is greater than or equal to the expected value.

func BeLt added in v0.5.0

func BeLt[T cmp.Ordered](expect T) Matcher[T]

BeLt creates a matcher that asserts the actual value is less than the expected value.

func BeLte added in v0.5.0

func BeLte[T cmp.Ordered](expect T) Matcher[T]

BeLte creates a matcher that asserts the actual value is less than or equal to the expected value.

func BeNil added in v0.2.0

func BeNil[Actual any]() Matcher[Actual]

BeNil creates a matcher that asserts the actual value is nil.

func BeTrue added in v0.2.0

func BeTrue() Matcher[bool]

BeTrue creates a matcher that asserts the actual boolean value is true.

func ConsistOfSlice added in v0.5.0

func ConsistOfSlice[E comparable, S ~[]E](expect S) Matcher[S]

ConsistOfSlice creates a matcher that asserts the actual slice has exactly the same elements in the same order as the expected slice.

func Contains added in v0.2.0

func Contains[E comparable, S ~[]E](v E) Matcher[S]

Contains creates a matcher that asserts the actual slice contains the expected element.

func ContainsSubString added in v0.2.0

func ContainsSubString(sub string) Matcher[string]

ContainsSubString creates a matcher that asserts the actual string contains the expected substring.

func Equal added in v0.2.0

func Equal[T any](expect T) Matcher[T]

Equal creates a matcher that asserts the actual value is deeply equal to the expected value.

func EquivalentSlice added in v0.2.1

func EquivalentSlice[E comparable, S ~[]E](expect S) Matcher[S]

EquivalentSlice creates a matcher that asserts the actual slice has the same elements as the expected slice, regardless of order.

func ErrorContains added in v0.2.0

func ErrorContains(sub string) Matcher[error]

ErrorContains creates a matcher that asserts the actual error's message contains the expected substring.

func ErrorEqual added in v0.2.0

func ErrorEqual(expect string) Matcher[error]

ErrorEqual creates a matcher that asserts the actual error's message exactly matches the expected string.

func Failed added in v0.2.0

func Failed() Matcher[error]

Failed creates a matcher that asserts the actual error is not nil.

func HaveCap added in v0.2.0

func HaveCap[T any](cap int) Matcher[T]

HaveCap creates a matcher that asserts the actual collection has the expected capacity.

func HaveKey added in v0.5.0

func HaveKey[K comparable, V any, M ~map[K]V](key K) Matcher[M]

HaveKey creates a matcher that asserts the actual map contains the expected key.

func HaveLen added in v0.2.0

func HaveLen[T any](len int) Matcher[T]

HaveLen creates a matcher that asserts the actual collection has the expected length.

func HavePrefix added in v0.2.0

func HavePrefix(prefix string) Matcher[string]

HavePrefix creates a matcher that asserts the actual string starts with the expected prefix.

func HaveSuffix added in v0.2.0

func HaveSuffix(suffix string) Matcher[string]

HaveSuffix creates a matcher that asserts the actual string ends with the expected suffix.

func IsCodeError added in v0.2.5

func IsCodeError[Code codex.Code](expect Code) Matcher[error]

IsCodeError creates a matcher that asserts the actual error implements codex.Error and matches the expected code.

func IsError added in v0.2.0

func IsError(expect error) Matcher[error]

IsError creates a matcher that asserts the actual error is the expected error (using errors.Is).

func IsNotZero added in v0.2.12

func IsNotZero[Actual any]() Matcher[Actual]

IsNotZero creates a matcher that asserts the actual value is not the zero value for its type.

func IsType added in v0.5.0

func IsType[T any]() Matcher[any]

IsType creates a matcher that asserts the actual value is exactly of the type T.

func IsZero added in v0.2.12

func IsZero[Actual any]() Matcher[Actual]

IsZero creates a matcher that asserts the actual value is the zero value for its type.

func MatchRegexp added in v0.5.0

func MatchRegexp(pattern string) Matcher[string]

MatchRegexp creates a matcher that asserts the actual string matches the expected regular expression pattern.

func NewMatcher added in v0.2.0

func NewMatcher[Actual any](name string, matcher MatchFunc[Actual]) Matcher[Actual]

NewMatcher creates a new Matcher with the given name and matching function.

func Not added in v0.2.0

func Not[Actual any](matcher Matcher[Actual]) Matcher[Actual]

Not negates the result of the provided matcher.

func NotBe added in v0.2.0

func NotBe[T any](expect T) Matcher[T]

NotBe creates a matcher that asserts the actual value is not strictly identical to the expected value.

func NotBeNil added in v0.2.0

func NotBeNil[Actual any]() Matcher[Actual]

NotBeNil creates a matcher that asserts the actual value is not nil.

func NotEqual added in v0.2.0

func NotEqual[T any](expect T) Matcher[T]

NotEqual creates a matcher that asserts the actual value is not deeply equal to the expected value.

func Succeed added in v0.2.0

func Succeed() Matcher[error]

Succeed creates a matcher that asserts the actual error is nil.

type NormalizedExpectedMatcher added in v0.2.0

type NormalizedExpectedMatcher = internal.NormalizedExpectedMatcher

NormalizedExpectedMatcher is an alias for internal.NormalizedExpectedMatcher.

Directories

Path Synopsis
Package bdd provides a Behavior-Driven Development (BDD) style testing framework.
Package bdd provides a Behavior-Driven Development (BDD) style testing framework.

Jump to

Keyboard shortcuts

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