Go Lint that follows standards described in TestComments.
To install it, run:
go install github.com/manuelarte/testcomments@latestAnd then use it with
testcomments [-equality-comparison.reflect=true|false] [-equality-comparison.equal=true|false]
[-got-before-want=true|false] [-identify-function=true|false]
[-table-driven-format.type=map|slice] [-table-driven-format.inlined=true|false] ./...Parameters:
equality-comparison.reflect:true|false(defaulttrue) Checksreflect.DeepEqualcan be replaced by newercmp.Equal.equality-comparison.equal:true|false(defaulttrue) Checks helper test functions to compare two structs that can be replaced by eithercmp.Equalorcmp.Diff.got-before-want:true|false(defaulttrue) Check that the failure message outputs the actual value that the function returned before printing the value that was expected.identify-function:true|false(defaulttrue) Check that the failure messages int.Errorfcontains the function name.table-driven-format.type:map|slice(default ``) Check that the table-driven tests are either Map or Slice, empty to leave it as it is.table-driven-format.inlined:true|false(defaultfalse) Check that the table-driven tests are inlined in theforloop.
This linter detects the expression:
if !reflect.DeepEqual(got, want) {
t.Errorf("MyFunction got %v, want %v", got, want)
}And lint that the newer cmp.Equal or cmp.Diff should be used.
For more use cases and examples, check equality-comparison.
Note
Suggested Fix can't be supported since it could potentially imply adding go-cmp dependency
and reflect.DeepEqual can't be directly replaced by cmp.Equal or cmp.Diff.
This linter detects helper functions like:
func areEqual(a, b MyStruct) bool {
return a.Name && b.Name && a.Surname == b.Surname
}And propose to use cmp.Equal or cmp.Diff.
Test outputs should output the actual value that the function returned before printing the value that was expected.
So prefer failure messages like YourFunc(%v) = %v, want %v over want: %v, got: %v.
Note
Suggested Fix can't be supported since it would imply changing the original failure message.
In most tests, failure messages should include the name of the function that failed, even though it seems obvious from the name of the test function.
Prefer:
t.Errorf("YourFunc(%v) = %v, want %v", in, got, want)
and not:
t.Errorf("got %v, want %v", got, want)
Note
Suggested Fix may be supported.
Feature that checks consistency when declaring your table-driven tests. The options are:
tests := map[string]struct {
in int
out int
} {
"test1": {
in: 1,
out: 1,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := abs(test.in)
if got != test.out {
t.Errorf("abs(%d) = %d, want %d", test.in, got, test.out)
}
})
}for name, test := range map[string]struct {
in int
out int
} {
"test1": {
in: 1,
out: 1,
},
} {
t.Run(name, func(t *testing.T) {
got := abs(test.in)
if got != test.out {
t.Errorf("abs(%d) = %d, want %d", test.in, got, test.out)
}
})
}tests := []struct {
name string
in int
out int
} {
{
name: "test1",
in: 1,
out: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := abs(test.in)
if got != test.out {
t.Errorf("abs(%d) = %d, want %d", test.in, got, test.out)
}
})
}for _, test := range []struct {
name string
in int
out int
} {
{
name: "test1",
in: 1,
out: 1,
},
} {
t.Run(test.name, func(t *testing.T) {
got := abs(test.in)
if got != test.out {
t.Errorf("abs(%d) = %d, want %d", test.in, got, test.out)
}
})
}