Testing and Git · Part 16 of 36
Automated tests and test-driven development
Intermediate 16 min
Unit, integration and end-to-end tests, assertions, test runners in JavaScript and Python, and the red–green–refactor cycle.
What you will learn
- You can write unit tests with node:test and pytest.
- You can explain the test pyramid and what each level catches.
- You can work in the red–green–refactor cycle.
Before this: Functions: name a piece of work
Why automate tests?
Every time you change code you risk breaking something that used to work — a regression. Checking everything by hand after each change is slow and you will skip it. An automated test is code that runs your code with known inputs and checks the outputs. A whole suite runs in seconds, every time, without getting bored.

A first unit test
A unit test checks one small piece — usually one function — in isolation.
// area.test.js — run with: node --test
import { test } from "node:test";
import assert from "node:assert/strict";
import { areaOfBed } from "./garden.js";
test("area is length times width", () => {
assert.equal(areaOfBed(3, 1.2), 3.6);
});
test("rejects negative sizes", () => {
assert.throws(() => areaOfBed(-1, 2), RangeError);
});
# test_garden.py — run with: pytest
import pytest
from garden import area_of_bed
def test_area_is_length_times_width():
assert area_of_bed(3, 1.2) == pytest.approx(3.6)
def test_rejects_negative_sizes():
with pytest.raises(ValueError):
area_of_bed(-1, 2)
Node.js has had a built-in test runner (node:test) since version 18, stable since 20; Python's ecosystem mostly uses pytest (the standard library also has unittest). Notice pytest.approx — floating point again.
What to test
Good tests cover partitions of the input, not random values: a typical value, the boundaries (0, 1, the maximum, an empty list), and invalid input. Each test has a descriptive name, checks one behaviour and does not depend on other tests or on the order they run in.
The test pyramid
- Unit tests — many, fast, precise: they tell you exactly which function broke.
- Integration tests — fewer: do the parts work together (your code plus a real database or file system)?
- End-to-end tests — a few: a script drives the real app through a browser (tools like Playwright) as a user would.
Higher levels catch more realistic problems but are slower and harder to debug. Most of your tests should be at the bottom.
Test doubles
To test a unit in isolation, you replace slow or unpredictable collaborators — the network, the clock, randomness — with test doubles: a stub returns fixed answers, a mock also records how it was called. Code designed with composition (lesson "Object-oriented programming") makes this easy: pass the clock in instead of calling Date.now() inside.
Test-driven development (TDD)
TDD flips the order:
- Red: write a test for the next small behaviour; run it and watch it fail.
- Green: write the simplest code that makes it pass.
- Refactor: clean up the code while all tests stay green.
Watching the test fail first proves the test can actually detect the problem. TDD also pushes you towards small, decoupled functions — code that is easy to test is usually well designed.
Coverage is a hint, not a goal
Coverage tools report which lines ran during the tests. Low coverage shows untested areas; 100 % coverage does not prove correctness — a line can run without its result being checked.
Try it: JavaScript sandbox
Write and run small JavaScript programs; the checker tells you when an exercise is solved.
Turn on JavaScript to use this lab. Worked example:
Exercise 12: the function isLeapYear(y) has a bug for years divisible by 400. Write the missing rule so that all checker tests pass: 2024 true, 1900 false, 2000 true, 2026 false.
Key points
- Automated tests catch regressions every time you change code.
- Unit tests check one function; use
node:test+assertorpytest. - Test boundaries, typical values and invalid input; one behaviour per test.
- Test pyramid: many unit, fewer integration, few end-to-end tests.
- TDD: red → green → refactor; coverage is a hint, not proof.
Flashcards
- Question: What is a regression?
- Answer: Something that used to work and is broken by a later change.
- Question: What are the three steps of TDD?
- Answer: Red (failing test), green (make it pass), refactor (clean up).
- Question: Why compare floats with pytest.approx?
- Answer: Floating-point results carry tiny rounding errors.
- Question: What does a stub do?
- Answer: Replaces a collaborator and returns fixed answers in a test.
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
Put the TDD cycle in order.
Show the answer
Write a failing test → Make it pass with simple code → Refactor while tests stay green
Red, green, refactor.
Source: MIT 6.031 Reading 3
Which inputs are good boundary cases for a function that averages a list?
- An empty list
- A list with one item
- A typical list of five items
- A list containing very large numbers
Show the answer
An empty list; A list with one item; A list containing very large numbers
Boundaries are edges of the input space: empty, single item, extreme values. The typical case is not a boundary (but you should test it too).
Source: MIT 6.031 Reading 3
Which level of the test pyramid should have the most tests?
- Unit tests
- End-to-end tests
- Manual tests
- Integration tests
Show the answer
Unit tests
Unit tests are fast and precise, so they form the wide base.
Source: MIT 6.031
Your suite has 100 % line coverage. What does that prove?
- Every line ran during the tests
- The code is bug-free
- Every possible input was tested
- The tests check every result
Show the answer
Every line ran during the tests
Coverage only tells you lines executed, not that results were checked.
Source: pytest documentation / coverage tools
Sources
- Node.js documentation: Test runner (node:test), OpenJS Foundation, 2025
- pytest documentation, pytest-dev, 2025
- MIT 6.031 Software Construction, Reading 3: Testing, MIT, 2022
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.