I have a table-driven test case like this one:
func CountWords(s string) map[string]int
func TestCountWords(t *testing.T) {
var tests = []struct {
input s
Use cmp (https://github.com/google/go-cmp) instead:
if !cmp.Equal(src, expectedSearchSource) {
t.Errorf("Wrong object received, got=%s", cmp.Diff(expectedSearchSource, src))
}
It does still fail when the map "order" in your expected output is not what your function returns. However, cmp
is still able to point out where the inconsistency is.
For reference, I have found this tweet:
https://twitter.com/francesc/status/885630175668346880?lang=en
"using reflect.DeepEqual in tests is often a bad idea, that's why we open sourced http://github.com/google/go-cmp" - Joe Tsai
Simplest way:
assert.InDeltaMapValues(t, got, want, 0.0, "Word count wrong. Got %v, want %v", got, want)
Example:
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestCountWords(t *testing.T) {
got := CountWords("hola hola que tal")
want := map[string]int{
"hola": 2,
"que": 1,
"tal": 1,
}
assert.InDeltaMapValues(t, got, want, 0.0, "Word count wrong. Got %v, want %v", got, want)
}