Editor

TDD Bug Fix: Reproduce Then Fix

Advanced · Write the code · from Test-Driven Development: Write the Test First, Then the Code

From the lesson: Test-Driven Development: Write the Test First, Then the Code

Imagine you're drawing a picture. Most people sketch the outline first, then fill in the colors. You wouldn't start by painting random patches of blue and hope a sky appears.

Read the lesson

A user reported that count_vowels returns wrong results for uppercase strings. Use the TDD bug-fix approach: (1) write a test called test_uppercase_vowels that reproduces the bug with "HELLO" (should return 2), (2) fix the function, (3) add a test called test_mixed_case for "PyThOn" (should return 1), (4) make sure all tests pass. Print PASS: <test_name> for each test, then Bug fix verified!.

def count_vowels(text):
count = 0
for char in text:
if char in 'aeiou': # Bug is here!
count += 1
return count
# Step 1: Write a test that REPRODUCES the bug
# Step 2: Fix the function
# Step 3: Add more tests
# Step 4: Run all tests

More TDD exercises

All Python exercises · 50 practice problems · Testing quiz