assert

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 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.

README

Assert - A minimal Go test package

GoDoc

Inspired by:

Usage

package main

import (
	"testing"

	"codeberg.org/dropwhile/go-droplibs/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.Error(t, err, "my bad error")
    // output => got: "oops"; want: "my bad error";

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

    wrappedErr := fmt.Errorf("wrapped: %w", err)
    assert.Error(t, err, 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.Error(t, err, 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

Overview

Package assert is a minimal Go test package.

Index

Constants

This section is empty.

Variables

View Source
var Assert = True

Assert is an alias for True

Functions

func Equal

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

Equal fails assertion if got and want are not equal.

func Error

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

Error fails assertion if got and want are not "error similar". want can be one of: nil, a string, an error, or an interface.

Some examples...

assert that an error value matches (string match)

assert.Error(t, err, "my bad error")

assert error value matches (error match)

assert.Error(t, err, err2)

assert that an error value matches, which works with wrapped errors, using errors.Is under the hood

wrappedErr := fmt.Errorf("wrapped: %w", err)
assert.Error(t, err, wrappedErr)

can also check for error type, using errors.As under the hood

assert.Error(t, err, reflect.TypeFor[*fs.PathError]())

func False

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

False fails assertion if got is true.

func MatchesRegex

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

MatchesRegex asserts that the string matches the regex pattern.

Example

assert.MatchesRegexp(t, "abc123d", `abc[123]+$`)

func Nil

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

Nil fails assertion if got is not nil.

func NotEqual

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

NotEqual fails assertion if got and want are equal.

func NotNil

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

NotNil fails assertion if got is nil.

func True

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

True fails assertion if got is false.

Types

type TestingT

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

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