test-rule

A Go testing rule that defines how to name test files and functions and how to structure tests. Table-driven tests keep several inputs and expected results in one list, then run each case separately.

In plain words
What is it for?
Use it when writing Go tests, especially tests for methods or functions with multiple normal and error cases.
Why use it?
It gives a shared testing style, making tests easier to find, read, extend, and run consistently.

Cursor rule for Cursor

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/showcase-gig-platform/cursor-rules-initializer/test-rule
Clone the repo
git clone --depth 1 https://github.com/showcase-gig-platform/cursor-rules-initializer

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 2,238 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.00000 $0.02238
Opus 5 $0.00000 $0.01119
Sonnet 5 $0.00000 $0.00448
Haiku 4.5 $0.00000 $0.00224

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

Security

Grade A, and why

test-rule 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/commands/cursor-rules-initializer/sample/rules/testing/test-rule.mdc · 169 lines

How it starts

The opening of the file, as written. The whole thing — 169 lines — stays where its author put it; the contents beside it link to each section on GitHub.

テスト規則

テストファイルの命名規則

  • テストファイルは実装ファイルと同じディレクトリに配置する
  • ファイル名は {実装ファイル名}_test.go とする
  • 例: order_service.goorder_service_test.go

テスト関数の命名規則

  • テスト関数名は Test で始める
  • 構造体のメソッドをテストする場合: Test_{構造体名}_{メソッド名}
    • 例: Test_orderService_CreateOrder
  • パッケージ関数をテストする場合: Test{関数名}
    • 例: TestNewOrderState
  • 複数のテストケースを持つ場合は、t.Run() のサブテスト名に日本語で説明的な名前を付ける
    • 例: "正常系: 注文作成""異常系: 在庫不足"

テスト構造

  • テーブル駆動テスト(Table-Driven Tests)を推奨する
    • テストケースを構造体のスライスとして定義
    • 各テストケースに名前 (name)、入力 (args)、期待される出力 (want*)、期待されるエラー (wantErr) を含める
  • t.Run() を使用して各テストケースをサブテストとして実行する
  • 複雑なセットアップやアサーションはヘルパー関数に切り出す

テーブル駆動テストの例

func TestOrderService_CreateOrder(t *testing.T) {
    type fields struct { // モック化する依存関係
        orderRepo repository.OrderRepository
        itemRepo  repository.ItemRepository
    }
    type args struct { // テスト対象メソッドの引数
        ctx context.Context
        req *usecase.CreateOrderRequest
    }
    tests := []struct {
        name    string
        fields  fields // モックの期待動作設定用
        args    args
        want    *usecase.CreateOrderResponse
        wantErr bool // エラー発生を期待するかどうか
        // wantErrIs as error // 特定のエラー型を期待する場合 (errors.Is)
        // wantErrMsg string // 特定のエラーメッセージを期待する場合 (strings.Contains)
    }{
        {
            name: "正常系: 注文作成",
            fields: fields{
                // モックリポジトリのEXPECT設定
            },
            args: args{
                ctx: context.Background(),
                req: &usecase.CreateOrderRequest{ /* ... */ },
            },
            want: &usecase.CreateOrderResponse{ /* ... */ },
            wantErr: false,
        },
        // 他のテストケース...
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            ctrl := gomock.NewController(t)
            defer ctrl.Finish()

            // モックの準備 (tt.fields を使用)
            mockOrderRepo := mock_repository.NewMockOrderRepository(ctrl)
            // mockOrderRepo.EXPECT()...

            // テスト対象のサービス/メソッドを初期化
            s := &OrderService{
                orderRepo: mockOrderRepo,
                // ...
            }

            // テスト実行
            got, err := s.CreateOrder(tt.args.ctx, tt.args.req)

            // アサーション
            if (err != nil) != tt.wantErr {
                t.Errorf("CreateOrder() error = %v, wantErr %v", err, tt.wantErr)
                return
            }
            // if tt.wantErrIs != nil && !errors.Is(err, tt.wantErrIs) {
            //     t.Errorf("CreateOrder() error = %v, wantErrIs %v", err, tt.wantErrIs)
            // }
            // if tt.wantErrMsg != "" && !strings.Contains(err.Error(), tt.wantErrMsg) {
            //    t.Errorf("CreateOrder() error = %v, wantErrMsg %q", err, tt.wantErrMsg)
            //}

            // 戻り値の比較 (ポインタや複雑な構造体は go-cmp を推奨)
            if diff := cmp.Diff(tt.want, got, cmpopts.IgnoreUnexported(/* 必要に応じて */)); diff != "" {
                t.Errorf("CreateOrder() mismatch (-want +got):\n%s", diff)
            }
        })
    }
}

Read the full file on GitHub · 169 lines

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 · 169 lines · 2,238 tokens per session scan A ae8514ec5139

Subscribe to this mod's changes

test-rule is a cursor rule published in the GitHub repository showcase-gig-platform/cursor-rules-initializer (7 stars, last pushed 9mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 2,238 tokens. 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-31.