...

/

Writing BDD Tests for Multi-git

Writing BDD Tests for Multi-git

Now, let's practice behavior-driven development (BDD) and testing in Go.

In this lesson, you will learn how to use the Ginkgo and Gomega frameworks to write and run tests. Along the way, we will compare BDD-based testing with standard Go testing. We will also implement unit tests for multi-git.

Writing BDD tests for multi-git

There are several steps when writing Ginkgo tests for a package:

  • Creating a test suite
  • Planning the test hierarchy
  • Writing the actual tests

Let’s tackle these steps one by one

Bootstrapping a test suite

The Ginkgo bootstrap command creates a test suite file that registers the Ginkgo fail handler and runs all your Ginkgo tests as nested tests of the single test method. This is the integration of Ginkgo with the standard testing package of Go. Here is the result for the repo_manager package of multi-git:

package repo_manager_test

import (
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
    "testing"
)

func TestRepoManager(t *testing.T) {
    RegisterFailHandler(Fail)
    RunSpecs(t, "RepoManager Suite")
}

Building the hierarchy

The top-level of the hierarchy is a Describe block that describes what the group of test cases in this group does. You could have multiple Describe blocks if you need to divide your tests even further. For example, you may want to put performance tests and benchmarks in a separate Describe block. You can nest Describe blocks, too.

For the repo_manager package, there is just one Describe block.

var _ =
...