assert

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MIT Imports: 5 Imported by: 0

README

Assert - A minimal Go test package

Inspired by:

Usage

package foo

import (
	"testing"

	"codeberg.org/dropwhile/assert"
)

// errType is a custom error type.
type errType string

func (e errType) Error() string {
	return string(e)
}

func TestSomething(t *testing.T) {
    t.Parallel()

    err := errors.New("my error")
    assert.Nil(t, err) // test assertion that something is nil
    // output => &errors.errorString{s:"my error"}; want: <nil>;

    err = errType("oops")
    assert.NotNil(t, nil) // test assertion that something is NOT nil
    // output => got: <nil>; expected non-nil

    // assert that an error value matches (string match)
    assert.ErrorIs(t, err, "my bad error")
    // output => got: "oops"; want: "my bad error";

    assert.ErrorIs(t, nil, err)  // assert error value matches (error match)
    // output => got: <nil>; want: errType(oops);

    wrappedErr := fmt.Errorf("wrapped: %w", err)
    assert.ErrorIs(t, nil, wrappedErr) // works with wrapped errors, using errors.Is under the hood
    // output => got: <nil>; want: *fmt.wrapError(wrapped: oops)

    // can also check for error type, using errors.As under the hood
    assert.ErrorIs(t, nil, reflect.TypeFor[*fs.PathError]())
    // output => got: <nil>; want: *fs.PathError

    // assert boolean true
    assert.True(t, false)
    // output => got: false; want: true;

    // assert boolean false
    assert.False(t, true)
    // output => got: true; want: false;

    // assert equal
    assert.Equal(t, 1, 2)
    // output => got: 1; want: 2;

    // assert NOT equal
    assert.NotEqual(t, 1, 1)
    // output => got: 1; expected values to be different;

    // assert string matches regex
    assert.MatchesRegexp(t, "abc123d", `abc[123]+$`)
    // output => got: "abc123d"; want to match "abc[123]+$";

    // a third argument can be passed to each of these functions to emit additional
    // information on failure
    assert.Equal(t, 1, 2,
        fmt.Sprintf("%d times around the moon", 3),
    )
    // output => got: 1; want: 2; 3 times around the moon

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[T any](t TestingT, got, want T, msg ...string)

func Error

func Error(t TestingT, got error, want any, msg ...string)

func False

func False(t TestingT, got bool, msg ...string)

func MatchesRegex

func MatchesRegex(t TestingT, got, pattern string, msg ...string)

func Nil

func Nil(t TestingT, got any, msg ...string)

func NotEqual

func NotEqual[T any](t TestingT, got, want T, msg ...string)

func NotNil

func NotNil(t TestingT, got any, msg ...string)

func True

func True(t TestingT, got bool, msg ...string)

Types

type TestingT

type TestingT interface {
	Error(args ...any)
	Errorf(format string, args ...any)
	Fatal(args ...any)
	Fatalf(format string, args ...any)
}

TestingT is the subset of testing.T (see also testing.TB) used by the assert package.

Jump to

Keyboard shortcuts

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