gotest

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

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

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

在 TestMain 中一次性接入上报:

package checkout_test

import (
    "os"
    "testing"

    "github.com/tracera-dev/tracera-go/gotest"
)

func TestMain(m *testing.M) {
    os.Exit(gotest.Run(m))
}

绑定 testCaseId 与 autotestName

使用标准 testing,并以命令式方式绑定:

func TestCheckout(t *testing.T) {
    gotest.TestCase(t, 42)
    // gotest.TestCase(t, 42, "e2e/checkout")
}

在并行测试中,请在 gotest.TestCase 之前调用 t.Parallel()。

Skip

使用 gotest.Skip / gotest.Skipf,原因会作为 comment 写入结果(单独的 t.Skip 不会):

func TestCheckout(t *testing.T) {
    gotest.TestCase(t, 42)
    gotest.Skip(t, "needs a seeded account")
}

步骤

使用 tracera.Step("…", func() { … })。Go 没有方法级的步骤装饰器。tracera.Setup / tracera.Teardown 内的步骤进入结果的 准备 / 清理;其他步骤进入 测试。

tracera.Step 内的 panic 会把错误消息写入结果卡片。若测试因 t.Fatal / t.Error 失败,结果仍为 Failed,但失败文本只留在 go test 输出里。

import "github.com/tracera-dev/tracera-go"

func TestCheckout(t *testing.T) {
    gotest.TestCase(t, 10)

    tracera.Setup("open shop", func() {})
    tracera.Step("add item", func() {
        tracera.Step("nested leaf", func() {})
    })
    tracera.Teardown("close shop", func() {})
}

注释、错误与附件

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

tracera.Comment、tracera.Error 和附件会添加到当前打开的步骤;若没有打开的步骤,则添加到结果。tracera.AttachmentFile 接收文件路径;tracera.AttachmentBytes 接收名称、字节和 MIME 类型。附件在结果上报时上传。

import tracera "github.com/tracera-dev/tracera-go"

tracera.Comment("result-level note")
tracera.Error("result-level error")
tracera.AttachmentFile("testdata/screenshot.png")
tracera.Step("open login", func() {
    tracera.Comment("step note")
    tracera.AttachmentBytes("body.json", []byte(`{"ok":true}`), "application/json")
})

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

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

参数化测试

使用 t.Run 的各行。若希望每行有稳定的名称,请在 TestCase 中传入名称:

func TestMatrix(t *testing.T) {
    for _, row := range []struct{ login string }{{"alice"}, {"bob"}} {
        t.Run(row.login, func(t *testing.T) {
            gotest.TestCase(t, 7, "login as "+row.login)
        })
    }
}