4.5.17 · HinglishSoftware Engineering

Property-based testing

1,674 words8 min readRead in English

4.5.17 · Coding › Software Engineering


YE HAI KYA

Key vocabulary:

  • Property ::= ek boolean rule jo har valid input ke liye true hai.
  • Generator ::= woh code jo random valid inputs produce karta hai (integers(), lists(), text()).
  • Shrinking ::= ek failing input milne ke baad, framework use minimal example tak simplify karta hai jo phir bhi fail karta hai.

KYUN: example tests vs property tests


ACCHI properties kaise dhundhen

Tumhe exact output hamesha pata nahi hota, lekin tum usually ek relationship jaante ho. Common patterns:


SHRINKING kaise kaam karta hai (killer feature)

Figure — Property-based testing

Shrink loop ka mental model:

  1. Failing input mila.
  2. Ek "simpler" candidate propose karo (ek element drop karo, ek number halve karo).
  3. Agar phir bhi fail kare → use rakho, recurse karo. Agar woh pass kare → discard karo, doosra simplification try karo.
  4. Ruk jao jab koi simpler candidate fail na kare. Woh minimum report karo.

Yeh sabse simple failing case ki taraf ek greedy descent hai.


Worked example: ek buggy average

def average(xs):
    return sum(xs) / len(xs)
 
@given(lists(integers()))
def test_average_within_bounds(xs):
    avg = average(xs)
    assert min(xs) <= avg <= max(xs)

Common mistakes (Steel-manned)


The 80/20


Recall Feynman: 12-saal ke bachche ko explain karo

Socho tumne ek toy machine banayi jo ek moje ko andar-bahar kar deti hai. Apne chune hue teen mojon se test karne ki jagah, tumhara robot dost ghar ke har random moje ko pakad leta hai — chhote moje, bade moje, phaate moje — aur har ek ko palat deta hai. Tum robot ko nahi batate ki result kaisa dikhna chahiye; tum use ek rule batate ho: "agar tum moja palto aur phir palto, toh exactly waisa hi dikhna chahiye jaise pehle tha." Robot saikdon mojon ko try karta hai. Jab koi rule todta hai, woh tumhe messy bada moja nahi deta — woh simpler mojon ke saath swap karta rehta hai jab tak sabse chhota moja nahi milta jo phir bhi rule todta ho, phir wahi dikhata hai. Ab tumhe exactly pata hai kya galat hai.


Flashcards

Property-based test kya assert karta hai, example-based test ke unlike?
Ek rule/invariant jo SABHI valid inputs ke liye true hai, na ki ek single hardcoded input→output pair.
Property-based testing mein "shrinking" define karo.
Failing input milne ke baad, use automatically simplify karna sabse chhote/simple input tak jo phir bhi fail kare, easy debugging ke liye.
RIIOM property patterns ke naam batao.
Round-trip, Idempotence, Invariant, Oracle, Metamorphic.
Round-trip property formula do.
g(f(x)) == x for all x, jahan g, f ka inverse hai (e.g. decode(encode(x))).
Idempotence property likho.
f(f(x)) == f(x) (e.g. sort, normalize, dedupe).
@given ke andar exact expected output assert kyun nahi karte?
Random inputs ke liye answer jaanne ke liye tumhe function reimplement karna padega, uske bugs copy ho jaate hain; uski jagah ek relationship assert karo.
"Generator/strategy" kya hota hai?
Woh code jo diye gaye type ke random VALID inputs produce karta hai (integers, lists, text).
1000 random cases pass hona correctness ka proof hai?
Nahi — yeh ek sampling hai jo confidence badhata hai aur bugs dhundta hai, exhaustive proof nahi.
Sort ke liye ek invariant property?
len(sort(xs)) == len(xs) aur sorted output input ka ek permutation hai.
"Oracle" pattern kya hai?
Fast implementation ko ek simple trusted (slow) reference ke against compare karo: f_fast(x) == f_slow(x).
PBT runs har input ke liye deterministic kyun hone chahiye?
Taaki failures reproduce hon; hidden randomness/global state flaky, non-reproducible failures cause karta hai.

Connections

  • Unit testing — PBT example tests ko complement karta hai, replace nahi karta.
  • Test-driven development — properties design drive kar sakti hain.
  • Invariants and contracts — properties runtime-checkable invariants HAIN.
  • Fuzzing — random input generation ka cousin (PBT = fuzzing + structured assertions + shrinking).
  • Hypothesis (Python) / QuickCheck (Haskell) — original PBT tools.
  • Pure functions — property-test karne ki sabse aasaan cheezein (deterministic, no side effects).

Concept Map

too few inputs, misses

contrasts with

you specify

framework runs

feeds hundreds of inputs to

when it fails

reports

provides

performs

help you write

derived from

Property-Based Testing

Example-Based Tests

Property: rule true for all inputs

Generator: random valid inputs

Shrinking: minimal counterexample

Edge-case bugs

RIIOM Property Patterns

Framework e.g. Hypothesis

Algebraic laws