为什么 GoConvey 测试失败并显示错误代码 0?

Why GoConvey Tests fail with error code 0?

我需要 运行 我的 GoConvey 测试作为构建的一部分 我如何确保 go test 以错误退出代码(不是 0)退出?

你可以 运行 goconvey 作为一个正常的测试,你可以通过编写一个给出可预测结果的小测试来非常简单地测试它,就像这样

package c

import (
    "testing"
    . "github.com/smartystreets/goconvey/convey"
)

func TestConvey(t *testing.T){
    Convey("TestConvey", t, func() {
        So(false, ShouldBeTrue)
    })
}

这应该是结果 xxx 14:44:03 ~/GO/src/c/c > 去测试 X 失败:

* /home/xxx/GO/src/c/c/a_test.go 
Line 10:
Expected: true
Actual:   false


1 total assertion

--- FAIL: TestConvey (0.00s)
FAIL
exit status 1
FAIL    c/c     0.006s

确定发现错误:

我的代码中有 TestMain 在测试之前运行:

func TestMain(m *testing.M) {
    log.SetOutput(ioutil.Discard) // Disable logs.
    m.Run()
}

m.Run 应该用 os.Exit 包裹起来,以便它可以与错误状态代码一起使用:

func TestMain(m *testing.M) {
    log.SetOutput(ioutil.Discard) // Disable logs.
    os.Exit(m.Run())
}

解决了问题