在單元測試通過後觸發。透過引入人工錯誤(Mutants)來「測試你的測試」,確保測試案例具有足夠的錯誤偵測能力,建立對驗證機制的信任(Trust the Verification)。
在單元測試通過後,執行變異測試來「測試你的測試」。它會自動引入程式碼錯誤(如 `a+b` 改為 `a-b`),檢查現有測試能否偵測這些變異,從而找出測試盲點並提升驗證的可靠性。
/plugin marketplace add knowlet/skills/plugin install knowlet-skills@knowlet/skillsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
執行變異測試(Mutation Testing),量化測試套件的品質,找出「倖存的變異體(Surviving Mutants)」,並據此強化測試案例。
單元測試覆蓋率(Line Check)只能告訴你「程式碼被執行到了」,但無法告訴你「測試是否驗證了正確的行為」。
"Coverage only checks if the code is executed, Mutation Testing checks if the code is verified."
a + b 改為 a - b,或將 return true 改為 return false),產生一個「變異體 (Mutant)」。使用 PITest (PIT),這是目前 Java 生態系最成熟的變異測試工具。
<!-- pom.xml configuration example -->
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.15.0</version>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
<configuration>
<targetClasses>
<param>com.yourdomain.core.*</param>
</targetClasses>
<targetTests>
<param>com.yourdomain.core.*Test</param>
</targetTests>
<mutators>
<mutator>STRONGER</mutator> <!-- 使用更強的變異算子 -->
</mutators>
</configuration>
</plugin>
常見的變異類型包括:
i < 10 → i <= 10a + b → a - bi++ → i---i → ireturn true → return false / return object → return null1. 執行單元測試 (必須全數通過)
↓
2. 執行變異測試 (mvn pitest:mutationCoverage)
↓
3. 產生報告 (target/pit-reports/index.html)
↓
4. 分析倖存變異體 (Analyze Surviving Mutants)
↓
5. 強化測試案例 (Add/Refine Test Cases)
↓
6. 重複直到 Mutation Score 達標
| 指標 | 建議閾值 | 說明 |
|---|---|---|
| Line Coverage | > 85% | 基礎要求 |
| Mutation Score | > 80% | 殺死的變異體 / 總變異體 |
| Test Strength | > 80% | 只計算有被覆蓋到的程式碼的變異分數 |
測試執行了代碼,但沒有檢查結果。
assertEquals 或 verify。只檢查了部分狀態(例如只檢查 List 不為空,沒檢查內容)。
變異後的代碼在邏輯上與原代碼等價(例如 i < 10 在迴圈中改成 i != 10)。
執行此 Skill 後,應產出:
注意:變異測試極其耗時。建議只針對核心 Domain Logic (Entities, Value Objects, Domain Services) 執行,避免對整個專案執行。
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.