Testing in Go

  • Go has a built-in testing framework.
  • Tests should be in the same package as the code they are testing.
  • Test files must have the suffix _test.go.
  • The Go CLI go test command is used to run tests.









Example: Code under test

func PrintMyName(name string) string {
    return fmt.Sprintf("Hello, my name is %s.", name)
}

Example: Test case

package main

import "testing"

func TestPrintMyName(t *testing.T) {
    testCases := []struct {
        name        string
        value       string
        expectValue string
        expectFail  bool
    }{
        {"Valid response", "John Doe", "Hello, my name is John Doe.", false},
        {"Invalid response", "John Doe", "Hello, my name is .", true},
        {"Valid lower case name", "john doe", "Hello, my name is john doe.", false},
    }

    for _, tc := range testCases {
        t.Run(tc.name, func(t *testing.T) {
            out := PrintMyName(tc.value)
            if out != tc.expectValue && !tc.expectFail {
                t.Errorf("expected value: %s, got: %s, for name: '%s'", tc.expectValue, out, tc.value)
            }
        })
    }
}









Running tests

go test .

Note: There are other ways to run tests, such as go test -v . to get more verbose output.

Note: You can also get coverage reports with go test -cover ..