Contents

Testing in golang with "Is" framework

Contents

Today’s post is gonna be a short one. I’d like to recommend a framework which I used personally on a number of occasions.

Golang comes with a great environment and a set of tools. Tests are first class citizens. Why would you need additional framework for your tests then? Purely for convenience.

is API

is is a mini framework, at the moment of writing this post, the API is comprised of four functions:

  • is.Equal
  • is.True
  • is.NoErr
  • is.Fail

and frankly, majority of the times, that’s all you’ll need. The framework itself doesn’t distract you from the work, yet provides convenience to write tests faster and express the intention clearer. Compare the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
func TestIfReturnsCorrectResults(t *testing.T) {
	is := is.New(t)
	is.Equal(1+2, add(1, 2))
}

func TestIfReturnsCorrectResults2(t *testing.T) {
	expected := 1 + 2
	if res := add(1, 2); res != expected {
		t.Fatalf("%d != %d", res, expected)
	}
}

I’m sure we all agree that the former is much more concise and quicker to write.

API documentation

Tests usually serve two purposes. The first one is obvious: quality assurance. The second one is API documentation. As far as the latter is concerned, is serves that purpose perfectly. Tests written with is assertions can be, more or less, read like a natural spoken language which greatly improves comprehension.

Give is a try. I’m pretty sure you won’t regret it.