Quick introduction to Ginkgo and Gomega
You'll learn about behavior-driven development (BDD) and two Go libraries - Ginkgo and Gomega, in this lesson.
We'll cover the following
Ginkgo and Gomega provide a robust and easy-to-use framework for practicing BDD in Go.
What is BDD?
BDD stands for behavior-driven development. The basic idea is to define your system’s behavior at a higher level of abstraction. Often, less technical stakeholders can participate and validate these specifications, which increases the likelihood that the developers actually build a system that meets the requirements. The key point is to connect the behavioral specifications to actual tests. Ginkgo is a BDD framework for Go that can help writing behavior-driven tests.
Quick introduction to Ginkgo
Ginkgo is a framework that builds on top of the standard Go testing package and tooling. You define your BDD tests, but under the cover, Ginkgo creates standard Go tests.
This is powerful for several reasons. It’s easy to migrate to Ginkgo tests because they can co-exist side by side with normal Go tests. You can also take advantage of many other library tools that integrate with the standard Go testing support. This collaborative ecosystem effect is a testament to the wisdom of the Go designers that included testing as a core capability.
Ginkgo lets you specify your tests in hierarchical and higher-level constructs that are close to natural language. You use building blocks like: Describe
, Context
, and It
to structure your tests. You provide setup and teardown at different levels using BeforeEach
, AfterEach
, BeforeSuite
, and AfterSuite
. You can also use Ginkgo to run asynchronous tests.
Here is an example of what Ginkgo tests look like:
var _ = Describe("Repo manager tests", func() {
BeforeEach(func() {
...
})
AfterEach(func() {
...
})
Context("Tests for failure cases", func() {
It("Should fail with invalid base dir", func() {
...
})
It("Should fail with empty repo list", func() {
...
})
})
Context("Tests for success cases", func() {
It("Should get repo list successfully", func() {
...
})
It("Should get repo list successfully with non-git directories", func() {
...
})
It("Should get repo list successfully with non-git directories", func() {
...
})
It("Should create branches successfully", func() {
...
})
It("Should commit files successfully", func() {
})
})
})
Quick introduction to Gomega
Gomega is a general-purpose assertion library that can be used with any Go test framework, including standard Go tests. However, it is a great complement to Ginkgo. Although, it is not required, it is a very popular option. Gomega is used inside the test functions and provides a slew of useful assertions. Gomega provides two alternative and equivalent forms of assertions. You can use the Expect()
syntax or the Ω()
syntax which gives Gomega its name interchangeably.
Here are a couple of examples in both forms that are functionally identical.
The first example uses both forms to check that a value equals to 5:
Ω(value).Should(Equal(5))
Expect(value).To(Equal(5))
The second example uses both forms to check that today is not Monday:
Ω(today).ShouldNot(Equal("Monday"))
Expect(today).ToNot(Equal("Monday"))
Try it by running the application below.
Get hands-on with 1400+ tech skills courses.