DEV Community

Cover image for Expectate: A Go Testing Utility
Steven Kaufman
Steven Kaufman

Posted on

Expectate: A Go Testing Utility

Do you love the simplicity of Go testing but hate writing if statements constantly?

Do you like the 'expect' style syntax of testing libraries like jest in JavaScript but don't care for Gomega?

The answer:

Expectate

github.com/gomagedon/expectate

Why is Expectate better than Gomega?


Well, that's a matter of opinion. The main differences are:

  • Expectate is simpler
  • The syntax is a little different (closer to jest)

How is it simpler?


If you've ever used Gomega (the Ginkgo assertion tool)...

You wouldn't describe it as simple. It does a lot. Like a lot a lot. There are a lot of ways to write the same thing. For example:

err := DoSomethingSimple()
Ω(err).ShouldNot(HaveOccurred())
Enter fullscreen mode Exit fullscreen mode

Is the same as:

Ω(DoSomethingSimple()).Should(Succeed())
Enter fullscreen mode Exit fullscreen mode

And that's not to mention the Expect syntax:

g.Expect(DoSomethingSimple()).To(Succeed())
Enter fullscreen mode Exit fullscreen mode

And those are just the different ways to assert that an error is nil!

The Expectate Way:

err := DoSomethingSimple()
expect(err).NotToBe(nil)
Enter fullscreen mode Exit fullscreen mode

That's it.
No alternative syntax.

In fact, Expectate has only 4 methods!

  • ToBe()
  • ToEqual()
  • NotToBe()
  • NotToEqual()

ToBe() checks for simple equality, i.e. var1 == var2

ToEqual() checks for deep equality using google/go-cmp

NotToBe() and NotToEqual() check for the opposite cases just like you would expect! (pun intended)

That's all it does?


Yeah. So what?

It's all syntactic sugar. The whole point is to turn a three-line if statement into one line that looks nice and pretty. Gomega doesn't actually provide much more functionality, just more ways to write the same thing.

Expectate is real nice and lightweight 😎

If I download the latest version of Gomega from GitHub, guess how big the zip file is?

Gomega size:

gomega-1.10.4.zip: 163K

Compared to Expectate:

expectate-1.0.0.zip: 7K


Not to mention that all the production code for Expectate is in a single file, and 80% of that 7K is probably the tests.

So this is just another stupid testing library that doesn't do much?


Right again.

Don't use it for everything!

If you want a more descriptive error message than foo is not bar, just use testing.T like normal!!

A Note On Compatibility


Expectate doesn't have to use testing.T (although you really should)!

Typically you would write an Expectate test like this:

func TestFooIsBar(t *testing.T) {
  expect := expectate.Expect(t)
  expect("foo").ToBe("bar")
}
Enter fullscreen mode Exit fullscreen mode

And the output:

-------- FAIL: TestFooIsBar (0.00s)
    expect.go:34: foo is not bar
Enter fullscreen mode Exit fullscreen mode

The above example uses testing.T (as our lord and savior Rob Pike intended)
But you can use any object that satisfies the Fataler interface!

type Fataler interface {
  Fatal(args ...interface{})
}
Enter fullscreen mode Exit fullscreen mode

So anyway check it out! It's my very new project, so please give lots of feedback, positive or negative! Open all the issues!

github.com/gomagedon/expectate

Top comments (0)