Skip to content

[Code Quality] Add t.Helper() to test helper functions for better debugging #12874

@github-actions

Description

@github-actions

Description

Test helper functions lack t.Helper() calls, causing test failure stack traces to point to helper function internals instead of the actual failing test cases. With 1,394 table-driven tests and complex setup functions, this severely degrades debugging experience and developer productivity.

Currently, only 5 occurrences of t.Helper() exist across the entire test suite. This issue was identified in the Sergo Report #12711.

Impact

Severity: High (developer productivity)
Affected: 100+ test files (estimated)
Problem: Developers waste time navigating from helper internal lines to actual failing test cases when debugging test failures.

Current Pattern (Problematic)

func setupTestEnvironment(t *testing.T) *TestEnv {
    // Missing t.Helper() - stack traces show internal lines
    tempDir := t.TempDir()
    // ... setup code ...
    return &TestEnv{Dir: tempDir}
}

Correct Pattern

func setupTestEnvironment(t *testing.T) *TestEnv {
    t.Helper()  // ✅ Marks this as a helper - stack traces show caller
    tempDir := t.TempDir()
    // ... setup code ...
    return &TestEnv{Dir: tempDir}
}

Good Examples to Follow

These files already implement the pattern correctly:

  • pkg/cli/compile_integration_test.go:91
  • pkg/cli/deps_test.go:237,252
  • pkg/cli/mcp_add_integration_test.go:21
  • pkg/cli/audit_report_test.go:55,76,87

Implementation Approach

  1. Find candidate functions (semi-automated):

    rg "func \w+\(.*\*testing\.T" --type go -g "*_test.go"
  2. Add t.Helper() as first line in each test helper function

  3. Run tests to verify correct line numbers in failures

  4. Add golangci-lint rule to enforce pattern (thelper linter)

Success Criteria

  • All test helper functions (100+ estimated) have t.Helper() as first line
  • Full test suite passes without regressions
  • Test failures show correct line numbers (caller, not helper internals)
  • thelper linter enabled in golangci-lint configuration
  • Pattern documented in CONTRIBUTING.md

Priority

High - Affects developer productivity across all test debugging sessions.

Estimated Effort

Medium (2-3 hours, semi-automated via ripgrep + manual review)

Source

Extracted from Sergo Report: Table-Driven Test & Init Function Hygiene Analysis #12711

AI generated by Discussion Task Miner - Code Quality Improvement Agent

  • expires on Feb 14, 2026, 5:19 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions