【问题标题】:Why GoConvey Tests fail with error code 0? [closed]为什么 GoConvey 测试失败并显示错误代码 0? [关闭]
【发布时间】:2018-12-17 02:47:11
【问题描述】:

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

【问题讨论】:

  • GoConvey 支持 Go 的原生测试包。既不需要 Web UI 也不需要 DSL;您可以单独使用其中任何一个。
  • @Bert Verhees 我运行 go test,看到一个失败的测试但得到退出代码 0...
  • go test 应该在测试失败时以代码 1 退出,see this,并且使用该值 here。对于各种初始化错误,它似乎使用退出代码 2。如果您能提供一个 minimal reproducible example 来演示问题,那将会很有帮助。

标签: testing go continuous-integration tdd goconvey


【解决方案1】:

好的,发现错误:

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

解决了问题

【讨论】:

    【解决方案2】:

    您可以像普通测试一样运行 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
    

    【讨论】:

    • 我运行了你的示例并得到了失败测试的退出代码 0
    • 不,您没有运行我的示例,您将它包装在其他代码中然后运行它。那是一个完全不同的故事,它让我和你头疼。不要再这样做了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    相关资源
    最近更新 更多