Test-Driven Development (TDD) Flashcards
What does TDD stand for, and what is its main purpose?
TDD stands for Test-Driven Development.
It emphasizes writing tests before actual code to ensure the code meets requirements and remains maintainable.
List four reasons to use TDD.
Reliability: Ensures code correctness through automated tests.
Maintainability: Acts as documentation for easier understanding and maintenance.
Refactoring Confidence: Enables safe refactoring with a robust test suite.
Early Bug Detection: Catches bugs early, reducing costs of fixes later.
Name the three phases of the TDD cycle.
Red Phase
Green Phase
Refactor Phase
What happens during the Red Phase?
Write a failing test representing a specific requirement.
Clearly define what needs to be implemented.
What happens during the Green Phase?
Write the minimum code required to make the test pass.
Focus on functionality, not optimization.
What happens during the Refactor Phase?
Improve the code without altering its behavior.
Enhance readability, maintainability, and performance.
What are the first four best practices for TDD?
Start Small: Begin with small, focused tests for clarity and manageability.
Write Failing Tests First: Ensure tests fail initially to validate their effectiveness.
Implement Minimum Code: Write only the code needed to pass the test.
Run Tests Frequently: Catch issues early by regularly running tests.
What are the next four best practices for TDD?
Keep Tests Independent: Isolate tests to simplify debugging.
6. Maintain a Fast Test Suite: Optimize test speed for frequent testing.
7. Write Descriptive Tests: Use clear names and assertions for documentation.
8. Test Edge Cases: Cover diverse inputs, including edge cases, for robust coverage.
What are two common challenges in TDD?
Writing Tests for Legacy Code: Start by testing new features or refactoring.
Balancing TDD with Deadlines: Though initially time-consuming, TDD speeds up development over time.