CAD Test-Driven Development (TDD) 2 — Questions and Answers
Question 1: What does the 'Red' phase in the Red-Green-Refactor cycle indicate?
- The test passes with flying colors
- The test fails because the production code doesn't exist yet (Correct answer)
- The code has a runtime error unrelated to tests
- Refactoring has introduced a regression
Correct answer: The test fails because the production code doesn't exist yet
Red means the newly written test fails, confirming the feature is not yet implemented.
Question 2: Which practice does TDD most directly discourage?
- Writing modular code
- Writing production code before any test exists (Correct answer)
- Continuous integration
- Pair programming
Correct answer: Writing production code before any test exists
TDD mandates that a failing test must exist before production code is written.
Question 3: In TDD, what is the purpose of writing only the minimum code to pass a test?
- To reduce compile time
- To avoid over-engineering and keep design emergent (Correct answer)
- To satisfy code coverage tools
- To make refactoring optional
Correct answer: To avoid over-engineering and keep design emergent
Writing minimal code prevents premature optimization and lets design evolve from actual requirements.
Question 4: A developer writes a test that passes immediately without any production code changes. This is called a:
- Green test
- Degenerate test (Correct answer)
- Assertion-free test
- False negative
Correct answer: Degenerate test
A test that passes without implementation changes is a degenerate (trivially-passing) test that provides no value.
Question 5: What is 'triangulation' in the context of TDD?
- Running tests on three different environments
- Adding multiple specific examples to drive out a general algorithm (Correct answer)
- Organizing tests into three layers: unit, integration, and E2E
- Using three assertions per test case
Correct answer: Adding multiple specific examples to drive out a general algorithm
Triangulation means writing multiple concrete test cases to force a general, correct implementation rather than a hard-coded one.
Question 6: Which statement best describes TDD's impact on software design?
- TDD has no influence on design; it only checks correctness
- TDD drives a loosely coupled, testable design as a side effect (Correct answer)
- TDD enforces a specific architectural pattern like MVC
- TDD replaces the need for design reviews
Correct answer: TDD drives a loosely coupled, testable design as a side effect
Writing tests first naturally pressures developers to create small, focused, independently testable units with low coupling.
Question 7: When should a developer refactor during TDD?
- Before writing the failing test
- Only after the full feature is complete
- After all tests pass (Green phase) (Correct answer)
- During the Red phase to simplify the test
Correct answer: After all tests pass (Green phase)
Refactoring happens in the third phase, after tests are green, to improve code quality without changing behavior.