assert

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2025 License: MIT Imports: 1 Imported by: 2

README

Assert

Build Status Software License

Simple and lightweight testing assertion library for Go.

Installation

go get github.com/gravitton/assert

Usage

package main

import (
	"github.com/gravitton/assert"
	"testing"
)

func TestSomething(t *testing.T) {
	// assert equality
	assert.Equal(t, 123, 123)
	// assert inequality
	assert.NotEqual(t, 123, 456)
}

Credits

License

The MIT License (MIT). Please see License File for more information.

Documentation

Overview

Package assert provides a simple and lightweight testing assertion library for Go.

This package offers a collection of assertion functions that can be used with any testing framework that implements the Testing interface (such as Go's standard testing.T). The assertions are designed to be intuitive and provide clear error messages when tests fail.

Basic Usage

The assert package works with any type that implements the Testing interface:

func TestExample(t *testing.T) {
	// Boolean assertions
	assert.True(t, condition)
	assert.False(t, !condition)

	// Equality assertions
	assert.Equal(t, actual, expected)
	assert.NotEqual(t, actual, unexpected)

	// Identity assertions (pointer comparison)
	assert.Same(t, &obj, &obj)
	assert.NotSame(t, &obj1, &obj2)
}

Assertion Functions

• True/False: Assert boolean values • Equal/NotEqual: Assert value equality using reflect.DeepEqual • Same/NotSame: Assert pointer identity (same memory address) • Fail/Failf: Manually fail tests with custom messages

Error Messages

All assertion functions provide detailed error messages when assertions fail, including actual and expected values formatted with Go's %#v verb for maximum clarity.

Helper Support

All assertion functions automatically call t.helper() if the testing type implements the helper interface, ensuring that test failures point to the correct line in your test code rather than inside the assertion library.

Return Values

All assertion functions return a boolean indicating success (true) or failure (false), allowing for conditional test logic if needed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(t Testing, actual, expected any) bool

Equal asserts that two objects are equal.

assert.Equal(t, actual, expected)

Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses).

func Fail

func Fail(t Testing, message string) bool

Fail reports a failure through

func Failf

func Failf(t Testing, format string, args ...any) bool

Failf reports a failure through

func False

func False(t Testing, actual bool) bool

False asserts that the specified value is false.

assert.False(t, condition)

func NotEqual

func NotEqual(t Testing, actual, expected any) bool

NotEqual asserts that the specified values are NOT equal.

assert.NotEqual(t, actual, expected)

Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses).

func NotSame

func NotSame(t Testing, actual, expected any) bool

NotSame asserts that two pointers do NOT reference the same object.

assert.NotSame(t, actual, expected)

Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.

func Same

func Same(t Testing, actual, expected any) bool

Same asserts that two pointers reference the same object.

assert.Same(t, actual, expected)

Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.

func True

func True(t Testing, actual bool) bool

True asserts that the specified value is true.

assert.True(t, condition)

Types

type Testing

type Testing interface {
	Errorf(format string, args ...any)
}

Testing is an interface wrapper around *testing.T

Jump to

Keyboard shortcuts

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