Skip to content

Instantly share code, notes, and snippets.

@hamakn
Last active Apr 26, 2020
Embed
What would you like to do?

migrate testify to go-cmp

module github.com/hamakn/go_hello_world/examples/test/gocmp/002_testify_and_gocmp
go 1.14
require (
github.com/google/go-cmp v0.4.0
github.com/stretchr/testify v1.5.1
)
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func RequireEqual(t *testing.T, want, got interface{}, msg string, options ...cmp.Option) {
t.Helper()
if diff := cmp.Diff(want, got, options...); diff != "" {
t.Fatalf("RequireEqual unmatched:\n message: %s\n (-want +got):\n%s", msg, diff)
}
}
// TODO:
// func RequireNil
// func RequireNotNil
// func RequireError
package main
import (
"testing"
"github.com/stretchr/testify/require"
)
type s struct {
I int
}
func TestByTestify(t *testing.T) {
s1 := s{1}
s2 := s{2}
require.Equal(t, s1, s2, "s2 should equal to s1")
}
func TestByGoCmp(t *testing.T) {
s1 := s{1}
s2 := s{2}
RequireEqual(t, s1, s2, "s2 should equal to s1")
}
// % go test ./. -v
// === RUN TestByTestify
// TestByTestify: hoge_test.go:17:
// Error Trace: hoge_test.go:17
// Error: Not equal:
// expected: main.s{I:1}
// actual : main.s{I:2}
//
// Diff:
// --- Expected
// +++ Actual
// @@ -1,3 +1,3 @@
// (main.s) {
// - I: (int) 1
// + I: (int) 2
// }
// Test: TestByTestify
// Messages: s2 should equal to s1
// --- FAIL: TestByTestify (0.00s)
// === RUN TestByGoCmp
// TestByGoCmp: hoge_test.go:24: RequireEqual unmatched:
// message: s2 should equal to s1
// (-want +got):
// main.s{
// - I: 1,
// + I: 2,
// }
// --- FAIL: TestByGoCmp (0.00s)
// FAIL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment