Golang测试脚本可以发出警告而不是错误吗?

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

Can Golang test scripts issue warnings rather than errors?

问题

我有一组测试,由于外部第三方问题可能无法通过。
当出现这种情况时,我不希望测试失败,但希望得到通知。

发出 t.Errorf() 不是一个好主意,因为它会停止所有后续的测试。有没有一种"警告"我可以触发,让测试脚本发布并继续进行剩余的测试?

英文:

I have a set of tests that may not pass due to external 3rd party issues.
I don't want the test to fail when this condition occurs but would like to be made aware.

Issuing a t.Errorf() is not idea because it will stop all subsequent tests. Is there some kind of "Warning" I can trigger that the test script would post and then continue with the remainder of the tests?

答案1

得分: 3

go test工具类似于编译器。对于编译器来说,要么编译成功,要么失败,没有警告。我认为你能得到的最接近的结果是使用t.Skip。它会停止当前测试的执行,但不会标记为失败。然而,在go test的输出中你将看不到任何信息,所以你需要使用go test -v

下面是一个示例包,如果addExternal函数失败,就使用了t.Skipf

package app

import "testing"

func add(a, b int) int {
    return a + b
}

func addExternal(a, b int) int {
    return 4
}

func divide(a, b int) int {
    return a / b
}

func TestThing(t *testing.T) {
    got := add(1, 2)
    want := 3
    if got != want {
        t.Errorf("add(1, 2) = %d, want %d", got, want)
    }
}

func TestExternalThing(t *testing.T) {
    got := addExternal(3, 4)
    want := 7
    if got != want {
        t.Skipf("addExternal(3, 4) = %d, want %d", got, want)
    }
}

func TestAnotherThing(t *testing.T) {
    got := divide(6, 3)
    want := 2
    if got != want {
        t.Errorf("divide(6, 3) = %d, want %d", got, want)
    }
}

运行上述代码的输出如下。请注意,返回状态为0,表示包已通过测试。

$ go test -v
=== RUN   TestThing
--- PASS: TestThing (0.00s)
=== RUN   TestExternalThing
--- SKIP: TestExternalThing (0.00s)
        app_test.go:29: addExternal(3, 4) = 4, want 7
=== RUN   TestAnotherThing
--- PASS: TestAnotherThing (0.00s)
PASS
ok      github.com/jcbwlkr/app  0.006s
$ echo $?
0

请注意,如果我将t.Skipf更改为t.Errorft.Fatalf,则会得到以下输出。

$ go test -v
=== RUN   TestThing
--- PASS: TestThing (0.00s)
=== RUN   TestExternalThing
--- FAIL: TestExternalThing (0.00s)
        app_test.go:29: addExternal(3, 4) = 4, want 7
=== RUN   TestAnotherThing
--- PASS: TestAnotherThing (0.00s)
FAIL
exit status 1
FAIL    github.com/jcbwlkr/app  0.005s
$ echo $?
1

包中的其他测试仍然会运行。如果我测试多个包,例如使用go test -v ./...,我相信它们也会继续运行。

英文:

The go test tool is like the compiler. To the compiler something either compiles or doesn't, there are no warnings. I think the closest thing you're going to get is to use t.Skip. It will stop execution of the current test but does not mark it as failed. You will not see anything in the output of go test however so you have to use go test -v.

Here's an example package that uses t.Skipf if the addExternal function fails.

package app

import "testing"

func add(a, b int) int {
	return a + b
}

func addExternal(a, b int) int {
	return 4
}

func divide(a, b int) int {
	return a / b
}

func TestThing(t *testing.T) {
	got := add(1, 2)
	want := 3
	if got != want {
		t.Errorf("add(1, 2) = %d, want %d", got, want)
	}
}

func TestExternalThing(t *testing.T) {
	got := addExternal(3, 4)
	want := 7
	if got != want {
		t.Skipf("addExternal(3, 4) = %d, want %d", got, want)
	}
}

func TestAnotherThing(t *testing.T) {
	got := divide(6, 3)
	want := 2
	if got != want {
		t.Errorf("divide(6, 3) = %d, want %d", got, want)
	}
}

And here's the output from running that. Note the return status is 0 and the package is considered to have passed

$ go test -v
=== RUN   TestThing
--- PASS: TestThing (0.00s)
=== RUN   TestExternalThing
--- SKIP: TestExternalThing (0.00s)
        app_test.go:29: addExternal(3, 4) = 4, want 7
=== RUN   TestAnotherThing
--- PASS: TestAnotherThing (0.00s)
PASS
ok      github.com/jcbwlkr/app  0.006s
$ echo $?
0

Note though that if I change the t.Skipf to t.Errorf or t.Fatalf I get this output

$ go test -v
=== RUN   TestThing
--- PASS: TestThing (0.00s)
=== RUN   TestExternalThing
--- FAIL: TestExternalThing (0.00s)
        app_test.go:29: addExternal(3, 4) = 4, want 7
=== RUN   TestAnotherThing
--- PASS: TestAnotherThing (0.00s)
FAIL
exit status 1
FAIL    github.com/jcbwlkr/app  0.005s
$ echo $?
1

The other tests in the package are still ran. If I was testing multiple packages such as with go test -v ./... I believe they would also still be ran.

huangapple
  • 本文由 发表于 2016年9月7日 05:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/39357910.html
匿名

发表评论

匿名网友

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

确定