Prompt
Choose the most specific and appropriate testify assertion methods for your test scenarios to improve test clarity, error reporting, and maintainability.
Key guidelines:
- Use
requireinEventuallyWithTfunctions: As of testify v1.10.0,requirecan be used withinEventuallyWithTand 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())
})
- Use specialized error assertion methods: Instead of combining generic assertions, use specific methods like
require.ErrorContainsfor error message validation.
// Instead of:
require.Error(t, err)
require.Contains(t, err.Error(), tt.errMsg)
// Use:
require.ErrorContains(t, err, tt.errMsg)
- Use structured data assertions: For JSON and YAML comparisons, use semantic equality assertions rather than string matching.
// 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.