4.5.16 · HinglishSoftware Engineering

Mutation testing

1,653 words8 min readRead in English

4.5.16 · Coding › Software Engineering


WHY — yeh technique exist kyun karti hai?


WHAT — ek mutant exactly kya hota hai?


Figure — Mutation testing

HOW — process mechanically kaise chalta hai


Common mistakes (Steel-manned)


Forecast-then-Verify

Recall Aage padhne se pehle: predict karo ki yeh mutant survive karta hai ya nahi

Code: def max2(a,b): return a if a > b else b. Test: assert max2(3, 1) == 3. Mutant: a > ba < b. Survive hoga ya kill hoga?

Answer: max2(3,1) ke saath, mutant b = 1 ≠ 3 return karta hai → test fails → KILLED. Lekin mutant a > ba >= b is test par survive kar jaata (usse kill karne ke liye a == b case chahiye, jaise max2(5,5)).


Flashcards

Mutation testing code mein kya inject karta hai?
Chhoti deliberate bugs jisme mutants kehte hain, har ek ek mutation operator se banaya gaya hota hai.
Ek mutant "killed" kab hota hai?
Jab suite ka kam se kam ek test us mutant par fail ho jaye.
Ek mutant "survive" kab karta hai?
Jab inject ki gayi bug ke bawajood saare tests pass ho jaayein — test suite mein ek gap reveal hota hai.
Mutation score formula
= killed / (total mutants − equivalent mutants).
Denominator se equivalent mutants kyun subtract karte hain?
Woh original se identically behave karte hain, isliye koi test kabhi unhe kill nahi kar sakta; unhe count karna score ko unfairly lower kar dega.
Code coverage ki woh key limitation kya hai jo mutation testing fix karta hai?
Coverage prove karta hai ki ek line run hui, yeh nahi ki koi assertion us mein ek bug pakad paati.
Equivalent mutant kya hota hai?
Ek syntactic change jo original program ke identical behaviour produce karta hai; use kabhi kill nahi kiya ja sakta.
Ek surviving (non-equivalent) mutant tumhe kya karne ko kehta hai?
Ek test add karo ya strengthen karo — production code fix karne ki zaroorat nahi.
Ek mutation operator ka example do
>>=, +-, TrueFalse swap karo, ya koi statement delete karo.

Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho tumne ek smoke alarm banaya aur tum jaanna chahte ho ki woh sach mein kaam karta hai ya nahi. Tum bas usse dekhte nahi — tum uske neeche ek chhoti si maacha jalate ho yeh dekhne ke liye ki woh chillaata hai ya nahi. Mutation testing tumhare tests ke saath yahi karta hai: woh secretly tumhare program ko chhote-chhote tareekon se todo (chhote "bug matches" jalao) aur dekhta hai ki tumhare tests chillaate hain (fail hote hain) ya nahi. Agar tumhare tests tab bhi chup rahein jab program toota hua ho, tumhare tests bure smoke alarms hain aur unhe fix karna hoga.


Connections

  • Unit testing — mutation testing tumhari likhi unit tests ko grade karta hai.
  • Code coverage — woh weaker metric jise mutation testing complement/expose karta hai.
  • Test-driven development — TDD pehle tests likhta hai; mutation testing unki strength audit karta hai.
  • Boundary value analysis — bahut saare surviving mutants boundary bugs hote hain ( vs ).
  • Continuous integration — jahan mutation runs hota hai (aksar nightly, kyunki yeh slow hota hai).
  • Fault injection — "deliberately todo aur observe karo" techniques ki broader family.

Concept Map

only checks line executed

motivates

injects

applies tiny change to make

test fails

tests all pass

same behaviour

reveals

counted as K

excluded as E

equals K over M minus E

Code coverage

False sense of safety

Mutation testing

Mutants

Mutation operator

Killed mutant

Survived mutant

Equivalent mutant

Weak test suite

Mutation score

Suite quality