Qt Unit Test Generator
Overview
Core principle: Precisely analyze class structures using LSP, generate test cases with 100% function coverage, and enforce build success verification.
Architecture Pattern: Skill routing + full-stack execution by sub-agents. Sub-agents handle all specific tasks, while the Skill only takes charge of invocation and result feedback.
Key Features: Smart CMake merging, incremental update support, strict build verification, detailed error handling.
Iron Laws
- Google Test Only: The testing framework is fixed to GTest, Qt Test is not supported
- 100% Function Coverage: Each public/protected function must have at least one test case
- Smart CMake Merging: Optimize merging based on actual project conditions to ensure universality
- Incremental Update Support: Compare with existing tests to complete uncovered functions
- Mandatory Build Verification: Must compile successfully before reporting completion
- Fix Compilation Failures: Retry each error up to 3 times, with a maximum of 10 cycles
When to Use
Trigger Conditions:
- User requests to generate unit tests for a module:
Create unit tests for the src/lib/ui module
- User requests to generate tests for a class:
Generate tests for MyClass
- User requests to complete tests:
Complete tests for MyClass
When Not to Use This Skill:
- User requests to generate a testing framework (use )
- User requests to fix test failures (use )
- User requests to analyze test coverage (use other tools)
Execution Flow
Step 1: Check Testing Framework
Verify that
and
exist. If not, prompt the user to run
first.
Step 2: Analyze Class Structure
Batch Generation for Modules: Use glob to scan all
files in the directory and extract class names.
Single Class: Directly analyze the specified class.
Use LSP tools:
- - Extract class structures
- - Read function implementations
- - Find dependencies
Step 3: Invoke Sub-Agent (MUST DO)
Important: Must invoke the sub-agent, cannot skip this step!
Sub-Agent Location:
agent/unittest-generator.md
Invocation Method:
Use the
tool to invoke the sub-agent:
task(
description="Generate unit test code",
prompt="Complete task description including:
- Target module or class
- Testing framework requirement (Google Test)
- Function coverage requirement (100%)
- Build verification requirement",
subagent_type="general"
)
Why Sub-Agents Are Needed:
- Independent Context: Avoids context pollution when generating tests for a single class or small module
- Parallel Execution: Fork multiple sub-agents to execute in parallel when generating tests for multiple classes in batches, improving efficiency
- Task Isolation: Sub-agent failures do not affect the main agent, facilitating error handling
Invocation Timing:
- Batch Module Generation: Invoke one sub-agent per class (parallel execution)
- Single Class Generation: Invoke one sub-agent
- Incremental Update: Invoke sub-agent to analyze differences
Step 4: Wait for Sub-Agent Completion
Monitor the sub-agent's execution results:
- Collect generated test files
- Check build verification results
- Record any errors encountered
Step 5: Provide Feedback to User
Provide feedback to the user based on the sub-agent's results:
- If successful: Report generated test files and coverage
- If failed: Report detailed error information and correction suggestions
Red Flags
- ❌ Use Qt Test framework
- ❌ Coverage less than 100%
- ❌ Hard-coded CMake templates
- ❌ No build verification
- ❌ No incremental update support
- ❌ Report completion even if compilation fails
- ❌ Skip sub-agent invocation (manual work directly)
Quick Reference
Test Case Naming:
{Feature}_{Scenario}_{ExpectedResult}
Compilation Retry Logic: Retry each error up to 3 times, with a maximum of 10 cycles
Sub-Agent Invocation
Must Use the Tool to Invoke Sub-Agents:
task(
description="Generate unit test code",
prompt="Complete task description including target, requirements, and verification process",
subagent_type="general"
)
Sub-Agent Execution Content:
- Analyze project structure (LSP)
- Generate test files (100% coverage)
- Smart CMake merging
- Verify build (retry each error up to 3 times, maximum 10 cycles)
Why Sub-Agents Are Needed:
- Independent context to prevent pollution
- Parallel execution to improve efficiency
- Task isolation to facilitate error handling
Common Errors
| Error | Cause | Fix |
|---|
| Testing framework not found | not run | Prompt user to run the framework build skill first |
| Insufficient coverage | All functions not analyzed | Ensure extracts complete information |
| CMake merging failed | Hard-coded templates | Use AI for smart merging, optimize based on actual project conditions |
| Compilation failed | Incorrect stub signature | Use LSP to obtain precise signatures |
| Report completion despite compilation failure | Verification skipped or not rigorous | Must only report to user after successful compilation |
| Multiple errors not fixed | Global 3 retries insufficient | Retry each error up to 3 times, not 3 global retries |
Rationalization Table
| Excuse | Reality |
|---|
| "Manual work is faster directly" | Manual work cannot guarantee 100% coverage and build verification; sub-agents are mandatory |
| "Skip sub-agent invocation" | Violates the skill's iron laws; sub-agent invocation is mandatory |
| "Can report completion even if compilation fails" | Violates mandatory build verification requirements; must only report after successful compilation |
| "3 global retries are enough" | Retry each error up to 3 times, not 3 global retries |
| "80% coverage is sufficient" | Iron law requires 100% function coverage, no exceptions |
| "Qt Test is the same" | The skill is fixed to use Google Test; Qt Test is not supported |