Search code examples
testinggotestify

Is there a way to chain asserts with testify?


I really like what testify brings to go test. However, I dug through the documentation and didn't see anything on how to handle multiple asserts.

Does Go handle "first failure", in the sense it fails at the first bad assert, or will it only concern itself with the last assert in the test method?


Solution

  • You can use testify/require which has the exact same interface as assert, but it terminates the execution on failure. 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
    }