Test

Introduction

This page shows how to run go test based on simple example.

Main file

// main.go
package main

import (
	"fmt"
	"io"
)

func copySourceToDest(source io.Reader, dest io.Writer) error {
	b, err := io.ReadAll(source)
	if err != nil {
		return err
	}
	_, err = dest.Write(b)
	return err
}
 
func main() {
	fmt.Println("Test")
}

Test file

// main_test.go
package main

import (
	"bytes"
	"strings"
	"testing"
)

func TestCopySourceToDest(t *testing.T) {
	const input = "foo"
	source := strings.NewReader(input)
	dest := bytes.NewBuffer(make([]byte, 0))

	err := copySourceToDest(source, dest)
	if err != nil {
		t.FailNow()
	}

	got := dest.String()
	if got != input {
		t.Errorf("expected: %s, got: %s", input, got)
	}
}

How to run tests

1. Run test

$ go test
# Output:
PASS
ok      ch02    0.269s

2. Run test, more verbose

$ go test -v
# Output:
=== RUN   TestCopySourceToDest
--- PASS: TestCopySourceToDest (0.00s)
PASS
ok      ch02    0.257s

3. Run test by suppling the test files as argiments

$ go test main.go main_test.go
# Output
ok      command-line-arguments  0.257s

4. Run test by specifying test

$ go test -run TestCopySourceToDest
# Output
PASS
ok      ch02    0.247s

5. Run a test coverage

go test -cover
# Output
PASS
coverage: 66.7% of statements
ok      ch02    0.253s

6. Run test by providing a package name

$ go test person
# Output
ok  	person	0.004s

7. Run a test on all packages in a directory

$ go test ./...

Last updated

Was this helpful?