testing: wrong line number from t.Log when using "log := t.Log" #14415
Comments
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 And what is more interesting is it persists across function calls. Notice that the As a stripped down version, I have something like func TestEncode(t *testing.T) {
_ = t.Log -> L29
...
...
}
func TestLineNumber(t *testing.T) {
log := t.Log
log("test") -> L44
} So, even if I execute only the |
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. |
Consider the following test program:
When run with
go test -v
, the output is: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 thetesting.T
logging function. It appears that thelog := t.Log
line is implicitly creating a closure, i.e. adding to the depth of the call stack, resulting in an incorrect line number.The text was updated successfully, but these errors were encountered: