All courses

What makes a test able to fail

Explain a test as inputs plus an oracle, the expected result. The oracle must come from the specification rather than from the code under test. A test that cannot go red when the code is wrong proves nothing.

Read it card by card Start the whole course

Nine green tests. Do they prove anything?

You asked the agent to add tests for the shipping-fee function. It reports: "Added 9 tests, all green." The diff is full of `assert` lines with confident numbers. Before you approve, ask one thing: could any of these tests catch a bug? Green alone doesn't answer that. A test that would also be green with broken code tells you nothing.

A test is inputs plus an oracle

Every test has two parts: inputs, and an **oracle**, the expected result it compares against. A test is only as good as where its oracle came from. The agent writes tests while looking at the code. The easiest oracle is whatever the code already does: copy its formula, or run it and paste the output. That reliably turns the tests green, which is a proxy signal. **Later cost:** the bug is now locked in as "expected", and the correct fix later turns a test red.

Two questions for every assertion

1. **Where did the expected value come from?** Good sources are the spec or ticket, a worked example, a hand calculation, or a trusted reference. Bad sources are calling the code under test, copying its formula or constants, and pasting its output. 2. **Could it go red?** Picture a plausible bug, such as a wrong boundary or a swapped unit. If the test would stay green, it proves nothing. Also weak: asserting only "no error" or "not null".

Same formula, same bug

The spec says shipping is free for orders of 50 or more. The code uses `>`, so an order of exactly 50 pays 5. The BAD test computes its oracle with the same `t > 50` expression. It agrees with the bug and stays green. The BETTER test takes `0` straight from the spec at the boundary. It goes red against this code and exposes the bug. The `49` case pins down the other side of the boundary.
// Python
# spec: free shipping for orders >= 50
def fee(total):
    return 0 if total > 50 else 5

# BAD
def test_fee():
    t = 50
    assert fee(t) == (0 if t > 50 else 5)

# BETTER
def test_fee_at_threshold():
    assert fee(50) == 0
    assert fee(49) == 5

An expected value pasted from a run

The agent tests `parseDuration`. Its transcript shows it ran `node -e` on `parseDuration('1h30m')`, which printed `3630`. The test it then wrote is `expect(parseDuration('1h30m')).toBe(3630)`. **Tell:** the expected value came from running the code itself. By the spec, 1h30m is 5400 seconds; the code reads the minutes as seconds. **Good version:** `toBe(5400)`, worked out from the spec. It goes red and reveals the bug. Ask the agent where each expected value came from.

Remember

A test is inputs plus an oracle. Take the oracle from the spec, never from the code under test. Then ask of every test: would it go red if the code were wrong? If not, it proves nothing.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.