package main
import "testing"
// Without t.Helper(), all failures would point HERE — useless.
// With it, the line shown is whichever test called assertEqual.
func assertEqual[T comparable](t *testing.T, got, want T, label string) {
t.Helper()
if got != want {
t.Errorf("%s: got %v, want %v", label, got, want)
}
}
func TestSomething(t *testing.T) {
assertEqual(t, 1+1, 2, "addition") // shows THIS line on failure
assertEqual(t, len("hi"), 2, "string length")
// Subtests benefit too
t.Run("multiplication", func(t *testing.T) {
assertEqual(t, 3*4, 12, "mul")
})
}
Create a free account and build your private vault. Share publicly whenever you want.