| name | Ginkgo Testing |
| description | This skill should be used when the user asks to "setup Ginkgo", "write BDD tests", "Ginkgo test suites", "Gomega matchers", "E2E tests", or works with E2E testing in Go using Ginkgo framework. |
| version | 0.2.0 |
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() {
BaseURL = "http://localhost:8080"
})
var _ = AfterSuite(func() {
})
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
Expect(actual).To(Equal(expected))
Expect(err).NotTo(HaveOccurred())
Expect(err).To(MatchError("message"))
Expect(slice).To(HaveLen(3))
Expect(slice).To(ContainElement("foo"))
Expect(str).To(ContainSubstring("partial"))
Expect(n).To(BeNumerically(">", 10))
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
ginkgo ./test/e2e/...
ginkgo -v ./test/e2e/...
ginkgo --focus="Health" ./test/e2e/...