Documentation
¶
Overview ¶
Package fixture reads and checks the fixture files of a test package: the expected outputs and the sample documents under its testdata/fixtures directory.
A test package embeds the directory once and passes the embedded file system to every call:
//go:embed testdata/fixtures
var fixturesFS embed.FS
func TestX(t *testing.T) {
fixture.Assert(t, fixturesFS, "help_root.txt", got)
}
Names are relative to testdata/fixtures. A test never reads a fixture path from the disk.
To rewrite the fixtures from the output of the tests, run them with UPDATE_FIXTURES=1 (not in parallel with the other tests of the package, and review the diff): Assert writes the file in the source tree instead of comparing. CheckUsage fails for a fixture that no test of its package names, and for a test that reads the fixture directory by path (a comment may name it).
This package is not github.com/blackbirdworks/semaphore/testhelpers/fixtures, which holds shared constants.
Index ¶
- Constants
- func Assert[T ~string | ~[]byte](tb testing.TB, fsys embed.FS, name string, got T)
- func CheckUsage(tb testing.TB, root string)
- func JSON[T any](tb testing.TB, fsys embed.FS, name string) *T
- func List(tb testing.TB, fsys embed.FS) []string
- func Read(tb testing.TB, fsys embed.FS, name string) []byte
- func ReadString(tb testing.TB, fsys embed.FS, name string) string
- func YAML[T any](tb testing.TB, fsys embed.FS, name string) *T
Constants ¶
const ( // Dir is the directory of the fixtures of a package, relative to the package, and the path to give to go:embed. Dir = "testdata/fixtures" // UpdateEnv is the environment variable that makes [Assert] rewrite the fixtures. UpdateEnv = "UPDATE_FIXTURES" )
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
Assert compares got with the fixture name. With UPDATE_FIXTURES set in the environment it writes got to the file in the source tree (the working directory of go test is the package directory) and compares nothing.
func CheckUsage ¶
CheckUsage walks the source tree at root and fails the test for every package that has a fixtures directory and
- does not embed it with //go:embed testdata/fixtures,
- has a fixture whose file name no test file of the package contains, or
- has a test file that reads the fixtures directory by its path instead of through this package.
func JSON ¶
JSON decodes the JSON fixture name into a new T; it fails the test when the file is missing or is not JSON of that shape.
func Read ¶
Read returns the bytes of the fixture name of the embedded directory; it fails the test when there is none.
func ReadString ¶
ReadString is Read as a string.
Types ¶
This section is empty.