[Go] Check an expected value with Testify's assert.Equal

Created on November 30, 2026

You can use Testify's Equal assertion to compare an expected result with the value your function returns.

Save the function under test as add.go.

package calculator

func AddNumbers(a, b int) int {
    return a + b
}

Save this test as add_test.go in the same package.

package calculator

import (
    "testing"

    "github.com/stretchr/testify/assert"
)

func TestAddNumbers(t *testing.T) {
    check := assert.New(t)
    got := AddNumbers(2, 3)
    check.Equal(5, got)
}

assert.New(t) attaches the assertion to the current test. check.Equal(5, got) takes the expected value first and the actual result second. In a Go module with the Testify dependency installed, go test passes for this example.

Reference

Go