I’m happy to announce that Ginkgo, a BDD-style testing framework for Go, and its preferred matcher library Gomega are ready for public release. There’s a comprehensive feature list, on the GitHub READMEs and both projects have extensive documentation written up (Ginkgo and Gomega).
Here’s a quick taste of what a Ginkgo & Gomega test looks like, there are many more samples in the documentation:
Describe("ScoreKeeper", func() {
var scoreKeeper *ScoreKeeper
BeforeEach(func() {
var err error
scoreKeeper, err = NewScoreKeeper("Denver Broncos")
Expect(err).NotTo(HaveOccured())
})
It("should have a starting score of 0", func() {
Expect(scoreKeeper.Score).To(Equal(0))
})
Context("when a touchdown is scored", func() {
BeforeEach(func() {
scoreKeeper.Touchdown("Manning")
})
It("should increment the score", func() {
Expect(scoreKeeper.Score).To(Equal(6))
})
It("should update the scoring player's stats", func() {
Expect(scoreKeeper.Stats).To(HaveKey("Manning"))
Expect(scoreKeeper.Stats["Manning"]["Touchdowns"]).To(Equal(1))
Expect(scoreKeeper.Stats["Manning"]["Points"]).To(Equal(6))
})
})
})
And here’s (a screenshot of) the output of go test
:
So, why write Ginkgo?
Google’s Golang treats testing as a first-class citizen. Go has builtin support for writing and running unit tests in the Xunit style. Go, however, does not come with an assertion/matcher library. Nor does it provide any of the higher-level testing constructs that those in the BDD school of testing have grown fond of. Go’s authors have made it clear that these omissions are intentional.
After spending some time writing tests the Go way, I quickly came to miss all the things about BDD that make BDD-style testing so expressive and productive: descriptive tests that can act as effective documentation, simple shared test set up and teardown, nested contexts, etc… Thankfully, writing a solid full-featured BDD testing framework and matcher library for Go turned out to be relatively straightforward: a testament to Go’s flexibility!
Ginkgo and Gomega are heavily influenced by Cedar (BDD for Objective-C) and Jasmine (BDD for Javascript). If you’re familiar with either you’ll feel right at home with Ginkgo.
So Go forth and BDD! I’m looking forward to your pull requests – especially for any new Gomega matchers that you might find useful.