go.astrophena.name/base Module
Base Go packages for my projects.
package testutil
import "go.astrophena.name/base/testutil"
Package testutil provides helpers for common testing scenarios.
Index
- func AssertEqual(t *testing.T, got, want any)
- func BuildTxtar(t *testing.T, dir string) []byte
- func ExtractTxtar(t *testing.T, ar *txtar.Archive, dir string)
- func MockHTTPClient(h http.Handler) *http.Client
- func Run(t *testing.T, glob string, f func(t *testing.T, match string))
- func RunGolden(t *testing.T, glob string, f func(t *testing.T, match string) []byte, ...)
- func UnmarshalJSON(t *testing.T, b []byte) V
Functions
func AssertEqual
func AssertEqual(t *testing.T, got, want any)
AssertEqual fails the test if got is not deeply equal to want. It prints both values for easy comparison upon failure.
func BuildTxtar
func BuildTxtar(t *testing.T, dir string) []byte
BuildTxtar creates a txtar-formatted byte slice from the contents of a directory.
func ExtractTxtar
func ExtractTxtar(t *testing.T, ar *txtar.Archive, dir string)
ExtractTxtar extracts a txtar archive to a specified directory.
func MockHTTPClient
func MockHTTPClient(h http.Handler) *http.Client
MockHTTPClient returns an http.Client that directs all requests to the provided http.Handler, allowing to test HTTP clients without making real network requests.
func Run
func Run(t *testing.T, glob string, f func(t *testing.T, match string))
Run runs a subtest for each file that matches the provided glob pattern. The subtest name is the file's path relative to its directory.
func RunGolden
func RunGolden(t *testing.T, glob string, f func(t *testing.T, match string) []byte, update bool)
RunGolden runs a test for each file matching a glob pattern and compares the result of a function f with the contents of a corresponding ".golden" file.
If update is true, the golden file is updated with the new result instead of being compared.
func UnmarshalJSON
func UnmarshalJSON[V any](t *testing.T, b []byte) V
UnmarshalJSON parses a JSON byte slice into a value of type V, failing the test if an error occurs.