Godog

go get github.com/tracera-dev/tracera-go/godog

需要 Go ≥ 1.25 和 Godog ≥ 0.16(见 自动化测试适配器)。godog 在 pkg.go.dev 上的页面。

环境变量与运行模式:自动化测试适配器。

接入套件与场景初始化器,使标准 After 钩子进入 清理(Godog 按注册顺序执行 After 钩子):

import (
    "github.com/cucumber/godog"
    tracera_godog "github.com/tracera-dev/tracera-go/godog"
)

suite := godog.TestSuite{
    TestSuiteInitializer: tracera_godog.RegisterSuite,
    ScenarioInitializer: tracera_godog.Initialize(func(sc *godog.ScenarioContext) {
        sc.Before(setup)
        sc.After(teardown)
        // step definitions …
    }),
}

绑定 testCaseId 与 autotestName

推荐在 Scenario 上使用标签:

@tracera.id:42
Scenario: Checkout
  Given the cart is open

可选的 @tracera.name: 使用 URL 编码。若未指定名称标签,则 autotestName 取场景名称。

也可以用 tracera.TestCase(42) 以命令式方式绑定。

Skip

在 step definition 中返回 godog.ErrSkip 可跳过场景。若要把原因写入结果的 comment,这样包装:

return fmt.Errorf("needs a seeded account: %w", godog.ErrSkip)

也可用 Godog 的 --tags 排除场景。

若步骤为 pending(尚未实现)或 undefined(没有对应的 step definition),Godog 在默认模式下不会把场景判为 Failed — Tracera 将整个场景上报为 Skipped。这些步骤不会出现在 步骤 中,结果上也没有错误。

步骤

Gherkin 步骤进入结果的 测试。嵌套请使用 tracera.Step("…", func() { … })。场景的 Before/After 钩子进入 准备 / 清理;套件级钩子不会上报。

func addItem() error {
    tracera.Step("nested leaf", func() {})
    return nil
}

注释、错误与附件

限制与允许的 MIME 类型:自动化测试适配器。

调用 tracera.Comment、tracera.Error、tracera.AttachmentFile 和 tracera.AttachmentBytes,内容会添加到当前打开的 Gherkin 步骤或结果。

若在新的 goroutine 中调用 Tracera API(步骤、注释、错误、附件),先在测试 goroutine 中保存上下文,再用 tracera.RunWithContext 运行工作代码 — 否则这些调用不会写入报告:

ctx := tracera.GetContext()
go tracera.RunWithContext(ctx, func() {
    tracera.Step("worker", func() { /* ... */ })
})

Scenario 并发(godog.Options.Concurrency > 1)无需额外 Tracera 配置。

参数化测试

每一行 Examples 都会得到 Godog 已填好 <…> 的场景名称。在 Outline 或 Scenario 上写固定的 @tracera.id:;若每行 id 不同,则调用 tracera.TestCase。

@tracera.id:42
Scenario Outline: Login as <login>
  Given the user signs in as "<login>"
  Examples:
    | login |
    | ok    |
    | sso   |