Property-based testing

속성 기반 테스트

Generate many inputs and check an invariant that should hold for all of them.

···
html
<div class="stage"><div class="title">PROPERTY TEST</div><div class="canvas"><div class="rule">property: normalize(x) ≥ 0</div><div class="samples" id="samples"></div><div class="counter" id="counter">checking generated inputs</div></div></div>
css
.stage{width:min(94vw,820px);height:min(88vh,330px);box-sizing:border-box;padding:clamp(9px,2.4vmin,18px);border:1px solid var(--line);border-radius:13px;background:var(--surface);display:flex;flex-direction:column;gap:clamp(7px,2vmin,14px);font:600 clamp(12px,2.5vmin,16px)/1.28 "Pretendard Variable",Pretendard,-apple-system,BlinkMacSystemFont,"Apple SD Gothic Neo",sans-serif;color:var(--fg);overflow:hidden}.title{color:var(--accent);letter-spacing:.035em;flex:none}.canvas{position:relative;flex:1;min-height:0;display:flex;align-items:center;justify-content:center}.muted{color:var(--muted)}.mono{font-family:ui-monospace,SFMono-Regular,monospace}.box{border:1px solid var(--line);border-radius:8px;background:var(--bg);padding:clamp(5px,1.4vmin,12px);text-align:center}.active{border-color:var(--accent)!important;color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 12%,var(--surface))!important}.canvas{flex-direction:column;gap:clamp(8px,3vmin,20px)}.rule{border:1px solid var(--accent);border-radius:6px;padding:6px 10px;color:var(--accent)}.samples{display:flex;gap:clamp(3px,1.5vmin,10px);justify-content:center;flex-wrap:wrap;width:100%}.sample{min-width:24px;padding:7px 3px;border:1px solid var(--line);border-radius:5px;text-align:center;font-family:ui-monospace,monospace}.sample.bad{border-color:var(--accent-3);color:var(--accent-3)}.counter{color:var(--muted)}
js
const values=[4,-2,7,0,-3,11,-8];let n=0;function run(){const list=document.getElementById('samples');list.innerHTML='';for(let i=0;i<7;i++){const x=values[(i+n)%values.length],bad=x===-3;const el=document.createElement('span');el.className='sample'+(bad?' bad':'');el.textContent=x;list.appendChild(el)}document.getElementById('counter').textContent='normalize(-3) = -1 · FAIL';n++}run();setInterval(run,1300)

Property-based testing defines an input generator and an invariant instead of selecting only a few examples. Shrinking a failing input to a smaller counterexample makes the bug easier to understand.

The demo streams generated numbers through a property check and isolates a failure. Many runs do not help if the generator or invariant misses important cases.

When to use

Use it for invariants in sorting, serialization, parsers, and similar transformations.

Open as page ↗