Assert

The testing/assert package is a flexible and extensible assertion library designed to provide a unified interface for asserting conditions in Go tests. It allows developers to integrate assertion functionality without being tied to a specific assertion library.

Installation

go get oss.nandlabs.io/golly/testing/assert

Features

  • General assertion interface for asserting conditions in tests
  • Supports various assertion functions for different types of conditions
  • Easy-to-use functions for consistent handling of assertions
  • Clear failure messages with optional formatting

Usage

  1. Import the library into your Go test file:

    import "oss.nandlabs.io/golly/testing/assert"
  2. Use the assertion functions in your test cases:

    func TestAdd(t *testing.T) {
        result := add(1, 2)
        assert.Equal(t, result, 3)
    }
  3. Run your tests:

    go test

Available Assertions

FunctionDescription
EqualAsserts two values are equal
NotEqualAsserts two values are not equal
TrueAsserts a value is true
FalseAsserts a value is false
NilAsserts a value is nil
NotNilAsserts a value is not nil
ContainsAsserts a string, array, slice, or map contains an element
NoErrorAsserts an error is nil
ErrorAsserts an error is not nil

Examples

Struct Testing

type Person struct {
    Name string
    Age  int
}

func TestPersonCreation(t *testing.T) {
    person := Person{Name: "Alice", Age: 30}
    assert.Equal(t, person.Name, "Alice", "Name should be Alice")
    assert.Equal(t, person.Age, 30, "Age should be 30")

    var nilPerson *Person
    assert.Nil(t, nilPerson, "Uninitialized pointer should be nil")

    actualPerson := &Person{Name: "Bob", Age: 25}
    assert.NotNil(t, actualPerson, "Initialized pointer should not be nil")
}

Error Handling

func TestErrorHandling(t *testing.T) {
    _, err := riskyOperation()
    assert.NoError(t, err, "operation should succeed")

    _, err = failingOperation()
    assert.Error(t, err, "operation should fail")
    assert.Equal(t, err.Error(), "expected failure")
}