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

overview

它能做什么

这项技能清楚地介绍 Ginkgo 测试工具在 Go 中的工作方式。适合刚开始 Ginkgo 测试项目,或正在规划测试任务的开发者。

适用场景

  • ✓当你第一次在 Go 项目中使用 Ginkgo 测试的时候。
  • ✓当你需要决定如何处理 Ginkgo 项目中的测试任务时。
  • ✓当你的 Ginkgo 测试表现出你没有预料到的行为时。

技能链接

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

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“我刚接触 Go 的 Ginkgo,开始之前你能解释一下它的测试是如何组织的吗?”

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

工作原理

它说明 Ginkgo 会先建立所有测试的列表,然后再运行它们,并为具体主题指向其他指南。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Ginkgo: the mental model

Ginkgo is an expressive BDD-style testing framework for Go, paired with the Gomega matcher library. You build suites out of nested container nodes (Describe/Context/When) and subject nodes (It), with setup nodes (BeforeEach/AfterEach/…) supplying state. You drive it with the ginkgo CLI. Strongly prefer ginkgo over go test.

Read the canonical narrative docs at https://onsi.github.io/ginkgo/ — they are the source of truth. This skill is the orientation; the other skills go deep.

The one idea: tree construction, then running

Ginkgo runs your suite in two distinct phases. Internalizing this explains nearly every Ginkgo gotcha:

  1. Tree-construction phase. Ginkgo invokes every container body exactly once to discover the structure of your suite. It collects — but does not run — the closures you pass to setup and subject nodes. The result is a tree it flattens into a list of specs.
  2. Run phase. Ginkgo walks the flattened spec list (randomized, possibly in parallel) and, for each spec, runs its setup closures, then its one subject closure, then its cleanup closures.

Container bodies run at construction time. Setup/subject closures run later, at run time. The consequences you must internalize:

  • No assertions in container bodies. They'd run once during construction, with no spec active — not as part of any test. Put assertions in It or BeforeEach.
  • No initialization in container bodies. A variable set in a Describe body is set once, shared across every spec, and mutated by whichever spec runs first. Declare in the container, initialize in BeforeEach so every spec gets a pristine copy. → ginkgo:writing-specs.
  • Loops that build specs run at construction time — that's how you generate specs dynamically, but it also means closure-captured loop variables and any data the loop reads must be available then, not in BeforeSuite. → ginkgo:tables-and-dynamic-specs.

Spec independence — the assumption everything rests on

Ginkgo assumes every spec is independent. This is what lets it randomize order (--randomize-all), run specs in parallel across processes (-p), and run any subset (--focus/labels). The rule that keeps you honest: declare in container, initialize in setup. Deliberate cross-spec dependencies break randomization and parallelism — when you genuinely need ordering, say so explicitly with an Ordered container (→ ginkgo:ordering-and-flakes) rather than relying on definition order.

The Parallelism Model

Ginkgo's parallelism model is process-based: each process runs a subset of the specs, with no shared memory.

The node taxonomy at a glance

  • Containers — Describe, Context, When (identical; pick the one that reads best). Organize the tree.
  • Subjects — It, Specify (identical). The actual test; contains the assertions.
  • Setup — BeforeEach/AfterEach (around every spec), JustBeforeEach/JustAfterEach (split configuration from creation), BeforeAll/AfterAll (once per Ordered container), BeforeSuite/AfterSuite (once per suite), SynchronizedBeforeSuite/SynchronizedAfterSuite (for common setup, then per-process setup when parallel). DeferCleanup registers teardown inline; DeferOnFailure registers failure-only diagnostics that run before teardown.
  • Reporting — ReportAfterEach/ReportAfterSuite etc. observe results. → ginkgo:reporting.
  • Decorators — modifiers attached to any node: Serial, Ordered, Label, Focus, FlakeAttempts, SpecTimeout, … → ginkgo:decorators.

Failures work by panic: Fail (which Gomega calls for you) panics; Ginkgo recovers it, records the failure, and runs cleanup. A goroutine that might fail needs defer GinkgoRecover() or its panic crashes the suite. → ginkgo:timeouts-and-async.

Where to go next

  • Wiring Ginkgo into a project (bootstrap, RunSpecs, the CLI, dot-import alternatives) → ginkgo:setup
  • Authoring specs (containers, subjects, setup/cleanup, the construction-time pitfalls) → ginkgo:writing-specs
  • Table-driven and generated specs (DescribeTable, shared behaviors) → ginkgo:tables-and-dynamic-specs
  • Looking up a decorator → ginkgo:decorators
  • Running a subset (focus, skip, labels) → ginkgo:filtering
  • Running in parallel / integration suites → ginkgo:parallelism
  • **Ordered containers and fl
常见问题

什么是 Ginkgo?

Ginkgo 是 Go 编程语言的测试框架,采用易读、接近自然语句的写法。

这项技能会替我编写测试吗?

它提供指导测试编写所需的背景知识,同一套指南中的其他文章会介绍具体细节。

我应该使用 ginkgo 命令吗?

这项技能建议使用 ginkgo 命令行工具,而不是标准的 go test 命令。

详情

作者的原始描述

The Ginkgo mental model for writing Go tests — the one idea that explains everything (Ginkgo builds a spec tree at construction time, then runs it) and its consequences for how you write specs, plus spec independence and the node taxonomy. Use this first when you start working with Ginkgo in a project, or to decide how to approach a Ginkgo task. Routes to the other ginkgo:* skills.

repo: onsi/ginkgo

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能