Ikai Lan says

I say things!

Archive for November 2017

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

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

https://github.com/stretchr/testify

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.

Advertisements

Written by Ikai Lan

November 7, 2017 at 10:21 am

Is go test running slowly with your sqlite3 tests?

I have to look this up every time I clean up my environment, so I’ll write a quick post about it.

I love go test. It runs fast … except when it doesn’t. I noticed after adding a sqlite3 dependency for unit testing that it started to take 30 seconds to run.

Digging into it, I found this link to a discussion on golang-nuts. Basically: the root cause is that it was compiling the 5mb .c file each time.

The suggestion is to use go install on the sqlite3 dependency. If this works for you, great. It didn’t seem to work for me – possibly because I’m using vendoring and govend? I’m not sure. I didn’t spend too much time digging into it.

The magic command for me was this:

go test -i -v

That installed the dependencies, which made subsequent runs of go test much faster.

Written by Ikai Lan

November 2, 2017 at 2:57 pm