Test Driven Development Flashcards
Test Driven Development:
Basic Idea
- Think about what code should do before writing any code
- Write a test for that requirement
- Write the simplest possible code that passes that test, without causing it to fail existing tests
- Refactor code as necessary to pass all tests
The idea is to meet requirements incrementally and always have working code.
This is called the “Red-Green-Refactor” cycle
Benefits of
Test Driven Development
- Confidence in Change:
- Increase overall confidence in the code
- Fearlessly change code
- Provides Documentation of Requirements
- Discover Usability issues early
- Results in high quality software
Test Driven Development:
Three Laws
DO NOT write production code until you have written failing unit tests
DO NOT write more of a unit than is sufficient to fail
DO NOT write more production code than is sufficient to pass the currently failing test
Test Driven Development:
Most Important Rule
DO NOT
Write code in your head
BEFORE
You write the TEST
Test Driven Development
Cycle
Write Test Code (Red)
Write Functional Code (Green)
Refactor
TDD Cycle:
Write Test Code (Red) Phase
Purpose and Benefits
- Write a test that fails
- Guarantees all code is testable
- Provides specification for the code that will be written in the next phase
- Helps you think about the design
- Ensures functional code is tangible
TDD Cycle:
Write Functional Code (Green) Phase
Purpose and Benefits
- Write the simplest working solution
- Fulfill the requirements of the test code
- Leave improvements/optimizations for later
- Code written is only designed to pass the test
TDD Cycle:
Refactor Phase
Purpose and Benefits
- Clean up all code (test and production)
- Make sure code expresses intent
- Rethink/optimize design
- Delete unnecessary code