generate-test-cases

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Generate Test Cases Skill

Generate Test Cases 技能

You will analyze code and generate a list of test cases that should be written for a given method/class. This skill outputs test case descriptions only — it does NOT generate actual test code.
Target to analyze: $ARGUMENTS
你将分析代码并生成针对给定方法/类应编写的测试用例列表。本技能仅输出测试用例描述——不生成实际测试代码
分析目标: $ARGUMENTS

Quality Standards

质量标准

  • Take your time to analyze the code thoroughly before listing test cases.
  • Quality is more important than speed — read all relevant source files and rules carefully.
  • Do not skip reading the dependency classes. Understanding the full context produces better test cases.

  • 列出测试用例前,请花时间彻底分析代码。
  • 质量比速度更重要——请仔细阅读所有相关源文件和规则。
  • 不要跳过依赖类的阅读。了解完整上下文才能生成更优质的测试用例。

Instructions

操作步骤

Step 1: Read Rules and Analyze Context

步骤1:阅读规则并分析上下文

  1. Read the rules from
    ./rules/general/
    directory (see Rules Reference below)
  2. Read the target source file/class/method specified above
  3. Read dependencies: Follow imports to read DTOs, entities, enums, and other types referenced by the target (as specified in
    code-context-analysis
    rule)
  4. Check for existing tests: Search for existing test classes covering this target (as specified in
    existing-test-awareness
    rule) — if found, read it fully and focus only on behaviors not yet covered
  1. 阅读规则:从
    ./rules/general/
    目录读取规则(见下方规则参考)
  2. 阅读目标对象:读取上方指定的源文件/类/方法
  3. 阅读依赖项:根据导入关系读取目标对象引用的DTO、实体、枚举及其他类型(遵循
    code-context-analysis
    规则)
  4. 检查现有测试:搜索覆盖该目标对象的现有测试类(遵循
    existing-test-awareness
    规则)——如果找到,请完整阅读并仅关注未覆盖的行为

Step 2: Generate Test Cases

步骤2:生成测试用例

  1. Analyze ALL code branches, including:
    • Success paths
    • Error/exception paths
    • Validation logic
    • Private/protected methods called by the target
    • Security annotations (if present)
  2. Apply the INCLUDE/EXCLUDE rules strictly
  3. Output the list of test cases in the specified format
  4. Do NOT generate actual test code — only the test case descriptions

  1. 分析所有代码分支,包括:
    • 成功路径
    • 错误/异常路径
    • 验证逻辑
    • 目标对象调用的私有/受保护方法
    • 安全注解(若存在)
  2. 严格遵循INCLUDE/EXCLUDE规则
  3. 按指定格式输出测试用例列表
  4. 不生成实际测试代码——仅输出测试用例描述

Output Format

输出格式

For each test case, provide:
undefined
每个测试用例需包含:
undefined

Test Cases for {ClassName}.{methodName}

Test Cases for {ClassName}.{methodName}

1. {testMethodName}

1. {testMethodName}

  • Given: {preconditions/input state}
  • When: {action being tested}
  • Then: {expected outcome}
  • Code branch: {which code path this covers}
  • Given: {preconditions/input state}
  • When: {action being tested}
  • Then: {expected outcome}
  • Code branch: {which code path this covers}

2. {testMethodName}

2. {testMethodName}

...
undefined
...
undefined

Naming Convention

命名规范

Test method name format:
{testedMethod}_{givenState}_{expectedOutcome}
Examples:
  • calculateTotal_validProducts_returnsSum
  • calculateTotal_emptyList_throwsIllegalArgumentException
  • getUser_unauthorized_returns401
  • getUser_forbidden_returns403

测试方法命名格式:
{testedMethod}_{givenState}_{expectedOutcome}
示例:
  • calculateTotal_validProducts_returnsSum
  • calculateTotal_emptyList_throwsIllegalArgumentException
  • getUser_unauthorized_returns401
  • getUser_forbidden_returns403

Troubleshooting

问题排查

Target file not found

目标文件未找到

If the specified target does not exist, inform the user with the exact path you searched and ask for clarification.
如果指定的目标对象不存在,请告知用户你搜索的准确路径并请求澄清。

Unsupported language

不支持的语言

If the target code is in a language without specific rules, apply only the general rules and inform the user.
如果目标代码使用的语言没有特定规则,仅应用通用规则并告知用户。

All behaviors already covered

所有行为已被覆盖

If the existing test class already covers all identified behaviors, output a summary stating that coverage is complete. List what is already tested. Do not invent additional test cases to justify the analysis.

如果现有测试类已覆盖所有识别到的行为,输出总结说明覆盖率已完成,并列出已测试的内容。请勿为了完成分析而编造额外测试用例。

Example

