testutils

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package testutils provides utility functions for testing and test assertions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertContains

func AssertContains(t TestingT, str, substr string, msgAndArgs ...interface{})

AssertContains asserts that a string contains a substring.

func AssertDeepEqual

func AssertDeepEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{})

AssertDeepEqual asserts that two values are deeply equal using reflection.

func AssertEqual

func AssertEqual[T comparable](t TestingT, expected, actual T, msgAndArgs ...interface{})

AssertEqual asserts that two values are equal.

func AssertError

func AssertError(t TestingT, err error, msgAndArgs ...interface{})

AssertError asserts that an error is not nil.

func AssertErrorType

func AssertErrorType[T error](t TestingT, err error, msgAndArgs ...interface{})

AssertErrorType asserts that an error is of a specific type.

func AssertEventuallyTrue

func AssertEventuallyTrue(t TestingT, condition func() bool, timeout time.Duration, msgAndArgs ...interface{})

AssertEventuallyTrue asserts that a condition becomes true within a timeout.

func AssertFalse

func AssertFalse(t TestingT, condition bool, msgAndArgs ...interface{})

AssertFalse asserts that a condition is false.

func AssertNil

func AssertNil(t TestingT, value interface{}, msgAndArgs ...interface{})

AssertNil asserts that a value is nil.

func AssertNoError

func AssertNoError(t TestingT, err error, msgAndArgs ...interface{})

AssertNoError asserts that an error is nil.

func AssertNotContains

func AssertNotContains(t TestingT, str, substr string, msgAndArgs ...interface{})

AssertNotContains asserts that a string does not contain a substring.

func AssertNotEqual

func AssertNotEqual[T comparable](t TestingT, expected, actual T, msgAndArgs ...interface{})

AssertNotEqual asserts that two values are not equal.

func AssertNotNil

func AssertNotNil(t TestingT, value interface{}, msgAndArgs ...interface{})

AssertNotNil asserts that a value is not nil.

func AssertNotPanics

func AssertNotPanics(t TestingT, fn func(), msgAndArgs ...interface{})

AssertNotPanics asserts that a function does not panic.

func AssertPanics

func AssertPanics(t TestingT, fn func(), msgAndArgs ...interface{})

AssertPanics asserts that a function panics.

func AssertTrue

func AssertTrue(t TestingT, condition bool, msgAndArgs ...interface{})

AssertTrue asserts that a condition is true.

func BenchmarkFunction

func BenchmarkFunction(fn func(), iterations int) time.Duration

BenchmarkFunction benchmarks a function and returns the duration per operation.

func CaptureOutput

func CaptureOutput(fn func()) (stdout, stderr string)

CaptureOutput captures stdout/stderr output from a function.

func CreateTempDir

func CreateTempDir(t *testing.T, pattern string) string

CreateTempDir creates a temporary directory for testing.

func CreateTempFile

func CreateTempFile(t *testing.T, pattern, content string) string

CreateTempFile creates a temporary file for testing.

func GetCallerInfo

func GetCallerInfo(skip int) (filename string, lineNumber int)

GetCallerInfo returns information about the calling function.

func MockHTTPServer

func MockHTTPServer(responses map[string]MockResponse) *httptest.Server

MockHTTPServer creates a mock HTTP server for testing.

Types

type MockResponse

type MockResponse struct {
	StatusCode int
	Headers    map[string]string
	Body       string
	JSONBody   interface{}
}

MockResponse represents a mock HTTP response.

func NewMockResponse

func NewMockResponse(statusCode int) MockResponse

NewMockResponse creates a new mock response.

func (MockResponse) WithBody

func (m MockResponse) WithBody(body string) MockResponse

WithBody sets the response body.

func (MockResponse) WithHeader

func (m MockResponse) WithHeader(key, value string) MockResponse

WithHeader adds a header to the mock response.

func (MockResponse) WithJSONBody

func (m MockResponse) WithJSONBody(data interface{}) MockResponse

WithJSONBody sets the response body as JSON.

type TestingT

type TestingT interface {
	Helper()
	Errorf(format string, args ...interface{})
}

TestingT is an interface wrapper around *testing.T

Jump to

Keyboard shortcuts

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