fixture

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func Assert[T ~string | ~[]byte](tb testing.TB, fsys embed.FS, name string, got T)

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

func CheckUsage(tb testing.TB, root string)

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

func JSON[T any](tb testing.TB, fsys embed.FS, name string) *T

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 List

func List(tb testing.TB, fsys embed.FS) []string

List returns the names of the fixtures of the embedded directory, in order.

func Read

func Read(tb testing.TB, fsys embed.FS, name string) []byte

Read returns the bytes of the fixture name of the embedded directory; it fails the test when there is none.

func ReadString

func ReadString(tb testing.TB, fsys embed.FS, name string) string

ReadString is Read as a string.

func YAML

func YAML[T any](tb testing.TB, fsys embed.FS, name string) *T

YAML decodes the YAML fixture name into a new T; it fails the test when the file is missing or is not YAML of that shape.

Types

This section is empty.

Jump to

Keyboard shortcuts

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