testutils

package
v0.0.0-...-03d6fc4 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2019 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Convenience utilities for testing.

Index

Constants

View Source
const (
	SMALL_TEST  = "small"
	MEDIUM_TEST = "medium"
	LARGE_TEST  = "large"
)

Variables

View Source
var (

	// DEFAULT_RUN indicates whether the given test type runs by default
	// when no filter flag is specified.
	DEFAULT_RUN = map[string]bool{
		SMALL_TEST:  true,
		MEDIUM_TEST: true,
		LARGE_TEST:  true,
	}

	TIMEOUT_SMALL  = "4s"
	TIMEOUT_MEDIUM = "15s"
	TIMEOUT_LARGE  = "4m"

	TIMEOUT_RACE = "5m"

	// TEST_TYPES lists all of the types of tests.
	TEST_TYPES = []string{
		SMALL_TEST,
		MEDIUM_TEST,
		LARGE_TEST,
	}

	// TryAgainErr use used by TryUntil.
	TryAgainErr = errors.New("Trying Again")
)

Functions

func AssertCloses

func AssertCloses(t TestingT, c io.Closer)

AssertCloses takes an ioutil.Closer and asserts that it closes. E.g.: frobber := NewFrobber() defer testutils.AssertCloses(t, frobber)

func AssertErrorContains

func AssertErrorContains(t TestingT, err error, substr string)

AssertErrorContains asserts that the given error contains the given string.

func EventuallyConsistent

func EventuallyConsistent(duration time.Duration, f func() error) error

EventuallyConsistent tries a test repeatedly until either the test passes or time expires, and is used when tests are written to expect non-eventual consistency.

Use this function sparingly.

duration - The amount of time to keep trying. f - The func to run the tests, should return TryAgainErr if

we should keep trying, otherwise TryUntil will return
with the err that f() returns.

func GetRepoRoot

func GetRepoRoot(t TestingT) string

Return the path to the root of the checkout.

func LargeTest

func LargeTest(t TestingT)

LargeTest is a function which should be called at the beginning of a large test: a test (> 15 seconds) with significant reliance on external dependencies which makes it too slow or flaky to run as part of the normal test suite.

func LocalOnlyTest

func LocalOnlyTest(t TestingT)

LocalOnlyTest will skip the test if it is bein run on GCE.

func ManualTest

func ManualTest(t TestingT)

ManualTest is a function which should be called at the beginning of tests which shouldn't run on the bots due to excessive running time, external requirements, etc.

func MarshalIndentJSON

func MarshalIndentJSON(t TestingT, i interface{}) string

MarshalIndentJSON encodes the given interface to an indented JSON string.

func MarshalJSON

func MarshalJSON(t TestingT, i interface{}) string

MarshalJSON encodes the given interface to a JSON string.

func MediumTest

func MediumTest(t TestingT)

MediumTest is a function which should be called at the beginning of an medium-sized test: a test (2-15 seconds) which has dependencies on external databases, networks, etc.

func MustReadFile

func MustReadFile(filename string) string

MustReadFile reads a file from the caller's testdata directory and panics on error.

func MustReadJsonFile

func MustReadJsonFile(filename string, dest interface{})

MustReadJsonFile reads a JSON file from the caller's testdata directory into the given interface and panics on error.

func ReadFile

func ReadFile(filename string) (string, error)

ReadFile reads a file from the caller's testdata directory.

func ReadJsonFile

func ReadJsonFile(filename string, dest interface{}) error

ReadJsonFile reads a JSON file from the caller's testdata directory into the given interface.

func Remove

func Remove(t TestingT, fp string)

Remove attempts to remove the given file and asserts that no error is returned.

func RemoveAll

func RemoveAll(t TestingT, fp string)

RemoveAll attempts to remove the given directory and asserts that no error is returned.

func ShouldRun

func ShouldRun(testType string) bool

ShouldRun determines whether the test should run based on the provided flags.

func SmallTest

func SmallTest(t TestingT)

SmallTest is a function which should be called at the beginning of a small test: A test (under 2 seconds) with no dependencies on external databases, networks, etc.

func TempDir

func TempDir(t TestingT) (string, func())

TempDir is a wrapper for ioutil.TempDir. Returns the path to the directory and a cleanup function to defer.

func TestDataDir

func TestDataDir() (string, error)

TestDataDir returns the path to the caller's testdata directory, which is assumed to be "<path to caller dir>/testdata".

func WriteFile

func WriteFile(t TestingT, filename, contents string)

WriteFile writes the given contents to the given file path, reporting any error.

Types

type TestingT

type TestingT interface {
	Error(...interface{})
	Errorf(string, ...interface{})
	Fail()
	FailNow()
	Failed() bool
	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Helper()
	Log(...interface{})
	Logf(string, ...interface{})
	Name() string
	Skip(...interface{})
	SkipNow()
	Skipf(string, ...interface{})
	Skipped() bool
}

TestingT is an interface which is compatible with testing.T and testing.B, used so that we don't have to import the "testing" package except in _test.go files.

Jump to

Keyboard shortcuts

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