Free CAD Test-Driven Development (TDD) Questions and Answers 1 — Questions and Answers
Question 1: A developer practicing Test-Driven Development (TDD) writes a test for a new feature and runs it, confirming that it fails. According to the TDD cycle, what is the immediate next step?
- Write the simplest possible production code to make the test pass. (Correct answer)
- Refactor the existing code to improve its design.
- Write another failing test for the next part of the feature.
- Ask a QA engineer to review and approve the failing test.
Correct answer: Write the simplest possible production code to make the test pass.
The TDD cycle follows the 'Red-Green-Refactor' pattern. After writing a failing test (Red), the next step is to write the minimum amount of production code necessary to make the test pass (Green). Only after the test passes should the developer consider refactoring.
Question 2: An agile team is adopting TDD to improve their software quality. One of the primary benefits they can expect is that TDD fosters a specific type of design. Which of the following best describes this design approach?
- Big Design Up Front (BDUF)
- Emergent Design (Correct answer)
- Contract-First Design
- Architecture-Driven Design
Correct answer: Emergent Design
TDD is a key enabler of emergent design. By writing tests first for small pieces of functionality and refactoring continuously, the software design evolves incrementally based on the actual needs of the code, rather than being planned extensively upfront.
Question 3: During the 'Refactor' step of the TDD cycle, what is the developer's main goal?
- To add new functionality that was discovered while writing the code.
- To improve the internal structure and readability of the code without changing its external behavior. (Correct answer)
- To write more tests to increase code coverage.
- To ensure the code integrates correctly with other system modules.
Correct answer: To improve the internal structure and readability of the code without changing its external behavior.
The Refactor phase is strictly for improving the quality of the code that was written to make the test pass. The key rule is to clean up the implementation (e.g., remove duplication, improve names) while ensuring all existing tests continue to pass, thus not changing the code's observable behavior.
Question 4: A developer is tasked with adding a complex validation rule to an existing feature. Using a strict TDD approach, which of the following should they do FIRST?
- Modify the existing production code to include the new validation logic.
- Discuss the design of the validation rule with the system architect.
- Write a small, specific unit test that checks for the new validation rule and fails. (Correct answer)
- Create an integration test to see how the new rule affects the entire feature.
Correct answer: Write a small, specific unit test that checks for the new validation rule and fails.
Test-Driven Development always begins with writing a test before writing the production code. For a new piece of functionality, the first action is to write a failing test that defines and verifies the desired behavior.
Question 5: Which of the following is a common misconception about Test-Driven Development in an Agile context?
- TDD creates a safety net of regression tests that enables confident refactoring.
- TDD serves as a form of living documentation for the code's behavior.
- TDD requires writing all possible tests for a feature before writing any implementation code. (Correct answer)
- TDD helps developers focus on writing only the code that is necessary to meet requirements.
Correct answer: TDD requires writing all possible tests for a feature before writing any implementation code.
A common myth is that TDD involves writing a comprehensive suite of tests upfront. In reality, TDD is an iterative process where you write only one small, failing test at a time, then write the code to make it pass, and then refactor before moving on to the next test.
Question 6: An agile developer is in the 'Green' phase of the TDD cycle. They have just written some code that makes their failing test pass. However, they notice the code is inefficient and contains some duplication. What should they do NEXT?
- Immediately rewrite the code to be more efficient.
- Write another failing test for the next piece of functionality.
- Move to the 'Refactor' phase to clean up the code while keeping the test green. (Correct answer)
- Commit the code as-is because the test is passing.
Correct answer: Move to the 'Refactor' phase to clean up the code while keeping the test green.
After achieving a passing test (Green), the immediate next step in the TDD cycle is to Refactor. The purpose of the Green phase is solely to pass the test, even with a suboptimal solution. The subsequent Refactor phase is dedicated to improving the code's internal quality without breaking the test.
A developer practicing Test-Driven Development (TDD) writes a test for a new feature and runs it, confirming that it fails.
According to the TDD cycle, what is the immediate next step?