Choose the most specific and appropriate testify assertion methods for your test scenarios to improve test clarity, error reporting, and maintainability.
Choose the most specific and appropriate testify assertion methods for your test scenarios to improve test clarity, error reporting, and maintainability.
Key guidelines:
require
in EventuallyWithT
functions: As of testify v1.10.0, require
can be used within EventuallyWithT
and will cause early return on failure, triggering a retry on the next tick.require.EventuallyWithT(t, func(t *assert.CollectT) {
trackers, err := auth.GetActiveSessionTrackers(ctx)
require.NoError(t, err) // Use require, not assert
require.Len(t, trackers, 1)
require.Equal(t, helpers.HostID, trackers[0].GetAddress())
})
require.ErrorContains
for error message validation.// Instead of:
require.Error(t, err)
require.Contains(t, err.Error(), tt.errMsg)
// Use:
require.ErrorContains(t, err, tt.errMsg)
// Instead of string comparison:
require.Contains(t, captureStdout.String(), tc.wantOutput())
// Use semantic comparison:
require.JSONEq(t, expectedJSON, actualJSON)
require.YAMLEq(t, expectedYAML, actualYAML)
These specialized assertions provide better error messages, handle formatting differences, and make test intentions clearer to other developers.
Enter the URL of a public GitHub repository