#development #golang #pattern #testing
I'm using the testify
package a lot when writing tests using Go. I find they make my test code much clearer, more concise and easier to read.
However, when you use it in your tests, you need to be very careful when choosing between assert
and require
. They look the same but are different in the way they behave. Which one you choose depends on the behaviour you are looking for.
If you use assert
, your test will look like:
1package yours
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7)
8
9func TestSomething(t *testing.T) {
10 assert.Equal(t, 123, 123, "they should be equal")
11 assert.NotEqual(t, 123, 456, "they should not be equal")
12 assert.Nil(t, object)
13 if assert.NotNil(t, object) {
14 assert.Equal(t, "Something", object.Value)
15 }
16}
With assert
, you will get a log entry for each assertation.
Changing it to use require
looks similar until you run the actual tests:
1package yours
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7)
8
9func TestSomething(t *testing.T) {
10 require.Equal(t, 123, 123, "they should be equal")
11 require.NotEqual(t, 123, 456, "they should not be equal")
12 require.Nil(t, object)
13 if require.NotNil(t, object) {
14 require.Equal(t, "Something", object.Value)
15 }
16}
With require
, as soon as you run the tests, the first requirement which fails interrupts and fails the complete test. So, if the first requirement fails, the rest of the test will be skipped.
An alternative to testify is the is
package from Mat Ryer. The biggest difference is that this library only supports the same flow as the assert
package. The advantage is that the output is much more concise and the API is a lot more straightforward. I guess I'll have to give it a go when writing tests again…
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.