testutils

package
v0.0.0-...-44533ca Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertErrorIs

func AssertErrorIs(t *testing.T, expected, actual error, message string)

AssertErrorIs asserts actual contains expected via errors.Is(). Special handling for testutils.ExpectedOneOfErr: succeeds if *any* listed error matches.

Usage example:

var errs testutils.ExpectedOneOfErr
errs = append(errs, ErrInvalidEmail, ErrMissingField)
testutils.AssertErrorIs(t, errs, apiErr, "should include validation error")

func CheckError

func CheckError(t *testing.T, name string, expectedErr error, actualErr error)

func CheckErrors

func CheckErrors(t *testing.T, name string, expectedErrs []error, actualErr error)

func CheckOutput

func CheckOutput(t *testing.T, name string,
	expected any, actual any,
)

func CheckOutputWithError

func CheckOutputWithError(t *testing.T, name string,
	expected any, expectedErr error,
	actual any, actualErr error,
)

CheckOutputWithError verifies both output values and returned errors produced by a test case.

Special handling:

  • If expectedErr is nil, any actual error fails the test.
  • If expectedErr is a StrError, comparison allows either:
  • errors.Is match, OR
  • substring match of the error message.
  • Otherwise, strict errors.Is comparison is required.

func DataFromFileAs

func DataFromFileAs[T any](t *testing.T, testFileName string) T

DataFromFileAs is similar to DataFromFile but additionally marshalls data into specified type T.

Types

type CompareResult

type CompareResult struct {
	OK   bool     // True if comparison passed completely, false otherwise
	Diff []string // List of human-readable failure descriptions, empty if OK
}

CompareResult represents the aggregated result of one or more comparisons performed during a test.

It collects multiple assertion failures and defers test termination until Validate is called, allowing a single test run to report all mismatches instead of failing on the first error.

A CompareResult is successful when OK is true. Any call that records a difference (e.g., AddDiff, Assert, AssertErr) sets OK to false and appends a human-readable message to Diff.

After performing comparisons, Validate must be called to fail the test if any mismatches were recorded.

Example:

main := NewCompareResult()

create := NewCompareResult()
create.Assert("Create", expectedCreate, actualCreate)
create.AssertErr("Create.Err", expectedCreateErr, actualCreateErr)

update := NewCompareResult()
update.Assert("Update", expectedUpdate, actualUpdate)
update.AssertErr("Update.Err", expectedUpdateErr, actualUpdateErr)

main.Merge(create)
main.Merge(update)

main.Validate(t, "TestFlow")

func NewCompareResult

func NewCompareResult() *CompareResult

NewCompareResult creates a successful comparison result instance.

func (*CompareResult) AddDiff

func (r *CompareResult) AddDiff(diff string, args ...any) *CompareResult

AddDiff marks the comparison as failed and appends a custom failure message.

This is the primary way to report simple failures like row count mismatches or pagination URL differences.

func (*CompareResult) AddDifference

func (r *CompareResult) AddDifference(diff string) *CompareResult

func (*CompareResult) Assert

func (r *CompareResult) Assert(dataName string, expectedData, gotData any) bool

Assert compares two data structures using github.com/go-test/deep.Equal and records a formatted mismatch report for the specified data name. Returns true if structures match exactly.

Example output:

Data[0].Fields[stagename] mismatch:
❌ Prospecting != PROSPECTING

Data[0].Raw[OpportunityContactRoles] mismatch:
❌ map[totalSize]: 2 != 3

No-op (returns true) if structures match exactly.

func (*CompareResult) AssertErr

func (r *CompareResult) AssertErr(dataName string, expectedErr, actualErr error) bool

AssertErr compares expected and actual errors and records a mismatch in the result if they do not match.

Matching rules:

  • If both expectedErr and actualErr are nil → considered a match.
  • If only one is nil → mismatch.
  • Otherwise, errors are compared using errors.Is (semantic match).
  • If expectedErr is of type StrError, errors are compared as substrings.

Returns true if the errors match, false if a mismatch is found.

func (*CompareResult) Merge

func (r *CompareResult) Merge(other *CompareResult)

Merge combines another CompareResult into the receiver.

Updates OK status (true only if both were true) and concatenates all Diff messages. Ignores nil other results. Used for chaining multiple sub-comparisons.

func (*CompareResult) Validate

func (r *CompareResult) Validate(t *testing.T, testName string)

Validate finalizes the comparison and fails the test if any mismatches were recorded.

If the CompareResult is successful (OK == true), Validate is a no-op.

Otherwise, it builds a structured failure message that includes the provided testName and all collected Diff entries, then terminates the test using t.Fatal. Each diff is numbered in order of occurrence to improve readability.

This method is intended to be called once at the end of a test after all assertions have been executed.

type ExpectedOneOfErr

type ExpectedOneOfErr []error

ExpectedOneOfErr is a testutils wrapper for multiple errors that signals AssertErrorIs should check if *any* contained error matches the actual error. Enables flexible multi-error assertions.

func (ExpectedOneOfErr) Error

func (e ExpectedOneOfErr) Error() string

type FileData

type FileData []byte

func DataFromFile

func DataFromFile(t *testing.T, testFileName string) FileData

DataFromFile is used for server mocking. Files must be located under ./test directory relative to the test runner.

type ParallelRunner

type ParallelRunner[C any] struct {
	FilePath  string
	TestTitle string
	Function  func(ctx context.Context, conn C, filePath string) (string, error)
}

type ParallelRunners

type ParallelRunners[C any] []ParallelRunner[C]

func (ParallelRunners[C]) Run

func (r ParallelRunners[C]) Run(ctx context.Context, conn C) []string

Run will execute test suites in parallel. Note: filePath must be neighbouring to the caller of this method.

type StrError

type StrError struct {
	// contains filtered or unexported fields
}

StrError is a marker error type used in tests to indicate that an error should primarily be compared by its string content.

Unlike ordinary errors that rely strictly on errors.Is matching, StrError allows fallback comparison via substring matching of the error message. This is useful when:

  • the concrete error type is unstable or wrapped differently
  • dynamic context is added to the error
  • only the semantic message matters for the test

Test-helpers interpret this type as a signal that textual comparison is acceptable when strict error identity does not match.

func StringError

func StringError(text string) StrError

StringError creates a StrError from a plain string.

Instead of asserting exact error identity, the testing utilities will allow message-based comparison when this marker type is used.

func (StrError) Error

func (e StrError) Error() string

Jump to

Keyboard shortcuts

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