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 ¶
- func Must(t TB, err error)
- func MustError(t TB, expect string, err error)
- func MustErrorPrefix(t TB, prefix, expect string, err error)
- func MustPrefix(t TB, prefix string, err error)
- func MustTempDir(t TB, dir, prefix string) (name string)
- func MustTempFile(t TB, dir, prefix string) (f *os.File)
- func MustWriteTempFile(t TB, dir, prefix string, body []byte) string
- func SrcDir() (dir string, ok bool)
- type TB
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustErrorPrefix ¶
MustErrorPrefix is like MustError, except that if it logs to t.Fatalf, the given prefix is prepended to the output.
func MustPrefix ¶
MustPrefix is like Must, except that if it logs to t.Fatalf, the given prefix is prepended to the output.
func MustTempDir ¶
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 ¶
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 ¶
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 ¶
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.