Example code:
package main
import "testing"
func TestOne(t *testing.T) {
assertEq(t, "A", "B")
assertEq(t, "B", "B")
assertEq(t, "C", "X")
}
func TestTwo(t *testing.T) {
assertEq(t, "A", "A")
assertEq(t, "B", "B")
assertEq(t, "C", "D")
}
func assertEq(t *testing.T, a, b string) {
if a != b {
t.Fatalf("%s != %s, in %s", a, b, t.Name())
}
}
Output:
=== RUN TestOne
prog.go:19: A != B, in TestOne
--- FAIL: TestOne (0.00s)
=== RUN TestTwo
prog.go:19: C != D, in TestTwo
--- FAIL: TestTwo (0.00s)
FAIL
If I call assertEq()
many times, it is difficult to find what is line where the test failed. Can I get the caller line number?
question from:
https://stackoverflow.com/questions/65856284/go-testing-get-caller-line-number 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…