Part 2: Testing

Letting the computer double-check your work.

Why bother?

“The project is late.”

“It works, don’t touch it.”

“My code is too complicated to test.”

“I’m the only one working on this.”

Untested code isn’t bug-free

It’s evidence-free.

What a unit test looks like

_images/p2-given-when-then.svg

The assertion is the test

_images/p2-no-assert.svg

Habits that keep tests honest

Do

Avoid

build test data inside the test

reading it from external files

use a temporary folder if you must touch disk

real network or API calls

include an input that should not pass

mocks, for now

test your own logic

testing third-party libraries

Refactoring

_images/p2-refactor.svg

Continuous integration

_images/p2-ci-gate.svg

Two rules that make CI mean something

  • nobody pushes to main directly

  • the only way in is a merge request, merged only when the checks are green

Test-driven development

_images/p2-tdd-cycle.svg

…but it isn’t free

Works best on new code, where you already know what you want.

Over to you

Write tests, refactor behind them, then build a feature test-first.

Part 2 tasks →