Documentation
¶
Overview ¶
Package testcmp bridges testify and go-cmp, providing assertion helpers that use cmp.Diff under the hood for structured, readable diffs on failure while keeping the testify style.
It also ships pre-built cmp.Option helpers covering the most common cases: timestamps, IDs, and audit fields.
Example:
testcmp.Equal(t, want, got, testcmp.IgnoreTimestamps())
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
Equal asserts that want and got are equal using cmp.Diff. opts are passed directly to cmp.Diff.
Example:
testcmp.Equal(t, want, got, testcmp.IgnoreTimestamps())
func IgnoreAuditFields ¶
IgnoreAuditFields returns an option that ignores common audit fields by name: CreatedAt, UpdatedAt, CreatedBy, UpdatedBy.
Unlike IgnoreTimestamps, this targets fields by name, so other time.Time fields (e.g. ScheduledAt, BirthDate) are still compared.
Example:
testcmp.Equal(t, want, got, testcmp.IgnoreAuditFields())
func IgnoreIDs ¶
IgnoreIDs returns an option that ignores any struct field named exactly "ID" or ending with a lowercase letter followed by "ID", matching the standard Go naming convention (e.g. SupplierID, UserID, AssessmentID).
Fields like VALID or SQUID are not matched because the character before "ID" is uppercase.
If you need to ignore fields that don't follow this convention, use cmpopts.IgnoreFields directly.
Example:
testcmp.Equal(t, want, got, testcmp.IgnoreIDs())
func IgnoreTimestamps ¶
IgnoreTimestamps returns an option that ignores all time.Time values during comparison. Useful when structs contain CreatedAt, UpdatedAt, or similar fields set by the database.
This ignores time.Time by type, so it applies to all time.Time fields and values, not just those with specific names. Use IgnoreAuditFields if you only want to ignore CreatedAt and UpdatedAt.
Example:
testcmp.Equal(t, want, got, testcmp.IgnoreTimestamps())