testutil

package module
v0.0.0-...-b5d8aa7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2017 License: BSD-3-Clause Imports: 4 Imported by: 0

README

testutil GoDoc

Package testutil provides utilities to make common testing tasks easier.

Checking errors

Nil errors

A common pattern in testing is to check that an error is nil, and fail if it is non-nil. For example:

func TestFoo(t *testing.T) {
  f, err := getFoo()
  if err != nil {
    t.Fatalf("could not get foo:", err)
  }
}

testutil provides a number of convenience functions to make this common task easier such as Must, which checks that its argument is nil, and otherwise calls t.Fatal:

func TestFoo(t *testing.T) {
  f, err := getFoo()
  testutil.Must(t, err)
}
Non-nil errors

Conversely, it's also common to check that a particular error is returned:

func TestFooError(t *testing.T) {
  _, err := getFoo()
  if err == nil || err.Error() != "error string" {
    t.Errorf("unexpected error: got %v; want %v", err, "error string")
  }
}

testutil makes this easy as well:

func TestFooError(t *testing.T) {
  _, err := getFoo()
  testutil.MustError(t, "error string", err)
}

Creating temporary files

Tests often rely on creating temporary files or directories, which results in a lot of boilerplate:

func TestTempFile(t *testing.T) {
  f, err := ioutil.TempFile("", "")
  if err != nil {
    t.Fatal("could not create temp file:", err)
  }
  err = os.Remove(f.Name())
  if err != nil {
    t.Fatalf("could not remove temp file:", err)
  }
}

testutil handles this simply as well:

func TestTempFile(t *testing.T) {
  f := testutil.MustTempFile(t, "", "")
  testutil.Must(t, os.Remove(f.Name()))
}

Other features

Complete documentation is at godoc.org.

Contributing

Contributions are welcome by pull request (please add your name to the AUTHORS file), or by submitting an issue. Feel free to email hello@joshlf.com if you have questions about contributing.

Documentation

Overview

Package testutil provides utilities to make common testing tasks easier.

For example, consider the task of creating a temporary file, and then removing it. Normally, this might look something like:

func TestTempFile(t *testing.T) {
    f, err := ioutil.TempFile("", "")
    if err != nil {
        t.Fatal("could not create temp file:", err)
    }
    err = os.Remove(f.Name())
    if err != nil {
        t.Fatalf("could not remove temp file:", err)
    }
}

Using testutil, this can be shortened to:

func TestTempFile(t *testing.T) {
    f := testutil.MustTempFile(t, "", "")
    testutil.Must(t, os.Remove(f.Name()))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must(t TB, err error)

Must logs to t.Fatalf if err != nil.

func MustError

func MustError(t TB, expect string, err error)

MustError logs to t.Fatalf if err == nil or if err.Error() != expect.

func MustErrorPrefix

func MustErrorPrefix(t TB, prefix, expect string, err error)

MustErrorPrefix is like MustError, except that if it logs to t.Fatalf, the given prefix is prepended to the output.

func MustPrefix

func MustPrefix(t TB, prefix string, err error)

MustPrefix is like Must, except that if it logs to t.Fatalf, the given prefix is prepended to the output.

func MustTempDir

func MustTempDir(t TB, dir, prefix string) (name string)

MustTempDir attempts to create a temp directory, and logs the error to t.Fatalf if it fails. The arguments dir and prefix behave as documented in ioutil.TempDir.

func MustTempFile

func MustTempFile(t TB, dir, prefix string) (f *os.File)

MustTempFile attempts to create a temp file, and logs the error to t.Fatalf if it fails. The arguments dir and prefix behave as documented in ioutil.TempFile.

func MustWriteTempFile

func MustWriteTempFile(t TB, dir, prefix string, body []byte) string

MustWriteTempFile attempts to create a temp file and initialize it with the given body. Unlike MustTempFile, it only returns the name of the created file. If it fails, it logs the error to t.Fatalf. The arguments dir and prefix behave as documented in ioutil.TempFile.

func SrcDir

func SrcDir() (dir string, ok bool)

SrcDir attempts to figure out what source file it is called from, and returns the parent directory of that file. This can be useful for tests which have local test data, since commands such as

go test ./...

can make it so that the current working directory is not necessarily the same as the source directory.

Types

type TB

type TB interface {
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fail()
	FailNow()
	Failed() bool
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Log(args ...interface{})
	Logf(format string, args ...interface{})
	Skip(args ...interface{})
	SkipNow()
	Skipf(format string, args ...interface{})
	Skipped() bool
}

TB is the interface common to testing.T and testing.B. It is used instead of testing.TB so that it can be satisfied by types other than testing.T and testing.B for internal testing purposes, but can be treated by users of this package as equivalent to testing.TB.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL