Beginner LevelSoftware Engineering Principles
What is Test-Driven Development (TDD) and the Red-Green-Refactor cycle?
❓ Interview Prompt: Explain Test-Driven Development (TDD), the Red-Green-Refactor workflow, code design benefits, and test-first mindset.
Test-Driven Development (TDD) is an iterative software development technique where unit tests are written before production code:
The Red-Green-Refactor Cycle:
1
🔴 Red: Write a unit test for a feature that does not exist yet. Run the test and watch it FAIL.
2
🟢 Green: Write the absolute minimum production code required to make the test PASS.
3
🔵 Refactor: Clean up and optimize the production code and tests while guaranteeing all tests remain GREEN.
Code Implementation Example
typescript
// Step 1 (Red): Define test requirement first
// test('slugify formats strings to lowercase hyphenated', () => expect(slugify('Hello World')).toBe('hello-world'));
// Step 2 (Green): Implement minimal working code
export function slugify(text: string): string {
return text.toLowerCase().replace(/\s+/g, '-');
}
// Step 3 (Refactor): Clean up and optimize regex while keeping test green!💡Executing the TDD Red-Green-Refactor cycle for a slugify utility function.
🎯 Interview Key Takeaway
TDD forces clear requirement definitions, prevents over-engineering, and produces self-testing codebases.
#TDD#Testing#Red-Green-Refactor#Beginner
Back to all Software Engineering Principles Questions→