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 เทสของเราก็น่าจะพอ

Image of DataStax

AI Agents Made Easy with Langflow

Connect models, vector stores, memory and other AI building blocks with the click of a button to build and deploy AI-powered agents.

Get started for free

Top comments (0)

Image of DataStax

Langflow: Simplify AI Agent Building

Langflow is the easiest way to build and deploy AI-powered agents. Try it out for yourself and see why.

Get started for free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay