Imagine you built a toy machine that flips a sock inside-out. Instead of testing it with three socks you picked, your robot friend grabs every random sock in the house — tiny socks, giant socks, holey socks — and flips each one. You don't tell the robot what the result should look like; you tell it ONE rule: "if you flip a sock and flip it again, it must look exactly like it started." The robot tries hundreds of socks. When one breaks the rule, it doesn't hand you the messy giant sock — it keeps swapping for simpler socks until it finds the tiniest one that still breaks the rule, then shows you THAT. Now you know exactly what's wrong.
Dekho, normal testing mein hum khud 2-3 examples sochte hain: "agar input [1,2,3] hai to output [3,2,1] aana chahiye." Problem ye hai ki hum sirf wahi inputs test karte hain jo humein yaad aate hain. Jo edge cases hum bhool jaate hain (empty list, negative numbers, bahut bade numbers) — bug wahin chhupa hota hai. Property-based testing iska solution hai: tum ek rule (property) likhte ho jo har valid input ke liye sach hona chahiye, aur framework (jaise Python ka Hypothesis) sainkdon random inputs bana ke us rule ko todne ki koshish karta hai.
Rule kaise socho? Output ka exact value pata nahi hota, par relationship pata hota hai. Jaise: reverse(reverse(xs)) == xs — list ko do baar palto to wahi wapas aa jaayegi, chahe values kuch bhi ho. Ye RIIOM patterns yaad rakho — Round-trip (encode/decode wapas same), Idempotence (sort do baar = ek baar), Invariant (sort karne ke baad length same), Oracle (slow but correct version se compare), Metamorphic (input ka order change karo, output same). In paanch patterns se 80% real cases cover ho jaate hain.
Sabse mast feature hai shrinking. Maan lo framework ne fail karne wala input dhoondha [847,-3,901,0,-12] — itna ganda input dekh ke debug karna mushkil. To framework khud ise chhota karta jaata hai, har baar simpler input try karke jo abhi bhi fail kare, aur ant mein tumhe minimal example deta hai jaise [0,-1]. Isse bug seedha samajh aa jaata hai.
Ek important baat: PBT 100 cases pass kar gaya iska matlab code "proven correct" nahi hai — ye sampling hai, proof nahi. Lekin ye un bugs ko pakadta hai jo tum kabhi haath se na sochte. Isliye example tests + property tests dono saath use karo. Aur dhyan rakho generator sirf valid inputs banaye, warna false failures aayenge.