示例

User says: "/generate-test-cases src/main/java/com/example/service/OrderService.java"

Step 1: Agent reads rules, reads OrderService.java, reads OrderRequest.java,
        Order.java (dependencies), checks for existing OrderServiceTest.java.

Step 2: Agent outputs:
User says: "/generate-test-cases src/main/java/com/example/service/OrderService.java"

Step 1: Agent reads rules, reads OrderService.java, reads OrderRequest.java,
        Order.java (dependencies), checks for existing OrderServiceTest.java.

Step 2: Agent outputs:

Test Cases for OrderService.createOrder

Test Cases for OrderService.createOrder

1. createOrder_validRequest_savesAndReturnsOrder

1. createOrder_validRequest_savesAndReturnsOrder

  • Given: Valid OrderRequest with productId "product-1" and quantity 5
  • When: createOrder is called
  • Then: Order is saved to repository and returned with generated ID
  • Code branch: Success path
  • Given: Valid OrderRequest with productId "product-1" and quantity 5
  • When: createOrder is called
  • Then: Order is saved to repository and returned with generated ID
  • Code branch: Success path

2. createOrder_nullProductId_throwsIllegalArgumentException

2. createOrder_nullProductId_throwsIllegalArgumentException

  • Given: OrderRequest with null productId
  • When: createOrder is called
  • Then: IllegalArgumentException is thrown
  • Code branch: Validation — productId null check ...

---
  • Given: OrderRequest with null productId
  • When: createOrder is called
  • Then: IllegalArgumentException is thrown
  • Code branch: Validation — productId null check ...

---

Rules Reference

规则参考

CRITICAL: You MUST read and apply all rules from the following files before generating test cases:
Maintenance note: General rules in
./rules/general/
are shared with the
generate-tests
skill (which has copies in
rules/tests/general/
). When updating rules, keep both locations in sync.
重要提示:生成测试用例前,你必须阅读并应用以下文件中的所有规则:
维护说明:
./rules/general/
中的通用规则与
generate-tests
技能共享(该技能的规则副本位于
rules/tests/general/
)。更新规则时,请保持两个位置的内容同步。

General Rules (Always Apply)

通用规则(始终适用)

  • ./rules/general/test-case-generation-strategy.md
    - INCLUDE/EXCLUDE criteria for test cases
  • ./rules/general/naming-conventions.md
    - Test naming format
  • ./rules/general/general-principles.md
    - Core testing principles
  • ./rules/general/what-makes-good-test.md
    - Clarity, Completeness, Conciseness, Resilience
  • ./rules/general/keep-tests-focused.md
    - One scenario per test
  • ./rules/general/test-behaviors-not-methods.md
    - Separate tests for behaviors
  • ./rules/general/prefer-public-apis.md
    - Test public APIs over private methods
  • ./rules/general/cleanly-create-test-data.md
    - Use helpers and builders for test data
  • ./rules/general/keep-cause-effect-clear.md
    - Effects follow causes immediately
  • ./rules/general/no-logic-in-tests.md
    - KISS > DRY, avoid logic in assertions
  • ./rules/general/technology-stack-detection.md
    - Detect language and framework
  • ./rules/general/verify-relevant-arguments-only.md
    - Only verify relevant mock arguments
  • ./rules/general/existing-test-awareness.md
    - Check for existing tests, avoid duplicates
  • ./rules/general/code-context-analysis.md
    - Read dependencies before analyzing
  • ./rules/general/test-case-generation-strategy.md
    - 测试用例的INCLUDE/EXCLUDE标准
  • ./rules/general/naming-conventions.md
    - 测试命名格式
  • ./rules/general/general-principles.md
    - 核心测试原则
  • ./rules/general/what-makes-good-test.md
    - 清晰性、完整性、简洁性、韧性
  • ./rules/general/keep-tests-focused.md
    - 每个测试对应一个场景
  • ./rules/general/test-behaviors-not-methods.md
    - 为不同行为分别编写测试
  • ./rules/general/prefer-public-apis.md
    - 优先测试公开API而非私有方法
  • ./rules/general/cleanly-create-test-data.md
    - 使用辅助工具和构建器创建测试数据
  • ./rules/general/keep-cause-effect-clear.md
    - 确保因果关系直接明确
  • ./rules/general/no-logic-in-tests.md
    - 遵循KISS原则而非DRY原则,避免在断言中加入逻辑
  • ./rules/general/technology-stack-detection.md
    - 检测语言与框架
  • ./rules/general/verify-relevant-arguments-only.md
    - 仅验证相关的Mock参数
  • ./rules/general/existing-test-awareness.md
    - 检查现有测试,避免重复
  • ./rules/general/code-context-analysis.md
    - 分析前先阅读依赖项