Testing & Debugging
Write tests that can actually fail, pick the inputs where bugs live, and shape code so it is easy to check. Then hunt bugs with a method instead of luck: reproduce, shrink, bisect, and pin each one down with a test so it never returns.
- Beginner
- 4 units, 12 lessons, 148 exercises
- Your language
- Builds on Algorithm Foundations
Unit 01 · 3 lessons
Writing tests
Turn "I think it works" into a claim a machine can check
- 01Assertions
A test is a claim a machine can check
- 02Choosing cases
Edges, boundaries and inputs that must be rejected
- BOSSTest first
Red, green, refactor: grow one function test by test
Read the unit guide
A test is a small function that makes one claim about your code and checks it. It has three steps. Arrange: build the input. Act: call the code. Assert: state what must be true. If the claim is false the assertion raises an error and the test fails; no error means it passed.
A test is only worth something if it can fail. Before you trust a green test, ask which bug would turn it red. "The result is not empty" passes for almost any wrong answer; "the result is 45" does not.
One behaviour per test, and a name that says which. When test_total_applies_discount_once fails at 3 a.m. you know what broke without opening the file. A test with ten assertions stops at the first failure and hides the other nine.
Choosing inputs is the real skill. Inputs the code treats alike form a class, and one test per class is enough. Then go to the edges between classes and test both sides: 49 and 50, not 20 and 80. For collections the classes are empty, one item, many, duplicates and negatives. Add the inputs that must be rejected, and assert that they are.
Test-first turns this into a rhythm. Red: write one small test and watch it fail. Green: write the least code that passes. Refactor: tidy up while everything stays green. Every line of code ends up demanded by a test, and every test has been seen to fail.
The classic pitfall is the test that agrees with the bug: an input like 100 for a "10% off" function that subtracts 10. Pick inputs where the right answer and the likely wrong answers differ.
Unit 02 · 3 lessons
Testable code
Shape code so that checking it is easy, fast and honest
- 01Pure functions
Same input, same output, nothing hidden
- 02Test doubles
Stubs, spies, fakes and mocks stand in for the slow parts
- BOSSWhat kind of test
Unit, integration, end-to-end: spend your tests wisely
Read the unit guide
Code that is hard to test is telling you something about its design. The usual message: it does too much, or it reaches out for things it should have been handed.
A pure function is the easiest thing in the world to test. Its result depends only on its arguments and it changes nothing outside itself, so a test is one call and one comparison. Clocks, random numbers, files, networks and globals are hidden inputs: they make the same call give different answers on different days.
The cure is to pass them in. A function that takes the hour as an argument can be tested for any hour; a function that takes its random source can be handed one that always says "last". Keep the logic pure and push reading, printing and network calls to a thin outer shell.
When the code must talk to something slow or dangerous, a test hands it a stand-in. A stub returns canned answers. A spy also records how it was called. A fake is a small working version, such as a dictionary instead of a database. A mock fails the test when the calls are not the expected ones. Replace only what you must: a test that mocks everything tests the mocks.
Test what the code does, not how. An assertion on the result survives every refactor; an assertion that a loop ran three times breaks on the first harmless rewrite.
Tests come in layers. Many unit tests: milliseconds each, and a failure names the culprit. Some integration tests for the seams between parts. A few end-to-end tests through the whole app. Flaky tests, the ones that pass and fail without a change, come from shared state, real clocks, test order and live networks. Coverage tells you what was never run, not what was tested well.
Unit 03 · 3 lessons
Debugging
Find bugs with a method instead of luck
- 01Reproduce it first
Make it fail on demand, then make it small
- 02Reading errors
The message says what, the stack trace says where
- BOSSThe bug that hides
Halve the suspects: in the code, the input and the history
Read the unit guide
Debugging is a search, and searches go faster with a method. The method has five steps: reproduce, shrink, guess, test the guess, pin it down.
Reproduce first. Until you can make the bug happen on demand you cannot study it, and you cannot know that a fix worked. For a bug that shows up "sometimes", find what varies between runs: the input, the time, a random seed, the order things happen in.
Then shrink. Cut the input down until removing anything more makes the bug vanish. A one-item list that gives the wrong answer leaves the bug nowhere to hide. Save that input as a failing test before you fix anything.
Read the error before you read the code. The message says what went wrong; the stack trace says where, and who called whom to get there. Start at the frame of your own code nearest the error. Remember that the line that crashes is often not the line at fault: a missing value blows up where it is first used, not where it went missing.
Form one hypothesis you could prove wrong, change one thing, and predict the result before you run it. If you are lost, bisect: check the data halfway through the code, or the version halfway through the history. Each check halves the suspects, so 64 candidates take 6 checks.
Three bugs to suspect by default: the off-by-one at a boundary, two names for one list (aliasing), and data changed while something else was still using it. Explaining the code aloud, line by line, to a colleague or a rubber duck, finds a surprising number of them.
Unit 04 · 3 lessons
Keeping it working
Let tools and habits catch bugs before users do
- 01Properties, not examples
State what must always hold, and let the computer pick the inputs
- 02Types and linters
Checks that read your code without running it, and what they miss
- BOSSNever again
Turn a bug report into a test, and let every push run it
Read the unit guide
Hand-picked examples only cover the inputs you thought of. A property test states something that must hold for every input, generates hundreds of inputs, and checks each one. Three kinds of property carry most of the weight: an invariant (sorted output is in order and has the same items), a round trip (decoding undoes encoding), and an oracle (the fast version agrees with a slow, obvious one).
Random inputs need a seed. With a fixed seed the same inputs come back on every run, so a failure can be reproduced. Then shrink the failing input, exactly as you would in any bug hunt.
Type checkers and linters read code without running it. A type checker proves that each value is the kind its user expects. A linter flags patterns that are usually mistakes, such as a parameter that is never used. Both cover every line, including the ones no test visits, and neither knows what the program is meant to do. Wrong logic with correct types still needs a test.
Every bug that reaches a user is a missing test. Turn the report into a failing test first, then fix the code, and keep the test for good. That is a regression test, and it is how a bug stays fixed.
Continuous integration runs all of this on every push: lint, types, tests, cheapest first. Green means every promise still holds. Red means the newest change broke one, and the rule is simple: fix it or revert it before anything else is merged.
Learn Testing & Debugging on your phone.
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.