gounit is test assertions library for Go. It was developed to address the shortcoming of many assertion frameworks that employ assertion of types at runtime rather than compile time.
go get github.com/gogunit/gunitUse gunit when you want direct typed assertions on testing.T.
Use hammy when you want matcher-style assertions, especially when composing checks with AllOf, AnyOf, HavingField, ContainsInOrder, and related helpers.
Within hammy, prefer the typed wrappers for most assertions:
assert.Is(a.Number(actual).Matches(a.AllOf(
a.GreaterThan(0),
a.LessThan(10),
)))Use a.Match(...) when no dedicated wrapper fits or when the value is intentionally held as any.
// direct assertion style
func Test_nine_plus_two_is_greater_than_ten(t *testing.T) {
actual := 9 + 2
expected := 10
gunit.Number(t, actual).GreaterThan(expected)
}
// wrap testing.T struct
func Test_nine_plus_two_is_greater_than_ten(t *testing.T) {
assert := gunit.New(t)
actual := 9 + 2
expected := 10
assert.Int(actual).GreaterThan(expected)
}package adder
import (
"testing"
a "github.com/gogunit/gunit/hammy"
)
func Test_add_returns_expected_sum(t *testing.T) {
assert := a.New(t)
actual := Add(2, 3)
assert.Is(a.Number(actual).EqualTo(5))
}func Test_add_returns_small_positive_sum(t *testing.T) {
assert := a.New(t)
actual := Add(2, 3)
assert.Is(a.Number(actual).Matches(a.AllOf(
a.GreaterThan(0),
a.LessThan(10),
)))
}package service
import (
"errors"
"fmt"
"testing"
a "github.com/gogunit/gunit/hammy"
)
var errTimeout = errors.New("timeout")
func Test_run_wraps_timeout_error(t *testing.T) {
assert := a.New(t)
err := fmt.Errorf("request failed: %w", errTimeout)
assert.Is(a.ErrorIs(err, errTimeout))
}func Test_people_are_sorted_by_name(t *testing.T) {
assert := a.New(t)
people := []Person{{Name: "Ada"}, {Name: "Linus"}}
assert.Is(a.Slice(people).ContainsInOrder(
a.HavingField("Name", func(person Person) string { return person.Name }, a.EqualTo("Ada")),
a.HavingField("Name", func(person Person) string { return person.Name }, a.EqualTo("Linus")),
))
}func Test_dynamic_payload_has_expected_type(t *testing.T) {
assert := a.New(t)
var payload any = Response{Status: "ok"}
assert.Is(a.Match(payload, a.TypeOf[Response]()))
}For more Hammy examples and the matcher reference, see hammy/README.md.