You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
testing: wrong line number from t.Log when using "log := t.Log" #14415
=== RUN TestLineNumber
--- PASS: TestLineNumber (0.00s)
my_test.go:6: test
PASS
The line number is wrong - it should be 7, not 6.
The testing package internally uses runtime.Caller(3) to identify the user code calling the testing.T logging function. It appears that the log := t.Log line is implicitly creating a closure, i.e. adding to the depth of the call stack, resulting in an incorrect line number.
(I have put the test code inside hex_test.go. Let's ignore that)
The testing.(*common).Log-fm indicates that a closure is being created the moment something like fn := t.Log is done.
And what is more interesting is it persists across function calls. Notice that the Log-fm line is at 29 and the actual call is at 44. That is because it is happening in another Test function altogether.
Activity
agnivade commentedon Aug 20, 2017
I looked into this. There doesn't seem to be an easy fix from inside the "testing" package. A look inside the call stack reveals -
(I have put the test code inside hex_test.go. Let's ignore that)
The
testing.(*common).Log-fmindicates that a closure is being created the moment something likefn := t.Logis done.And what is more interesting is it persists across function calls. Notice that the
Log-fmline is at 29 and the actual call is at 44. That is because it is happening in another Test function altogether.As a stripped down version, I have something like
So, even if I execute only the
TestLineNumberfunction, the stack shows that the closure is being created at L29.ianlancetaylor commentedon Aug 21, 2017
As of 1.9 this should now be possible, at the cost of being slightly more awkward, by writing
Closing because this is kind of special purpose and there is (now) a workaround.