Skip to content

Red Then Green

You'll write a test before the code it tests, watch it fail, then make it pass, and trust your own code for the first time.

About

Tests are the difference between hoping your code works and knowing it does. The first one takes an hour to set up and feels a little silly. The tenth one catches a bug you would have shipped. Use the test runner that comes with your language, like pytest, Jest, Go's testing package or RSpec, rather than a framework you have to study for a week.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
6
Takes
One weekend
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in Coding to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Install a test runner and get one passing test that checks 1 + 1 is 2. The point of this one is the plumbing: how files are named, how you run them, and what the output looks like. You're after green text. Take a screenshot of the passing run. Photo
  2. 2
    Write a failing test for a function that doesn't exist yet, then write the function. Pick something small, like is_leap_year or a function that turns a title into a slug. Red comes first, so watch it fail for the right reason, which is that the function is missing rather than a typo, and then make it pass. Take a screenshot of the red run, then one of the green run. Photo
  3. 3
    Add five more test cases, including the edge cases you'd rather not think about. Try an empty string, zero, negative numbers, Unicode, and a year like 1900, which is not a leap year. At least one of these will find a bug in your function. Take a screenshot of all the tests green with their names visible. Photo
  4. 4
    Break the function on purpose and watch the tests catch it. Change a less-than to a less-than-or-equal and run the suite. Something should go red, and if nothing does, you're missing a test. Take a screenshot of the failure with the assertion message. Photo
  5. 5
    Test a function that depends on time or randomness without getting flaky results. Pass the clock or the random source in as an argument so the test can control it. This is the first time most people see why dependencies are passed in rather than reached for. Take a screenshot of the test and the function side by side. Photo
  6. 6
    Run the whole suite with one command from the terminal. It should be one line with no clicking. Then run it before every commit for the rest of this track. Take a screenshot of the full suite passing with the test count showing. Photo