Knowledge
Search knowledge... ⌘K
Knowledge · Guidelines · go
Fixture Management
How to create, organize, and consume test fixtures in Go packages
Metadata
go go recommended
Procedures
Showing 3 of 6
- 1 Create testdata/ directory in the target package
Go toolchain ignores testdata/ directories (language-level convention). Place fixture files here for automatic exclusion from builds.
mkdir -p pkg/server/testdata
- 2 Create fixture YAML files following naming convention
Name: {entity}_{scenario}.yml. Each file has type: fixture header, entity, scenario, tags, input, and expected sections.# pkg/server/testdata/server_valid_creation.yml type: fixture entity: server scenario: valid-creation tags: [happy-path, api, unit] input: name: "test-vps-01" host: "10.0.0.1" expected: status: 201 body: name: "test-vps-01" - 3 Create a fixture loader in testutil/
Build a helper that reads YAML fixtures and returns typed structs. Use t.Helper() for clean test output.
// pkg/server/testutil/fixtures.go func LoadFixture(t *testing.T, name string) *Fixture { t.Helper() data, err := os.ReadFile(filepath.Join("testdata", name+".yml")) require.NoError(t, err) var f Fixture require.NoError(t, yaml.Unmarshal(data, &f)) return &f }
Tools
- go test
- go test -update
- gopkg.in/yaml.v3