How to Write Unit Tests in Go with testing Package

Write test functions in *_test.go files using the testing package and run them with go test.

Write a function with the testing.T parameter in a file named *_test.go and run it with go test.

package mypackage

import "testing"

func TestAdd(t *testing.T) {
    got := Add(2, 3)
    want := 5
    if got != want {
        t.Errorf("Add(2, 3) = %d; want %d", got, want)
    }
}

Run the test:

go test