d-test-unit-test

A set of rules for writing unit tests in Go, the programming language used by the examples. It covers test names, table-driven cases, subtests, mocks, assertions, file placement, and basic test quality principles.

In plain words
What is it for?
Use it when writing or reviewing Go unit tests, especially for naming test functions, grouping scenarios with subtests, using mocks, checking errors, and organizing table-driven cases.
Why use it?
It gives a Go team a consistent way to structure tests so they are readable, independent, repeatable, and easier to maintain.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/xyzbit/ai-coding/d-test-unit-test
Clone the repo
git clone --depth 1 https://github.com/xyzbit/AI-Coding
Per session 6 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 871 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00006 $0.00871
Opus 5 $0.00003 $0.00436
Sonnet 5 $0.00001 $0.00174
Haiku 4.5 $0.00001 $0.00087

Measured 2d ago against content hash 9a8c0e3d52bb, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

d-test-unit-test scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

cursor/rules/develop/d-test-unit-test.mdc · 115 lines

What it actually says

Golang 单元测试规范

命名规范

  • 测试文件命名:xxx_test.go
  • 测试函数命名:Test${被测试函数名}
  • 基准测试函数:Benchmark${被测试函数名}
  • 示例函数:Example${被测试函数名}

测试用例组织

  • 使用 t.Run() 进行子测试分组
  • 每个测试场景应该有清晰的描述
  • 使用表驱动测试方式组织多个测试用例
  • 测试覆盖率要求 > 60%

测试代码结构

func TestXxx(t *testing.T) {
    // 1. 准备测试数据
    type args struct {
        // 输入参数
    }
    tests := []struct {
        name    string     // 测试用例名称
        args    args       // 输入参数
        want    interface{} // 期望输出
        wantErr bool       // 是否期望错误
    }{
        // 测试用例列表
    }

    // 2. 执行测试
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            // 3. 准备测试环境(如果需要)
            
            // 4. 执行被测试函数
            
            // 5. 验证结果
            
            // 6. 清理测试环境(如果需要)
        })
    }
}

测试原则

  1. 单一职责:每个测试用例只测试一个功能点
  2. 独立性:测试用例之间相互独立,不应该有依赖关系
  3. 可重复:测试结果应该是稳定的,多次运行结果一致
  4. 简单明了:测试代码应该简单易懂,避免复杂的逻辑

Mock 使用规范

  • 优先使用接口进行依赖注入,便于 mock
  • 推荐使用 go-mocktestify/mock 等工具
  • Mock 对象应该在测试函数开始时创建,结束时销毁

断言规范

  • 推荐使用 testify/assert 包进行断言
  • 使用恰当的断言函数,如:
    • assert.Equal(t, expected, actual)
    • assert.NoError(t, err)
    • assert.True(t, condition)

最佳实践

  1. 测试文件与源文件放在同一目录下
  2. 优先测试接口而不是实现细节
  3. 避免测试私有方法,通过公共接口测试
  4. 合理使用测试辅助函数减少重复代码
  5. 及时清理测试产生的临时资源
  6. 编写测试时考虑边界条件和异常场景

示例

func TestCalculator_Add(t *testing.T) {
    type args struct {
        a, b int
    }
    tests := []struct {
        name    string
        args    args
        want    int
        wantErr bool
    }{
        {
            name: "正常相加",
            args: args{a: 1, b: 2},
            want: 3,
        },
        {
            name: "负数相加",
            args: args{a: -1, b: 1},
            want: 0,
        },
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            c := NewCalculator()
            got := c.Add(tt.args.a, tt.args.b)
            assert.Equal(t, tt.want, got)
        })
    }
}

运行测试

  • 运行所有测试:go test ./...
  • 运行指定包测试:go test ./pkg/...
  • 运行指定函数:go test -run TestXxx
  • 显示详细日志:go test -v
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 115 lines · 6 tokens per session scan A 9a8c0e3d52bb

Subscribe to this mod's changes

d-test-unit-test is a cursor rule published in the GitHub repository xyzbit/AI-Coding (21 stars, last pushed 10mo ago), licensed Apache-2.0. It adds 6 tokens to every session and 871 once invoked, about $0.0000 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.