跳到正文
Skillpedia
测试9.1k · #12307

setup

它能做什么

这项技能展示如何从头在 Go 项目中设置 Ginkgo 测试框架。适合第一次为项目添加 Ginkgo 测试的开发者。

适用场景

  • ✓当你第一次为 Go 项目添加 Ginkgo 测试的时候。
  • ✓当你需要为 Ginkgo 包创建入门测试文件时。
  • ✓当你现有的测试库需要与 Ginkgo 配合使用时。

技能链接

https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/setup/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/setup/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请在我的 Go 项目中设置 Ginkgo 测试,这样我就可以开始编写了。”

每周 5 个新技能,1 分钟读完。

工作原理

它会安装 ginkgo 命令和相关库,创建入门测试文件,并说明如何将其与标准 Go 测试系统连接。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Wiring Ginkgo into a suite

The one-time setup. For the mental model see ginkgo:overview; to start authoring see ginkgo:writing-specs. Docs: https://onsi.github.io/ginkgo/#getting-started.

1. Install the CLI and the libraries

go install github.com/onsi/ginkgo/v2/ginkgo   # the `ginkgo` CLI binary → $GOBIN (put it on $PATH)
go get github.com/onsi/gomega/...             # the Gomega matcher library

ginkgo version confirms the install. The CLI version must match the github.com/onsi/ginkgo/v2 version in your go.mod — re-run go install ... from the package to sync them. The current major version is v2; everything here assumes it. In CI, sidestep version drift by invoking the CLI via go run github.com/onsi/ginkgo/v2/ginkgo (→ ginkgo:ci).

On macOS, XProtect slows Ginkgo's per-package go test -c compiles dramatically — run spctl developer-mode enable-terminal and add your terminal under System Settings > Privacy & Security > Developer Tools.

2. Bootstrap the suite

From inside the package you want to test:

cd path/to/books
ginkgo bootstrap        # generates books_suite_test.go
package books_test

import (
	"testing"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

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

What each line buys you:

  • func TestBooks(t *testing.T) is the single go test entry point. Call RunSpecs exactly once per process — Ginkgo errors if you call it twice (a niche multi-suite-per-process escape hatch exists via extensions/globals.Reset(), see ginkgo:running).
  • RegisterFailHandler(Fail) is the one line of glue connecting Gomega to Ginkgo: it tells Gomega to call Ginkgo's Fail on a failed assertion. Without dot-imports this reads gomega.RegisterFailHandler(ginkgo.Fail).
  • RunSpecs(t, "Books Suite") builds the spec tree and runs it; let it drive *testing.T for you.

3. The _test package convention — and what it forces

ginkgo bootstrap puts the suite in package books_test, not package books. Go specially permits a *_test package to live in the same directory as the package it tests, compiled as a separate package. The consequence: your suite has no access to books's unexported internals — it must import and exercise the external API. That's deliberate, nudging you to test behavior, not implementation. (You may use plain package books for true white-box unit tests when you need the internals.)

4. Dot-imports and the alternatives

Ginkgo and Gomega are DSLs, so the docs dot-import both into the suite's namespace to read naturally. Recommend this strongly to the user. If they'd rather keep the global namespace clean:

  • Alias the whole DSL (slight g. stutter everywhere):
    import g "github.com/onsi/ginkgo/v2"
    var _ = g.Describe("Books", func() { g.It("works", func() {}) })
    
  • Mix-and-match the dsl subpackages — dot-import the parts you want, namespace the rest:
    import (
        . "github.com/onsi/ginkgo/v2/dsl/core"        // containers, setup, subjects, RunSpecs, By, GinkgoT
        "github.com/onsi/ginkgo/v2/dsl/decorators"    // Label, Ordered, Serial, …
    )
    var _ = It("core dot-imported", decorators.Label("namespaced decorator"), func() {})
    
    Available: dsl/core, dsl/decorators, dsl/reporting (ReportAfterEach, AddReportEntry, …), dsl/table (DescribeTable, Entry). They re-export from the root package — no behavioral difference, purely namespacing.
  • ginkgo bootstrap --nodot generates a no-dot bootstrap to start from.

5. Add spec files

ginkgo generate book      # generates book_test.go with a top-level Describe + the books import

You can write specs directly in the suite file, but ginkgo generate <subject> scaffolds a per-file Describe in the same _test package. Now write specs → ginkgo:writing-specs.

6. Third-party libraries that want a *testing.T

Matcher and mocking libraries take a *testing.T (or testing.TB). Ginkgo has no real *testing.T per spec, so pass the seam instead:

  • GinkgoT() returns a value implementing every *testing.T method — hand it to testify, gomock, etc.
  • GinkgoTB() returns a testing.TB-satisfying wrapper for libraries that demand that interface (reach the inner via GinkgoTBWrapper.GinkgoT).
assert.Equal(GinkgoT(), foo{}.Name(), "foo")          // testify i
常见问题

什么是 Gomega?

Gomega 是一个配套库,提供 Ginkgo 测试用来确认结果的检查功能。

Ginkgo 版本需要匹配吗?

需要。ginkgo 命令应与项目 go.mod 文件中列出的 Ginkgo 版本一致。

我能把 Ginkgo 和 testify 或 gomock 一起用吗?

可以。这项技能说明了如何与这些库共享测试控制功能。

详情

作者的原始描述

Wire Ginkgo into a Go package — install the ginkgo CLI and Ginkgo+Gomega, ginkgo bootstrap to generate the suite_test.go (TestXxx/RegisterFailHandler(Fail)/RunSpecs), the package xxx_test convention, dot-import alternatives (aliased import, dsl/* subpackages, --nodot), ginkgo generate, and *testing.T interop via GinkgoT()/GinkgoTB() for testify/gomock. Use when first adding Ginkgo to a repo, bootstrapping a suite, or integrating a *testing.T-based library.

repo: onsi/ginkgo

热度

GitHub 星标
9.1k
排名
#12307
许可证
MIT
更新时间
2026/10/9

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能