Boss lesson · Unit 01
Test first
Red, green, refactor: grow one function test by test
What you practise
- Red, green, refactor on a small function built one failing test at a time
- What test-first changes about design
- When it isn't worth it
Try a question from this lesson
Pick one
Test-first: the test exists before the real code does, so running it now fails. What does seeing it fail prove?
def leap(year):
return False
def test_2024_is_leap():
assert leap(2024) == Truefunction leap(year) {
return false;
}
function test2024IsLeap() {
assertEqual(leap(2024), true);
}function leap(year: number): boolean {
return false;
}
function test2024IsLeap(): void {
assertEqual(leap(2024), true);
}Correct answer
The test can fail, so a later pass means something
Why
A test you have never seen fail might be unable to fail, like the one that only checked for "not nothing". Red first proves the test is wired to the behaviour it names.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Red, green, refactor
Red: write one small test and watch it fail.
Green: write the least code that makes it pass, however naive.
Refactor: tidy the code while every test stays green.
Then pick the next test. The function grows one proven step at a time, and you are never more than a minute away from working code.
11 more exercises in the app
- 5× multiple choice
- 2× type the answer
- 1× fill the blanks
- 1× order the lines
- 1× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.