Skip to content

testing: wrong line number from t.Log when using "log := t.Log" #14415

@asadovsky

Description

@asadovsky

Consider the following test program:

package my_test

import "testing"

func TestLineNumber(t *testing.T) {
    log := t.Log
    log("test")
}

When run with go test -v, the output is:

=== 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions