【问题标题】:Is there a way to chain asserts with testify?有没有办法用 testify 链接断言?
【发布时间】:2017-08-21 01:40:59
【问题描述】:

我真的很喜欢 testify 为 go test 带来的东西。但是,我翻阅了文档,没有看到有关如何处理多个断言的任何内容。

Go 是否处理“第一次失败”,即它在第一个错误断言处失败,还是只关注测试方法中的最后一个断言?

【问题讨论】:

    标签: testing go testify


    【解决方案1】:

    您可以使用与 assert 具有完全相同接口的 testify/require,但它会在失败时终止执行。 http://godoc.org/github.com/stretchr/testify/require

    import (
        "testing"
        "github.com/stretchr/testify/require"
        "github.com/stretchr/testify/assert"
    )
    
    func TestWithRequire(t *testing.T) {
        require.True(t, false) // fails and terminates
        require.True(t, true) // never executed
    }
    
    func TestWithAssert(t *testing.T) {
        assert.True(t, false) // fails
        assert.True(t, false) // fails as well
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2018-05-01
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      相关资源
      最近更新 更多