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, context ...string)
- func False(t testing.TB, v bool)
- func File(t testing.TB, file, want 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, context ...string)
- 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 Diff ¶
Diff fails if got != want and provides a rich diff.
If got and want are structs, unexported fields will be included in the comparison.
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 Err ¶ added in v0.2.0
Err fails if err == nil.
err := shouldReturnErr() test.Err(t, err, "shouldReturnErr")
func File ¶ added in v0.7.0
File fails if the contents of the given file do not match want.
It takes the name of a file (relative to $CWD/testdata) and the contents to compare.
If the contents differ, the test will fail with output equivalent to test.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, "expected.txt", "hello\n")
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 Ok ¶
Ok fails if err != nil, optionally adding context to the output.
err := doSomething() test.Ok(t, err, "Doing something")
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.