Use the most specific and descriptive assertion methods available in your test framework to improve test readability and failure diagnostics. Prefer methods that clearly state intent and produce helpful error messages.
Use the most specific and descriptive assertion methods available in your test framework to improve test readability and failure diagnostics. Prefer methods that clearly state intent and produce helpful error messages.
Examples:
require.Empty(t, collection)
instead of require.Len(t, collection, 0)
require.NotZero(t, count)
instead of require.True(t, count > 0)
assert.EqualExportedValues(t, expected, actual)
instead of proto.Equal()
to get helpful diffs on failurefunc TestCollectionAttributes(t *testing.T) {
// Define test cases for both string and int keys
testCases := []struct{
name string
keyType string
expected interface{}
}{
{"StringKey", "string", expectedStringResult},
{"IntKey", "int", expectedIntResult},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Test logic with tc.keyType and tc.expected
})
}
}
Also use descriptive test names that follow a pattern like TestComponent_Behavior_Condition
(e.g., TestLogger_FilesArentRotated_WhenDisabled
) to clearly communicate what’s being tested.
These practices make tests more maintainable, easier to debug when they fail, and the intent clearer to other developers.
Enter the URL of a public GitHub repository