0

我需要在构建过程中运行我的 GoConvey 测试如何确保go test以错误退出代码(不是 0)退出?

4

2 回答 2

2

好的,发现了错误:

我的代码中有 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())
}

解决了这个问题

于 2018-07-09T13:28:06.827 回答
1

您可以将 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 > go test x Failures:

* /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
于 2018-07-09T12:47:37.227 回答