Ginkgo Testing
Installation
SKILL.md
Ginkgo Testing
Ginkgo is a BDD-style testing framework for Go, primarily used for E2E tests.
E2E Test Structure
test/e2e/
├── suite_test.go # Suite bootstrap with BeforeSuite/AfterSuite
└── xxx_test.go # Test specs
Suite Bootstrap
package e2e_test
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var BaseURL string
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "E2E Suite")
}
var _ = BeforeSuite(func() {
// Setup: deploy app, initialize clients
BaseURL = "http://localhost:8080"
})
var _ = AfterSuite(func() {
// Cleanup resources
})
Test Specs
package e2e_test
import (
"net/http"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Health API", func() {
var client *http.Client
BeforeEach(func() {
client = &http.Client{}
})
Describe("GET /api/v1/health", func() {
Context("when server is running", func() {
It("should return status ok", func() {
resp, err := client.Get(BaseURL + "/api/v1/health")
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
Expect(resp.StatusCode).To(Equal(http.StatusOK))
})
})
})
})
Common Matchers
// Equality
Expect(actual).To(Equal(expected))
// Errors
Expect(err).NotTo(HaveOccurred())
Expect(err).To(MatchError("message"))
// Collections
Expect(slice).To(HaveLen(3))
Expect(slice).To(ContainElement("foo"))
// Strings
Expect(str).To(ContainSubstring("partial"))
// Numbers
Expect(n).To(BeNumerically(">", 10))
// Async (for eventual consistency)
Eventually(func() int { return getCount() }).Should(Equal(5))
Best Practices
- Use BeforeSuite/AfterSuite for app deployment and cleanup
- Use BeforeEach for per-test setup (e.g., HTTP client)
- Use Context for different scenarios
- Use By() to document test steps
- Use Skip() when preconditions not met
Running E2E Tests
# Run E2E tests
ginkgo ./test/e2e/...
# Verbose output
ginkgo -v ./test/e2e/...
# Focus on specific specs
ginkgo --focus="Health" ./test/e2e/...