CallerInfo does not respect t.Helper() calls.

Open
#1,702 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
go
Domain
testing-qa

Research direction

Start by reading CallerInfo and the Go testing.T helper behavior, then reproduce the issue with the AssertCount example and assert.Equal. Compare the current trace with the expected output: done means the custom helper frame is omitted while the test caller remains.

Written by the indexing model from the issue text.

Description

bug pkg-assert TB.Helper

Description

I'm trying to implement some custom assertions as part of a test double. I've tried to follow along with the pattern where t.Helper() is called to register my assertion functions as helpers. Within them, I am using testify assertions. Unfortunately, it appears that while the functions all get registered by t.Helper() into t.helperPCs, this information is not used to inform the call trace produced by testify in CallerInfo().

It feels like testify should respect the state in testing.T (although I can see where that might be problematic). Is there another way to get the results desired?

Step To Reproduce

Code like this:

func (s *MyDouble) AssertCount(t assert.TestingT, expected int) {
	if h, ok := t.(tHelper); ok {
		h.Helper()
	}
	assert.Equal(t, expected, s.Count(), "unexpected number of items")
}

Expected behavior

I'd like a trace like:

Error Trace:    .../somelib_test.go:179
Error:          Not equal: 
                expected: 1
                actual  : 0
Test:           Mytest/thesethingsareequal
Messages:       They weren't equal

Actual behavior

Yields a test failure like this:

Error Trace:    .../mydouble.go:460
                .../somelib_test.go:179
Error:          Not equal: 
                expected: 1
                actual  : 0
Test:           Mytest/thesethingsareequal
Messages:       They weren't equal
Dominant language
Go
Stars
26.2k
Forks
1.9k
Avg merge
2d 4h
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stretchr/testify

All issues in stretchr/testify

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.