Ending a file's name with _test.go tells the go test command that this file contains test functions.

package greetings

import (
    "testing"
    "regexp"
)

// TestHelloName calls greetings.Hello with a name, checking
// for a valid return value.
func TestHelloName(t *testing.T) {
    name := "Gladys"
    want := regexp.MustCompile(`\b`+name+`\b`)
    msg, err := Hello("Gladys")
    if !want.MatchString(msg) || err != nil {
        t.Fatalf(`Hello("Gladys") = %q, %v, want match for %#q, nil`, msg, err, want)
    }
}

// TestHelloEmpty calls greetings.Hello with an empty string,
// checking for an error.
func TestHelloEmpty(t *testing.T) {
    msg, err := Hello("")
    if msg != "" || err == nil {
        t.Fatalf(`Hello("") = %q, %v, want "", error`, msg, err)
    }
}
This time, run go test without the -v flag. The output will include results for only the tests that failed, which can be useful when you have a lot of tests. The TestHelloName test should fail -- TestHelloEmpty still passes.

$ go test
--- FAIL: TestHelloName (0.00s)
    greetings_test.go:15: Hello("Gladys") = "Hail, %v! Well met!", <nil>, want match for `\bGladys\b`, nil
FAIL
exit status 1
FAIL    example.com/greetings   0.182s
The tests should pass.

$ go test
PASS
ok      example.com/greetings   0.364s

$ go test -v
=== RUN   TestHelloName
--- PASS: TestHelloName (0.00s)
=== RUN   TestHelloEmpty
--- PASS: TestHelloEmpty (0.00s)
PASS
ok      example.com/greetings   0.372s
In greetings/greetings.go, paste the following code in place of the Hello function. Note that the highlighted lines change the value that the function returns, as if the name argument had been accidentally removed.

// Hello returns a greeting for the named person.
func Hello(name string) (string, error) {
    // If no name was given, return an error with a message.
    if name == "" {
        return name, errors.New("empty name")
    }
    // Create a message using a random format.
    // message := fmt.Sprintf(randomFormat(), name)
    message := fmt.Sprint(randomFormat())
    return message, nil
}
fund_test.go

package funding

import "testing"

func BenchmarkFund(b *testing.B) {
    // Add as many dollars as we have iterations this run
    fund := NewFund(b.N)

    // Burn through them one at a time until they are all gone
    for i := 0; i < b.N; i++ {
        fund.Withdraw(1)
    }

    if fund.Balance() != 0 {
        b.Error("Balance wasn't zero:", fund.Balance())
    }
}
In your text editor, replace the unit test in reverse_test.go with the following fuzz test.

func FuzzReverse(f *testing.F) {
    testcases := []string{"Hello, world", " ", "!12345"}
    for _, tc := range testcases {
        f.Add(tc)  // Use f.Add to provide a seed corpus
    }
    f.Fuzz(func(t *testing.T, orig string) {
        rev := Reverse(orig)
        doubleRev := Reverse(rev)
        if orig != doubleRev {
            t.Errorf("Before: %q, after: %q", orig, doubleRev)
        }
        if utf8.ValidString(orig) && !utf8.ValidString(rev) {
            t.Errorf("Reverse produced invalid UTF-8 string %q", rev)
        }
    })
}
Run FuzzReverse with fuzzing, to see if any randomly generated string inputs will cause a failure. This is executed using go test with a new flag, -fuzz.

$ go test -fuzz=Fuzz
fuzz: elapsed: 0s, gathering baseline coverage: 0/3 completed
fuzz: elapsed: 0s, gathering baseline coverage: 3/3 completed, now fuzzing with 8 workers
fuzz: minimizing 38-byte failing input file...
--- FAIL: FuzzReverse (0.01s)
    --- FAIL: FuzzReverse (0.00s)
        reverse_test.go:20: Reverse produced invalid UTF-8 string "\x9c\xdd"

    Failing input written to testdata/fuzz/FuzzReverse/af69258a12129d6cbba438df5d5f25ba0ec050461c116f777e77ea7c9a0d217a
    To re-run:
    go test -run=FuzzReverse/af69258a12129d6cbba438df5d5f25ba0ec050461c116f777e77ea7c9a0d217a
