Your First TDD Cycle
Intermediate · 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 lessonUse TDD to build an is_palindrome function. Write the test first, then the function. Your function should return True if a string reads the same forwards and backwards (case-insensitive), and False otherwise. Test with at least 3 cases: a palindrome, a non-palindrome, and an empty string. Print TDD palindrome test passed! at the end.
# Step 1: Write the tests FIRST
def test_is_palindrome():
# Test a palindrome
# Test a non-palindrome
# Test empty string
pass
# Step 2: Write the function to make tests pass
def is_palindrome(s):
pass
# Step 3: Run tests
test_is_palindrome()
print('TDD palindrome test passed!')
More TDD exercises
- TDD: Build a Password ValidatorAdvanced
- Predict the TDD ProcessIntermediate
- TDD Bug Fix: Reproduce Then FixAdvanced
- TDD Challenge: Build a Word CounterAdvanced