111 words, 1 min read
⚠️ This post links to an external website. ⚠️
Many Go programmers prefer using if-free test assertions to make their tests shorter and easier to read. So, instead of writing if statements with
t.Errorf
:
func Test(t *testing.T) {db := getDB(t)db.Str().Set("name", "alice")age, err := db.Str().Get("age")if !errors.Is(err, redka.ErrNotFound) {t.Errorf("want ErrNotFound, got %v", err)}if age != nil {t.Errorf("want nil, got %v", age)}}
PASSThey would use
testify/assert
(or its evil twin,testify/require
):
func Test(t *testing.T) {db := getDB(t)db.Str().Set("name", "alice")age, err := db.Str().Get("age")assert.ErrorIs(t, err, redka.ErrNotFound)assert.Nil(t, age)}However, I don't think you need
testify/assert
and its 40 different assertion functions to keep your tests clean. Here's an alternative approach.
continue reading on antonz.org
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.