Writing Tests That Catch Real Bugs What a Test Is, and Which Ones Are Worth Writing
1 / 5
Next
What a Test Is, and Which Ones Are Worth Writing ~15min

A test is code that checks other code

function testTotalWithVat() {
    $result = calculateTotal(1000, 0.16);
    if ($result !== 1160.0) {
        throw new Exception("Expected 1160, got $result");
    }
}

That is the whole idea. Frameworks add convenience, not concept.

The real benefit is not finding today's bug

It is changing code in six months without fear. Without tests, every refactor is a gamble and you end up avoiding improvements you can see are needed.

Test these first

  • Money. Totals, discounts, VAT, commission
  • Access control. Can a non-admin reach the admin page?
  • Anything with awkward edge cases. Dates, phone formats, pagination
  • Every bug you fix. Write the test that would have caught it, so it cannot return

Do not bother testing

  • Getters that only return a property
  • Third-party library internals
  • Exact HTML markup. It changes constantly and the test breaks for no real reason

The honest cost

Tests are code you also maintain. Fifty brittle tests that break on every change get deleted or ignored. Ten tests around money and permissions get trusted, and that is the version worth having.

Tasks
Preview