FAIL
exit status 1
FAIL    example/fuzz  0.030s
A failure occurred while fuzzing, and the input that caused the problem is written to a seed corpus file that will be run the next time go test is called, even without the -fuzz flag. To view the input that caused the failure, open the corpus file written to the testdata/fuzz/FuzzReverse directory in a text editor. Your seed corpus file may contain a different string, but the format will be the same.

go test fuzz v1
string("泃")
Add a test to the stringutil package by creating the file $GOPATH/src/github.com/user/stringutil/reverse_test.go containing the following Go code.

package stringutil

import "testing"

func TestReverse(t *testing.T) {
	cases := []struct {
		in, want string
	}{
		{"Hello, world", "dlrow ,olleH"},
		{"Hello, 世界", "界世 ,olleH"},
		{"", ""},
	}
	for _, c := range cases {
		got := Reverse(c.in)
		if got != c.want {
			t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
		}
	}
}
Run go test again without the -fuzz flag; the new failing seed corpus entry will be used:

$ go test
--- FAIL: FuzzReverse (0.00s)
    --- FAIL: FuzzReverse/af69258a12129d6cbba438df5d5f25ba0ec050461c116f777e77ea7c9a0d217a (0.00s)
        reverse_test.go:20: Reverse produced invalid string
FAIL
exit status 1
FAIL    example/fuzz  0.016s

Recommend

Go Return and handle an error

Go Return a random greeting

Go Tutorial: Getting started with multi-module workspaces Download and modify the golang.org/x/example module Future step

Go Tutorial: Getting started with multi-module workspaces Download and modify the golang.org/x/example module Run the code in the workspace

Go Tutorial: Getting started with multi-module workspaces Download and modify the golang.org/x/example module

Go Tutorial: Getting started with multi-module workspaces Create the workspace Initialize the workspace

Go Tutorial: Getting started with multi-module workspaces Create a module for your code

Tutorial: Developing a RESTful API with Go and Gin Completed code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to return a specific item Run the code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to return a specific item Write the code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to add a new item Run the code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to add a new item Write the code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to return all items Run the code

Tutorial: Developing a RESTful API with Go and Gin Write a handler to return all items Write the code

Tutorial: Developing a RESTful API with Go and Gin Create the data Write the code

Tutorial: Developing a RESTful API with Go and Gin Create a folder for your code

Go Tutorial: Accessing a relational database Completed code

Go Tutorial: Accessing a relational database Add data Write the code

Go Tutorial: Accessing a relational database Query for a single row Write the code

Go Tutorial: Accessing a relational database Query for multiple rows Run the code

Go Tutorial: Accessing a relational database Query for multiple rows Write the code

Go Tutorial: Accessing a relational database Get a database handle and connect Run the code

Go Tutorial: Accessing a relational database Get a database handle and connect Write the code

Go Tutorial: Accessing a relational database Find and import a database driver

Go Tutorial: Accessing a relational database Set up a database

Go Tutorial: Accessing a relational database Create a folder for your code

Go Tutorial: Getting started with generics Completed code

Go Tutorial: Getting started with generics Declare a type constraint Write the code

Go Tutorial: Getting started with generics Add a generic function to handle multiple types Write the code

Go Tutorial: Getting started with generics Add non-generic functions Run the code

Go Tutorial: Getting started with generics Add non-generic functions Write the code

Go Tutorial: Getting started with generics Create a folder for your code

Go Setting up and using gccgo C Interoperability Function names

Go Setting up and using gccgo C Interoperability Types

Go Setting up and using gccgo Imports

Go Setting up and using gccgo Using gccgo

Go Setting up and using gccgo Building Build commands

Setting up and using gccgo Building Gold

Installing Go from source Optional environment variables

Installing Go from source Keeping up with releases