golden

package
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package golden provides standard way to write tests with golden files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(t T, data []byte) bool

Equal asserts that the golden file content is equal to the data.

func EqualString

func EqualString(t T, data []byte) bool

EqualString asserts that the golden file content is equal to the data in string format.

func File

func File(t T, data []byte) []byte

File returns the golden file content for the test. If OVERRIDE_TEST_DATA env is set to true, the golden file will be created with the content of the data. OVERRIDE_TEST_DATA is read only once at the start of the test and it's value is not updated. Depending of the test structure the golden file and it's directories arew created in ./testdata/{testFuncName}/{subTestName}.golden or ./testdata/{testFuncName}/{testFuncName}.golden.

func FileString

func FileString(t T, data []byte) string

FileString returns the output of golden.File as a string.

func Request added in v2.0.3

func Request(t T, client Client, req *http.Request, expectedStatusCode int) (*http.Response, bool)

Request sends the request and asserts that the response status code is equal to the expectedStatusCode. It also asserts that the response body is equal to the golden file content using EqualString. Example test function:

func TestAPI(t *testing.T) {
	tests := []struct {
		name         string
		method       string
		path         string
		body         io.Reader
		expectedCode int
	}{
		{
			name:   "create user",
			method: "POST",
			path:   "/api/v1/user",
			body: strings.NewReader(`{"name": "someone"}`),
			expectedCode: 200,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			req := must.NewRequest(t, tt.method, "http://127.0.0.1:8080"+tt.path, tt.body)
			golden.Request(t, http.DefaultClient, req, tt.expectedCode)
		})
	}
}

Types

type Client added in v2.0.3

type Client interface {
	Do(req *http.Request) (*http.Response, error)
}

type T

type T interface {
	Errorf(format string, args ...interface{})
	FailNow()
	Name() string
	Helper()
}

Jump to

Keyboard shortcuts

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