Ikai Lan says

I say things!

Use the testify “require” library go Go assertions instead of the default one

Love assertions in your tests? You’re probably using this library:


One thing that confused me about this library is that assert.NotNil(), assert.NotErr() and other methods return a boolean instead of failing the test. For instance, this test does not fail:

func TestStringCannotCastToUint(t *testing.T) {
    var s interface{}
    s = "can't cast this to int"
    _, err := s.(uint16)
    assert.NotNil(t, err)

It turns out the default assert library doesn’t fail on these boolean asserts. To force it to fail, change the import to this:

import (
    assert "github.com/attic-labs/testify/require"	

You’ve now aliased require, which does fail the test, to assert, with no other code changes. Enjoy!

 P.S. Does the tag work for Go? I’ve tried "go" and "golang" with no luck.


Written by Ikai Lan

November 7, 2017 at 10:21 am