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

reporting

它能做什么

这项技能讲解如何控制 Ginkgo 测试结果的显示方式,以及如何把结果保存为机器可读的报告。适合希望测试输出清晰,或需要为其他工具提供报告的开发者。

适用场景

  • ✓当你想在屏幕上看到更多或更少的测试结果细节时。
  • ✓当你需要 Ginkgo 测试生成 JSON 或 JUnit 报告文件时。
  • ✓当你想检查 Go 测试的代码覆盖率或性能时。

技能链接

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

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“你能让我的 Go 测试保存一份 JUnit 报告,供我们的持续集成系统读取吗?”

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

工作原理

它展示了哪些命令选项可以输出详细信息、写入报告文件,以及测量覆盖率或速度。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Reporting and profiling

How to produce reports and feed data into them. To read a failing report as an agent (the jq failure-diagnosis workflow), see ginkgo:debugging-failures. Docs: https://onsi.github.io/ginkgo/#generating-machine-readable-reports and https://onsi.github.io/ginkgo/#attaching-data-to-reports.

Console output

Flag Effect
--succinct Minimal (default when running multiple suites).
(none) Normal — detail only for failed specs (default, single suite).
-v Full timeline for every spec; streams live in series.
-vv -v plus node > Enter/< Exit events and every failure (not just the primary).
--trace Full stack trace on every failure, not just panics.
--no-color / GINKGO_NO_COLOR=TRUE Disable color.
--github-output Format console output for GitHub Actions.

In parallel (-p), -v/-vv timelines can't stream — the streams would interleave, so Ginkgo emits each spec's full timeline after that spec completes. → CI flags in ginkgo:ci.

Machine-readable reports

ginkgo -r --json-report=report.json --output-dir=./.reports
  • --json-report is native and richest — an array of types.Report, each with SpecReports[] of types.SpecReport (godoc); build tooling against the types package.
  • --junit-report=x.xml, --teamcity-report=x, --gojson-report=x.go.json exist for external CI but lose Ginkgo metadata — use them to feed a system, not to diagnose. (JUnit maps a Label("owner:XYZ") to the Owner attribute.)
  • --output-dir=DIR collects all files in one place; under -r all suites merge into one report file. --keep-separate-reports writes one PACKAGE_report.json per package instead.
  • Every machine-readable report embeds the full -vv timeline regardless of console verbosity. Run the suite quiet and mine the file — you don't need -vv on the console to get rich detail in the report.

Programmatic reporting nodes

CurrentSpecReport() works in any run-phase closure — use it for in-spec conditional diagnostics:

AfterEach(func() {
  if CurrentSpecReport().Failed() {
    GinkgoWriter.Println(libraryClient.DebugLogs())   // expensive diagnostics, only on failure
  }
})

For custom report files, don't roll your own in AfterEach — use reporting nodes:

  • ReportAfterEach(func(r SpecReport){...}) / ReportBeforeEach — run for every spec including skipped/pending (unlike AfterEach), after all AfterEaches. A failure here fails the spec. In parallel they run on each process concurrently — never write a shared file from them (you'll get interleaved garbage).
  • ReportBeforeSuite / ReportAfterSuite("name", func(r Report){...}) — top-level, and run only on process #1. ReportAfterSuite receives the Report aggregated across all parallel processes — this is the safe place for custom suite-level report files:
ReportAfterSuite("custom report", func(report Report) {
  f, _ := os.Create("report.custom")
  for _, sr := range report.SpecReports {
    fmt.Fprintf(f, "%s | %s\n", sr.FullText(), sr.State)
  }
  f.Close()
})

Reporting nodes can be made interruptible by also accepting SpecContext → ginkgo:timeouts-and-async.

Attaching data to a spec: AddReportEntry

It("is reported", func() {
  AddReportEntry("Publish stats", stats, ReportEntryVisibilityFailureOrVerbose)
})

AddReportEntry(name, value, ...args) attaches arbitrary data to the current spec; it lands in SpecReport.ReportEntries, in all machine-readable reports, and (per visibility) on the console. Visibility enum:

  • ReportEntryVisibilityAlways (default) — always printed.
  • ReportEntryVisibilityFailureOrVerbose — only on failure or -v (like GinkgoWriter).
  • ReportEntryVisibilityNever — never on console, but still in the JSON report.

For failure-only diagnostics, call AddReportEntry from a DeferOnFailure callback (→ ginkgo:writing-specs).

value is JSON-round-tripped into the report (retrieve via entry.GetRawValue() / json.Unmarshal(entry.Value.AsJSON, ...)). A fmt.Stringer/types.ColorableStringer controls console rendering; pass a pointer to capture later mutations. AddReportEntry decorators (Offset, CodeLocation, ReportEntryVisibility) → ginkgo:decorators.

Recap (owned by ginkgo:writing-specs): GinkgoWriter buffers log output a

常见问题

什么是 JUnit 报告?

它是一种标准文件格式,许多构建和测试系统都能读取它来显示测试结果。

我能看到每个测试的运行过程吗?

能。详细输出选项会显示每个测试的时间线,不过并行运行时,每条时间线会在该测试结束后才显示。

这项技能会修改我的测试吗?

不会。它主要涉及命令选项和报告代码,除非你添加报告步骤,否则测试保持不变。

详情

作者的原始描述

Generate, consume, and enrich Ginkgo reports — console verbosity (-v/-vv/--trace/--no-color/--succinct), machine-readable reports (--json-report/--junit-report with --output-dir/--keep-separate-reports), programmatic reporting nodes (ReportAfterEach, ReportAfterSuite, CurrentSpecReport), AddReportEntry with ReportEntryVisibility, and profiling (--cover/--race/--cpuprofile/--memprofile). Use when you need a report file, custom suite-level reporting, attaching data to a spec, controlling console output, or profiling a suite.

repo: onsi/ginkgo

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能