Write tests that are direct, focused, and free of unnecessary complexity. Remove redundant test fields, use direct assertions with expected values, and leverage existing fixtures instead of creating temporary content.
Write tests that are direct, focused, and free of unnecessary complexity. Remove redundant test fields, use direct assertions with expected values, and leverage existing fixtures instead of creating temporary content.
Key principles:
require.Equal
) with expected output instead of calculating or checking properties within testsExample of simplification:
// Before: Complex test with unused fields
type test struct {
errType errorType
err error // unused
resCode int
msg string // unused
errTypeToStatusCode ErrorTypeToStatusCode
}
// After: Simplified test structure
type test struct {
errType errorType
resCode int
errTypeToStatusCode ErrorTypeToStatusCode
}
// Use direct values instead of variables
api.respondError(w, &apiError{tc.errType, errors.New("message")}, "test")
This approach makes tests more maintainable, easier to understand, and less prone to errors from unused or redundant code.
Enter the URL of a public GitHub repository