Skip to content

testing: Helper line number has changed in 1.16 #44887

Closed

Description

@ianlancetaylor

This is the test program.

package main_test

import (
	"testing"
)

func TestHelper(t *testing.T) {
	helperA(t)
}

func helperA(t *testing.T) {
	t.Helper()

	t.Run("subtest", func(t *testing.T) {
		t.Helper()
		t.Fatal("fatal error message")
	})
}

When run using go test foo_test.go with Go 1.14 or Go 1.15, this program prints

--- FAIL: TestHelper (0.00s)
    --- FAIL: TestHelper/subtest (0.00s)
        foo_test.go:8: fatal error message
FAIL
FAIL	command-line-arguments	0.009s
FAIL

When run with Go 1.16 it prints

--- FAIL: TestHelper (0.00s)
    --- FAIL: TestHelper/subtest (0.00s)
        foo_test.go:14: fatal error message
FAIL
FAIL	command-line-arguments	0.007s
FAIL

The line number has changed from 8 to 14. Line number 14 is in a function that called t.Helper, so line 8 seems correct and line 14 seems wrong.

This appears to be due to https://golang.org/cl/231717. If I revert that CL, I see line number 8.

CC @bboreham

Activity

  1. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Mar 9, 2021
  2. added this to the Go1.17 milestone on Mar 9, 2021
  3. ianlancetaylor commented on Mar 9, 2021

    @ianlancetaylor
    ContributorAuthor

    @gopherbot Please open backport to 1.16

    In some cases t.Helper seems to be ineffective. It's not critical but it's definitely annoying. We should fix it.

  4. gopherbot commented on Mar 9, 2021

    @gopherbot
    Contributor

    Backport issue(s) opened: #44888 (for 1.16).

    Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

  5. dnephin commented on Mar 15, 2021

    @dnephin
    Contributor

    I just ran into this problem. I have been using a function runCase (see below) to make it easier to work with very large table tests. When a test spans thousands of lines it can be difficult to find the data for a specific failure. This runCase pattern has made it much easier to debug failing tests, and it no longer works due to this regression.

    I notice this issue has been pushed out a couple patch releases. Please let me know if I can be of any help in restoring the old behaviour or testing a fix.

    Example use case:

    func runCase(t *testing.T, name string, fn func(t *testing.T)) {
    	t.Helper()
    	t.Run(name, func(t *testing.T) {
    		t.Helper()
    		t.Log("case:", name)
    		fn(t)
    	})
    }
  6. added a commit that references this issue on Mar 17, 2021
    b00dcf0
  7. gopherbot commented on Mar 17, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/302469 mentions this issue: testing: fix Helper line number change

  8. added 4 commits that reference this issue on Mar 17, 2021
    b49843a
    8f3c2bc
    2ed2b81
    44b90b2
  9. gopherbot commented on Mar 19, 2021

    @gopherbot
    Contributor

    Change https://golang.org/cl/303189 mentions this issue: [release-branch.go1.16] testing: update helperNames just before checking it

  10. locked and limited conversation to collaborators on Mar 19, 2022
  11. added a commit that references this issue on Apr 29, 2024
    6704843
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.release-blocker

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions