CAD Test-Driven Development (TDD) 3 — Questions and Answers
Question 1: What is a 'test double' in TDD terminology?
- Running each test twice to check for flakiness
- A generic term for objects that stand in for real dependencies in tests (Correct answer)
- A test that covers two related behaviors
- A duplicate test used for comparison
Correct answer: A generic term for objects that stand in for real dependencies in tests
Test doubles (mocks, stubs, fakes, spies, dummies) replace real collaborators to isolate the unit under test.
Question 2: In TDD, what distinguishes a 'stub' from a 'mock'?
- Stubs verify interactions; mocks return canned responses
- Stubs return canned responses; mocks verify interactions occurred (Correct answer)
- Stubs are used only in integration tests; mocks in unit tests
- There is no practical difference between them
Correct answer: Stubs return canned responses; mocks verify interactions occurred
Stubs provide predetermined return values, while mocks additionally assert that specific calls were made.
Question 3: Which metric measures how much of the production code is exercised by tests?
- Cyclomatic complexity
- Code coverage (Correct answer)
- Defect density
- Velocity
Correct answer: Code coverage
Code coverage reports what percentage of lines, branches, or paths are executed during test runs.
Question 4: A team practicing TDD notices tests are slow and rarely run. The best corrective action is to:
- Remove assertions to speed execution
- Profile and optimize tests, possibly replacing slow I/O with fakes (Correct answer)
- Reduce the number of tests to only critical paths
- Switch to manual testing for slow paths
Correct answer: Profile and optimize tests, possibly replacing slow I/O with fakes
Slow tests discourage frequent execution; isolating I/O with fakes or in-memory alternatives restores speed without losing coverage.
Question 5: What does 'Outside-In TDD' (also called London School TDD) emphasize?
- Starting tests from the database layer upward
- Starting from the outermost acceptance test and mocking collaborators inward (Correct answer)
- Writing all tests before any production code across the entire system
- Using only integration tests to drive design
Correct answer: Starting from the outermost acceptance test and mocking collaborators inward
Outside-In TDD starts with a failing acceptance test, then drives lower-level units by mocking dependencies not yet built.
Question 6: Which TDD anti-pattern involves testing implementation details rather than observable behavior?
- Test isolation
- White-box over-specification (Correct answer)
- Boundary value analysis
- Exploratory testing
Correct answer: White-box over-specification
Over-specifying internal implementation makes tests brittle; they break on valid refactors that don't change behavior.
Question 7: How does TDD relate to regression testing?
- TDD replaces regression testing entirely
- The accumulated test suite acts as an automated regression safety net (Correct answer)
- TDD only catches new defects, not regressions
- Regression testing must be done manually after TDD cycles
Correct answer: The accumulated test suite acts as an automated regression safety net
Every passing test built up through TDD becomes a regression check that runs automatically on subsequent changes.
What is a 'test double' in TDD terminology?