Mutation Testing เป็นเทคนิควัดคุณภาพ Test Suite โดยสร้าง "Mutants" (โค้ดที่เปลี่ยนเล็กน้อย) แล้วดูว่า Test จับข้อผิดพลาดได้หรือไม่
VisualMutator
เครื่องมือ Mutation Testing ยอดนิยมสำหรับ .NET ที่ผสานกับ Visual Studio
คุณสมบัติหลัก
- 📊 Mutation Score — แสดงเปอร์เซ็นต์ Mutants ที่ Test จับได้ (Kill rate)
- 🔧 Built-in & Custom Mutators — สร้าง Mutants แบบ First-order (เปลี่ยน 1 จุด) ได้ทั้ง Built-in และ Custom
- 👁️ View Modified Code — ดูโค้ดที่ถูก Mutate แบบ Inline ใน Editor
- 🧪 NUnit & xUnit Support — รัน Test บน Mutants ได้ทั้งสอง Framework
- 📋 Test Results Detail — แสดง Passed/Failed tests ต่อ Mutant แต่ละตัว
- 📄 Export XML — บันทึกผลลัพธ์เพื่อนำไปวิเคราะห์ต่อ หรือ CI/CD
- ⚡ Real-time Feedback — ดูรายละเอียด Mutant ทันทีหลังเริ่ม Mutation Testing
การติดตั้งและใช้งาน
- ติดตั้ง Extension "VisualMutator" ใน Visual Studio Marketplace
- คลิกขวา Project → Run Mutation Testing
- รอการวิเคราะห์ และดูผลลัพธ์ใน Tool Window
ตัวอย่าง Mutations
| Original | Mutated | Mutator Type |
|---|---|---|
if (x > 0) | if (x < 0) | Relational Operator Replacement |
return a + b; | return a - b; | Arithmetic Operator Replacement |
for (i=0; i<n; i++) | for (i=0; i<n; i--) | Loop Variable Mutation |
x = true; | x = false; | Boolean Constant Replacement |
เครื่องมืออื่นๆ
- Stryker.NET — Open source, รองรับ .NET Core, CI/CD friendly
- NCrunch — Commercial, มี Mutation Testing built-in
- Foxtrot — Research tool สำหรับ Mutation Testing
คำแนะนำ
Mutation Testing ใช้เวลานาน (รัน Test หลายรอบ) เหมาะสำหรับรันใน CI Pipeline แทน Local development ปกติ