testgolden

package module
v0.0.2-0...-09c1f1f Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: MIT Imports: 11 Imported by: 0

README

go-testgolden

Oftentimes, tests are about comparing an instance of datastructure to an expected one. However, it might quickly become a lot of work to update those expectations when modifying the code. Like the butterfly effect, one tiny change to one line of code might have big consequences on the outcome of the algorithm and can therefore impact a lot of tests.

Golden files are files containing a serialized version of the expected instance of the datastructure that is used by the test as the expectation for the outcome of the algorithm. This repository offer primitives for creating and maintaining golden files, also called goldens.

Getting Started

go get github.com/clems4ever/go-testgolden
type MyStruct struct {
	ID      int
	Title   string
	Content string
}

func TestExample(t *testing.T) {
	s := MyStruct{
		ID:      1,
		Title:   "this is a title",
		Content: "Here is a little story",
	}

	// Generate a path of a file to be stored in the test directory dedicated to this specific test.
	goldenPath := testgolden.GetTestDataFilePath(t, "golden.json")

	// If the env variable CI=1 is not set, the golden is updated with the latest version of the struct,
	// but if the variable is set, the CI will check that the golden and the actual data is equal.
	testgolden.RequireDataEqualToJSON(t, goldenPath, s)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCurrentTestDataDir

func GetCurrentTestDataDir(t *testing.T) string

GetCurrentTestDataDir calculates the path to the test data corresponding to this exact test. This function must be called directly withing the test function but not deeper in the stack.

func GetProjectRootDir

func GetProjectRootDir() string

func GetTestDataFilePath

func GetTestDataFilePath(t *testing.T, path string) string

func GetTestsDir

func GetTestsDir() string

func RequireBytesEqualToGolden

func RequireBytesEqualToGolden(t *testing.T, goldenFilePath string, reader io.Reader)

func RequireDataEqualToJSON

func RequireDataEqualToJSON[T any](t *testing.T, goldenFilePath string, data T)

func RequireDataEqualToYaml

func RequireDataEqualToYaml[T any](t *testing.T, goldenFilePath string, data T)

Types

This section is empty.

Jump to

Keyboard shortcuts

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