Overview
Core principle: Use LSP to precisely analyze the class structure, generate test cases with 100% function coverage, and enforce verification that the build succeeds.
Architecture Pattern: Skill routing + sub-agent full-stack execution. Sub-agents are responsible for all concrete work; the Skill is only responsible for invocation and result feedback.
Key Features: Intelligent CMake merging, incremental update support, strict build verification, detailed error handling.
Iron Laws
- Use Google Test only: The test framework is fixed to GTest; Qt Test is not supported
- 100% function coverage: Each public/protected function must have at least one test case
- Intelligent CMake merging: Optimize the merge according to the actual project situation to ensure generality
- Support incremental updates: Compare with existing tests and fill in uncovered functions
- Must verify the build: After generation, it must compile successfully before completion can be reported
- Compilation failures must be fixed: Retry each error at most 3 times, with a maximum of 10 loops
When to Use
Trigger Conditions:
- The user requests unit tests for a module:
为 src/lib/ui 模块创建单元测试 - The user requests tests for a class:
为 MyClass 生成测试 - The user requests test completion:
为 MyClass 补全测试
When not to use this skill:
- The user requests generating a test framework (use
qt-unittest-build) - The user requests fixing test failures (use
systematic-debugging) - The user requests analyzing test coverage (use other tools)
Execution Flow
Step 1: Check the test framework
Verify that autotests/CMakeLists.txt and autotests/3rdparty/stub/ exist. If they do not exist, prompt the user to first run qt-unittest-build。
Step 2: Analyze the class structure
Batch module generation: Glob-scan all .h/.hppin the directory and extract class names.
Single class: Directly analyze the specified class.
Use LSP tools:
lsp_document_symbols- Extract class structurelsp_goto_definition- Read function implementationslsp_find_references- Find dependencies
Step 3: Invoke sub-agents (MUST DO)
Important: Sub-agents must be invoked; do not skip this!
Sub-agent location: agent/unittest-generator.md
Invocation method:
Use the task tool to invoke sub-agents:
task(
description="生成单元测试代码",
prompt="完整的任务说明,包括:
- 目标模块或类
- 测试框架要求(Google Test)
- 函数覆盖率要求(100%)
- 验证构建要求",
subagent_type="general"
)
Why sub-agents are needed:
- Independent context: When generating tests for a single class or small module, an independent context avoids contamination
- Parallel execution: When batch-generating tests for multiple classes, multiple sub-agents can be forked to execute in parallel, improving efficiency
- Task isolation: A sub-agent failure does not affect the main Agent, making error handling easier
Invocation timing:
- Batch module generation: Invoke one sub-agent for each class (in parallel)
- Single class generation: Invoke one sub-agent
- Incremental update: Invoke a sub-agent to analyze the differences
Step 4: Wait for sub-agents to complete
Monitor the sub-agents' execution results:
- Collect the generated test files
- Check the verification build results
- Record any errors encountered (if any)
Step 5: Provide feedback to the user
Provide feedback to the user based on the sub-agents' results:
- If successful: report the generated test files and coverage
- If failed: report detailed error information and correction suggestions
Red Flags
- ❌ Use the Qt Test framework
- ❌ Coverage less than 100%
- ❌ Hardcoded CMake template
- ❌ Do not verify the build
- ❌ Do not support incremental updates
- ❌ Report completion even if compilation fails
- ❌ Skip sub-Agent invocation (work directly by hand)
Quick Reference
Test file naming: test_myclass.cpp
Test class naming: MyClassTest
Test case naming: {Feature}_{Scenario}_{ExpectedResult}
LSP tools: lsp_document_symbols, lsp_goto_definition, lsp_find_references
Stub mode: &Class::method, VADDR(Class, method), static_cast<...>
Compilation retry logic: retry each error up to 3 times, with a maximum of 10 loops
Sub-Agent invocation
The task tool must be used to invoke the sub-Agent:
task(
description="生成单元测试代码",
prompt="完整任务描述,包括目标、要求、验证流程",
subagent_type="general"
)
Sub-Agent execution content:
- Analyze the project structure (LSP)
- Generate test files (100% coverage)
- Intelligently merge CMake
- Verify the build (retry each error 3 times, with a maximum of 10 loops)
Why a sub-Agent is needed:
- Independent context to prevent contamination
- Parallel execution to improve efficiency
- Task isolation for easier error handling
Common errors
| Error | Cause | Fix |
|---|---|---|
| Test framework does not exist | qt-unittest-build was not run | Prompt the user to run the framework build skill first |
| Insufficient coverage | Not all functions were analyzed | Ensure lsp_document_symbols extracts completely |
| CMake merge failed | Hardcoded template | Use AI intelligent merging and optimize according to the actual project situation |
| Compilation failed | Stub signature error | Use LSP to obtain precise signatures |
| Report completion even if compilation fails | Skipped verification or verification was not rigorous | Compilation must succeed before reporting to the user |
| Multiple errors were not corrected | Global retry 3 times is insufficient | Retry each error 3 times, with a maximum of 10 loops |
Rationalization Table
| Excuse | Reality |
|---|---|
| "Working directly by hand is faster" | Manual work cannot guarantee 100% coverage and build verification; a sub-Agent is mandatory |
| "Skip sub-Agent invocation" | This violates the iron rule of the skill; the sub-Agent must be invoked |
| "Completion can also be reported if compilation fails" | This violates the mandatory build verification requirement; compilation must succeed before reporting |
| "Global retry 3 times is enough" | Retry each error 3 times, not 3 times globally |
| "80% coverage is enough" | The iron rule requires 100% function coverage, with no exceptions |
| "Qt Test is the same" | The skill uses Google Test exclusively and does not support Qt Test |