DEV Community

Discussion on: How I write my unit tests in Go quickly

Collapse
 
jaimemartinez88 profile image
Jaime Martinez

Great read! You can also use testify's require instead of asserting. For example,

From

if assert.NotNil(t, err, "unexpected function error") {
    // here we assert the expected value
    // but only if the error is nil!
    assert.Equal(t, got, want, "unexpected function output)
}

To

got, err:= doSomething()
require.NoError(t, err) // or require.Nil(t, err)  or require.NotNil(t, got)
assert.Equal(t, expected, got.SomeValue)

In this case your test will automatically fail before going into the assertion, and not causing a nil pointer exception!

I also like table tests over BDD but that's just my personal preference.

Collapse
 
ilyakaznacheev profile image
Ilya Kaznacheev

Nice point, thanks!