如何获取测试包以指示调用者测试失败的位置?

huangapple go评论82阅读模式
英文:

How to get the test package to indicate the caller where the test failed?

问题

我正在测试中使用一个函数来包装错误,代码如下:

在checks.go中:

func NoError(t *testing.T, err error, message ...string) {
    if err != nil {
        t.Error(message)
    }
}

在我的测试文件中,比如my_test.go,我有类似这样的代码:

func TestThis(t *testing.T){
    err := someFuncWithError(...)
    NoError(t, err, "error found")
}

当发生错误时,t.Error()指示错误发生在checks.go而不是my_test.go中的行。我可以理解为什么会这样。**我该如何使NoError(...)打印出在my_test.go中引发错误的行?**也就是说,如果我在my_test.go中调用t.Error()时的行为方式。

也就是说,我得到的是:

    checks.go:33: some test failed

我想要的是:

    my_test.go:12: test failed here

我尝试过的方法:

调用runtime:

_, f, l, _ := runtime.Caller(1)
t.Errorf("%s:%d %s, in %s", f, l, "some error", t.Name())

但这仍然导致t.Error()打印出类似"checks.go:12"的内容。增加runtime.Caller()中的skip参数也没有帮助。

我还查看了如何在测试套件中设置深度,但我找不到方法来实现。

英文:

I am wrapping testing.Error in a func like so:

In checks.go

func NoError(t *testing.T, err error, message ...string) {
		if err != nil {
			t.Error(message)
		}
}

In my test file, say, my_test.go, I have something like this:

func TestThis(t *testing.T){
    err := someFuncWithError(...)
    NoError(t, err, "error found")
}

When an error happens, t.Error() indicates that the error happened in checks.go and not the line in my_test.go. I can see why that happens. How do I make it so that NoError(...) prints the line which threw the error in the my_test.go? I.e. The way it behaves if I were to call t.Error() in my_test.go.

IOW, I get:

    checks.go:33: some test failed

What I want:

    my_test.go:12: test failed here

Things I tried:

calling runtime:

		_, f, l, _ := runtime.Caller(1)
		t.Errorf("%s:%d %s, in %s", f, l, "some error", t.Name())

But that still resulted in t.Error() printing something like "checks.go:12". Increasing the skip param in runtime.Caller() didn't help.

I also looked at how to set the depth in the testing suite, but I couldn't find a way to do it.

答案1

得分: 2

使用t.Helper()函数来表示你的辅助函数是一个辅助函数。

根据Godoc的说明:

func (c *T) Helper()

Helper将调用的函数标记为测试辅助函数。在打印文件和行信息时,该函数将被跳过。Helper可以同时从多个goroutine中调用。

示例:

func NoError(t *testing.T, err error, message ...string) {
    t.Helper()
    if err != nil {
        t.Error(message)
    }
}
英文:

Use the t.Helper() function to indicate that your helper function is, well, a helper function.

From the Godoc:

> func (c *T) Helper()
>
> Helper marks the calling function as a test helper function. When printing file and line information, that function will be skipped. Helper may be called simultaneously from multiple goroutines.

Example:

func NoError(t *testing.T, err error, message ...string) {
    t.Helper()
    if err != nil {
        t.Error(message)
    }
}

huangapple
  • 本文由 发表于 2023年3月21日 18:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75800050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定