Skip to content

Conversation

@egawata
Copy link
Contributor

@egawata egawata commented Feb 26, 2021

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

@dolmen dolmen changed the title Add type names to fail message of assert.Same assert.Same: add type names to fail message Jun 2, 2025
@dolmen dolmen added enhancement pkg-assert Change related to package testify/assert enhancement: output format Enhancement related to formatting of messages labels Jun 2, 2025
Copy link
Collaborator

@dolmen dolmen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No tests provided.

@brackendawson
Copy link
Collaborator

brackendawson commented Aug 30, 2025

#1742 included a change equivalent to this.

return Fail(t, fmt.Sprintf("Not same: \n"+
"expected: %p %#v\n"+
"actual : %p %#v", expected, expected, actual, actual), msgAndArgs...)
"expected: %p %#v (%T)\n"+
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This format string is quite sensitive to the types used and sometimes duplicates details.

It's quite surprising that %#v formats MemberPtr(&Member{"Niko", 18}) as &Member{Name:"Niko", Age:18}

We need to include %T and %p, probably (%T)(%p) is the most conventional?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense,indeed

@egawata
Copy link
Contributor Author

egawata commented Sep 12, 2025

Thank you for comments. 😃
This PR is too old, and has merge conflicts.
I'll close this PR and re-create new one.

@egawata egawata closed this Sep 12, 2025
@egawata egawata deleted the improve_assert_same_message branch September 12, 2025 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement: output format Enhancement related to formatting of messages enhancement pkg-assert Change related to package testify/assert

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants