Course contentsShow
← Software Engineering
Lesson 491 of 1,93315. Test-Driven DevelopmentFree lesson

Green: Making the Test Pass Quickly

Discover strategies for writing minimal code to make tests pass, including fake implementations and obvious solutions.

Green: Making the Test Pass Quickly

What you'll learn: How to write the simplest code possible to turn a failing test green, prioritizing speed over elegance.

The Core Idea

Once you have a failing test (Red), your next job is to make it pass as quickly as possible. This doesn't mean writing beautiful, perfect code—it means writing just enough code to satisfy the test. You'll clean up later during Refactor.

Think of it like sketching before painting. You don't start with fine details and perfect colors; you block out shapes quickly first. The Green phase is your rough sketch.

Two Primary Strategies

Fake It (Obvious Implementation)

Sometimes the simplest way forward is to return a hard-coded value:

// Test expects: calculateTotal([5, 10]) === 15
function calculateTotal(prices) {
  return 15; // Hard-coded! But the test passes.
}

Yes, this feels absurd—but it works. Once you add more tests, you'll be forced to generalize.

Obvious Implementation

When the solution is straightforward, just write it:

function calculateTotal(prices) {
  return prices.reduce((sum, price) => sum + price, 0);
}

Use this when the path forward is clear and low-risk.

Why Speed Matters

Getting to green quickly gives you rapid feedback. You confirm your test setup is correct and you're on the right track. Premature optimization or over-engineering at this stage wastes time and introduces bugs.

The mantra: Make it work, make it right, make it fast. Green is "make it work."

Key Takeaway: In the Green phase, write the simplest code that makes your test pass—fake implementations and obvious solutions are both valid strategies. Elegance comes later.