Documentation
¶
Overview ¶
Package test provides a lightweight, but useful extension to the std lib's testing package with a friendlier and more intuitive API.
Index ¶
- func CaptureOutput(t testing.TB, fn func() error) (stdout, stderr string)
- func Data(t testing.TB) string
- func DeepEqual(t testing.TB, got, want any)
- func Diff(t testing.TB, got, want any)
- func Equal[T comparable](t testing.TB, got, want T)
- func EqualFunc[T any](t testing.TB, got, want T, equal func(a, b T) bool)
- func Err(t testing.TB, err error)
- func False(t testing.TB, v bool)
- func File(t testing.TB, got, file string)
- func NearlyEqual[T ~float32 | ~float64](t testing.TB, got, want T)
- func NotEqual[T comparable](t testing.TB, got, want T)
- func NotEqualFunc[T any](t testing.TB, got, want T, equal func(a, b T) bool)
- func Ok(t testing.TB, err error)
- func True(t testing.TB, v bool)
- func WantErr(t testing.TB, err error, want bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaptureOutput ¶ added in v0.9.0
CaptureOutput captures and returns data printed to stdout and stderr by the provided function fn, allowing you to test functions that write to those streams and do not have an option to pass in an io.Writer.
If the provided function returns a non nil error, the test is failed with the error logged as the reason.
If any error occurs capturing stdout or stderr, the test will also be failed with a descriptive log.
fn := func() error {
fmt.Println("hello stdout")
return nil
}
stdout, stderr := test.CaptureOutput(t, fn)
fmt.Print(stdout) // "hello stdout\n"
fmt.Print(stderr) // ""
func Data ¶ added in v0.6.0
Data returns the filepath to the testdata directory for the current package.
When running tests, Go will change the cwd to the directory of the package under test. This means that reference data stored in $CWD/testdata can be easily retrieved in the same way for any package.
The $CWD/testdata directory is a Go idiom, common practice, and is completely ignored by the go tool.
Data makes no guarantee that $CWD/testdata exists, it simply returns it's path.
file := filepath.Join(test.Data(t), "test.txt")
func Equal ¶
func Equal[T comparable](t testing.TB, got, want T)
Equal fails if got != want.
test.Equal(t, "apples", "apples") // Passes test.Equal(t, "apples", "oranges") // Fails
func EqualFunc ¶
EqualFunc is like Equal but allows the user to pass a custom comparator, useful when the items to be compared do not implement the comparable generic constraint
The comparator should return true if the two items should be considered equal.
func File ¶ added in v0.7.0
File fails if got does not match the contents of the given file.
It takes a string and the path of a file to compare, use Data to obtain the path to the current packages testdata directory.
If the contents differ, the test will fail with output equivalent to Diff.
Files with differing line endings (e.g windows CR LF \r\n vs unix LF \n) will be normalised to \n prior to comparison so this function will behave identically across multiple platforms.
test.File(t, "hello\n", "expected.txt")
func NearlyEqual ¶ added in v0.8.0
NearlyEqual is like Equal but for floating point numbers where typically equality often fails.
If the difference between got and want is sufficiently small, they are considered equal.
test.NearlyEqual(t, 3.0000000001, 3.0) // Passes, close enough to be considered equal test.NearlyEqual(t, 3.0000001, 3.0) // Fails, too different
func NotEqual ¶
func NotEqual[T comparable](t testing.TB, got, want T)
NotEqual fails if got == want.
test.NotEqual(t, "apples", "oranges") // Passes test.NotEqual(t, "apples", "apples") // Fails
func NotEqualFunc ¶
NotEqualFunc is like NotEqual but allows the user to pass a custom comparator, useful when the items to be compared do not implement the comparable generic constraint
The comparator should return true if the two items should be considered equal.
func WantErr ¶ added in v0.5.0
WantErr fails if you got an error and didn't want it, or if you didn't get an error but wanted one.
It simplifies checking for errors in table driven tests where on any iteration err may or may not be nil.
test.WantErr(t, errors.New("uh oh"), true) // Passes, got error when we wanted one
test.WantErr(t, errors.New("uh oh"), false) // Fails, got error but didn't want one
test.WantErr(t, nil, true) // Fails, wanted an error but didn't get one
test.WantErr(t, nil, false) // Passes, didn't want an error and didn't get one
Types ¶
This section is empty.