我希望能够包装标准的 golang 测试函数,例如来自测试包的 t.Errorf
。
我尝试了以下方法:
// AssertEqual tests that the expected and actual values match
func AssertEqual(t *testing.T, expected interface{}, actual interface{}) {
switch expected.(type) {
case string:
if expected != actual {
t.Errorf("Error:\nexpected: %s\nactual: %s", expected, actual)
}
default:
t.Errorf("Unsupported type")
}
}
然而,当测试失败时,我会得到我的辅助函数的函数名和行号:
test_asserts.go:12: Error:
expected:
actual: TestValue
有没有一种方法能够在调用方的行号上报告错误?
t.Helper()
将使行号变为正确的行号。请参见下面的答案。 - VonC