Documentation
¶
Index ¶
- func AssertErrorIs(t *testing.T, expected, actual error, message string)
- func CheckError(t *testing.T, name string, expectedErr error, actualErr error)
- func CheckErrors(t *testing.T, name string, expectedErrs []error, actualErr error)
- func CheckOutput(t *testing.T, name string, expected any, actual any)
- func CheckOutputWithError(t *testing.T, name string, expected any, expectedErr error, actual any, ...)
- func DataFromFileAs[T any](t *testing.T, testFileName string) T
- type CompareResult
- func (r *CompareResult) AddDiff(diff string, args ...any) *CompareResult
- func (r *CompareResult) AddDifference(diff string) *CompareResult
- func (r *CompareResult) Assert(dataName string, expectedData, gotData any) bool
- func (r *CompareResult) AssertErr(dataName string, expectedErr, actualErr error) bool
- func (r *CompareResult) Merge(other *CompareResult)
- func (r *CompareResult) Validate(t *testing.T, testName string)
- type ExpectedOneOfErr
- type FileData
- type ParallelRunner
- type ParallelRunners
- type StrError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertErrorIs ¶
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 CheckErrors ¶
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.
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 ParallelRunner ¶
type ParallelRunners ¶
type ParallelRunners[C any] []ParallelRunner[C]
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 ¶
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.