DEV Community

Kan Ouivirach
Kan Ouivirach

Posted on • Edited on

3 3

Mutation Testing อีกทางหนึ่งในการวัดคุณภาพของการทดสอบ

เพิ่งรู้จัก Mutation Testing จากบทความ A demonstration of Mutation Testing

เค้าบอกว่าปกติแล้วเราจะใช้ test coverage เป็น metric ในการวัด quality ของเทสของเรา แต่ทีนี้มันเหมือนโกงได้ คือจริงๆ มันประมาณว่าถ้าเทสเรารันผ่านโค้ดทุกส่วน โดยเทสไม่ต้อง pass หรือ fail ก็ได้ มันก็ได้ 100% coverage แล้ว (เช่น เขียนเทส แต่ไม่เขียน assertion เลย) ทีนี้ก็เลยมีอีกทางหนึ่งนอกจาก coverage แล้วเนี่ย เรายังทำ mutation testing เพิ่มได้อีก

ไอเดียคือ… เราจะใช้ tool เพื่อใส่บั๊ก 🐛 หรือ mutants เข้าไปในโค้ดของเรา เพื่อให้เทส fail

  • ถ้าเทสเรา fail แปลว่าเราฆ่า mutant นั้นได้
  • ถ้าเทสเรา pass แปลว่า mutant นั้นยังมีชีวิตอยู่

เป้าหมายของเราคือฆ่า mutant ให้ได้เยอะที่สุด % ที่ mutant ตายยิ่งมาก ก็หมายความว่าเทสของเราก็ยิ่งมีประสิทธิภาพมากขึ้นด้วย 💪

ลองดูตัวอย่างการที่ tool ใส่ mutant เข้าไปในโค้ดเราได้ที่ Stryker Mutator: An example

หมายเหตุ ข้อเสียมันคือใช้เวลารันนาน… แหงล่ะ เล่นใส่ mutant มาในโค้ดแล้วก็รันเทสทดสอบทีละ mutant แบบนั้น ดังนั้นเราก็ไม่ควรใส่ไว้ใน build pipeline นะ เอาไว้เป็น tool สำหรับ review เทสของเราก็น่าจะพอ

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more