CAD FREE CAD Test-Driven Development (TDD) Questions and Answers 2 — Questions and Answers
Question 1: In TDD, what is the primary purpose of the refactoring step after a test passes?
- To add new functionality to the codebase
- To improve code structure without changing behavior (Correct answer)
- To write additional test cases for edge cases
- To remove passing tests that are no longer needed
Correct answer: To improve code structure without changing behavior
Refactoring in TDD improves the internal structure and readability of code while keeping all existing tests passing and behavior unchanged.
Question 2: Which of the following best describes the 'Arrange-Act-Assert' pattern in TDD?
- A project management framework for agile teams
- A pattern where you set up preconditions, execute the action, and verify the outcome (Correct answer)
- A method for prioritizing which tests to write first
- A deployment strategy for test environments
Correct answer: A pattern where you set up preconditions, execute the action, and verify the outcome
Arrange-Act-Assert (AAA) structures each test by setting up the test data, performing the action under test, and then verifying the expected result.
Question 3: What is a 'test double' in the context of TDD?
- A test that validates two requirements simultaneously
- An object that stands in for a real dependency during testing (Correct answer)
- A duplicate test written to increase code coverage
- A secondary test suite run in a different environment
Correct answer: An object that stands in for a real dependency during testing
A test double is a generic term for any object that replaces a real dependency, including mocks, stubs, fakes, and spies.
Question 4: Why should TDD practitioners avoid writing multiple failing tests before making any of them pass?
- It violates the single responsibility principle
- It makes it unclear which code change satisfies which requirement (Correct answer)
- It increases compilation time significantly
- It prevents the use of continuous integration tools
Correct answer: It makes it unclear which code change satisfies which requirement
Writing multiple failing tests at once obscures the direct link between a specific code change and the test it satisfies, undermining the tight feedback loop TDD relies on.
Question 5: In TDD, what does 'triangulation' refer to?
- Running tests across three different environments simultaneously
- Adding more specific test cases to drive a more general implementation (Correct answer)
- Splitting a large class into three smaller components
- Measuring code coverage from three different metrics
Correct answer: Adding more specific test cases to drive a more general implementation
Triangulation involves writing additional test cases with different inputs to force the implementation to become more generalized rather than hard-coded.
Question 6: What is the recommended approach when a TDD practitioner discovers a bug in production code?
- Fix the bug immediately and then run existing tests
- Write a failing test that reproduces the bug before writing the fix (Correct answer)
- Rewrite the entire module using TDD from scratch
- Add the bug to the backlog and address it in the next sprint
Correct answer: Write a failing test that reproduces the bug before writing the fix
Writing a failing test first ensures the bug is captured as a regression test, preventing it from reoccurring after the fix is applied.
In TDD, what is the primary purpose of the refactoring step after a test passes?