testify icon indicating copy to clipboard operation
testify copied to clipboard

assert.Same: add type names to fail message

Open egawata opened this issue 4 years ago • 0 comments

Summary

This PR adds a type names of testing values to a fail message of assert.Same().

Changes

Adds type names of testing values to a fail message of assert.Same(), which might be necessary to identify the cause of failing.

Motivation

Currently, when assert.Same() fails, it outputs a message with pointer values and its go-syntax representation of actual/expected values.

assert.Same() would fail when types of two values are different. The following test code would fail:

type Member struct {
	Name string
	Age  int
}

type MemberPtr *Member

func TestMember(t *testing.T) {
	m := &Member{"Niko", 18}
	p := MemberPtr(m)

	assert.Same(t, m, p)
}

However, it seems no difference between actual and expected in its fail message, because they point to the same object. It would be hard to know why the test fails from this message.

--- FAIL: TestMember (0.00s)
    same_test.go:20:
                Error Trace:    same_test.go:20
                Error:          Not same:
                                expected: 0xc000132198 &main.Member{Name:"Niko", Age:18}
                                actual  : 0xc000132198 &main.Member{Name:"Niko", Age:18}
                Test:           TestMember
FAIL
FAIL    command-line-arguments  0.234s
FAIL

This PR adds type names of two values. The output would be like this:

--- FAIL: TestMember (0.00s)
    same_test.go:20:
                Error Trace:    same_test.go:20
                Error:          Not same:
                                expected: 0xc00000e1b0 &main.Member{Name:"Niko", Age:18} (*main.Member)
                                actual  : 0xc00000e1b0 &main.Member{Name:"Niko", Age:18} (main.MemberPtr)
                Test:           TestMember
FAIL
FAIL    command-line-arguments  0.517s
FAIL

Related issues

egawata avatar Feb 26 '21 18:02 egawata