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 ¶
- func Equal(t Testing, actual, expected any) bool
- func Fail(t Testing, message string) bool
- func Failf(t Testing, format string, args ...any) bool
- func False(t Testing, actual bool) bool
- func NotEqual(t Testing, actual, expected any) bool
- func NotSame(t Testing, actual, expected any) bool
- func Same(t Testing, actual, expected any) bool
- func True(t Testing, actual bool) bool
- type Testing
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
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 NotEqual ¶
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 ¶
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.