Test-Driven Development Response to Failed Tests
Short answer
C In Test-Driven Development, when a test fails, the developer modifies the implementation code to make the test pass, as the test defines the immutable requirement.
Question
Key Takeaways
In Test-Driven Development (TDD), when a test fails, the implementation code is modified to satisfy the test assertions. Option C is correct because the test defines the requirement and should never be altered to accommodate failing code, ensuring the software meets its specified design goals.
Expert Analysis
In TDD, when a test fails, the developer writes or modifies code to make the test pass. The test itself is not changed (it defines the requirement); the implementation code is updated.
Test-Driven Development (TDD) operates on a strict, iterative cycle often referred to as "Red-Green-Refactor." This methodology inverts the traditional software development approach by requiring that tests be written before the production code they validate. The question specifically addresses the state of the project immediately following a failed test execution, which represents the "Red" phase of the cycle. When a test fails, it indicates that the current implementation does not satisfy the specified requirements defined within that test case. Consequently, the only valid action to progress toward a passing state (the "Green" phase) is to modify the source code—either by writing new functions, classes, or methods—to fulfill the logic asserted by the failing test.
Option C, "code," is the correct answer because the fundamental contract of TDD dictates that the test suite serves as the immutable specification for the feature being built. If a test fails, it confirms that the code is incomplete or incorrect relative to that specification. Therefore, the developer must alter the implementation code to align with the test's expectations. This ensures that every line of code added has a corresponding automated verification mechanism, reducing the risk of defects and ensuring that the system behaves exactly as required by the initial test cases.
Option A, "schedule," is incorrect because while project timelines may need adjustment based on testing outcomes, this is a project management concern, not a technical step within the TDD workflow. Changing the schedule does not resolve the technical failure of the unit test or advance the codebase toward functionality. It is an administrative reaction, not a developmental one.
Option B, "project requirements," is incorrect in the context of immediate remediation. While requirements may evolve over time, the specific test that just failed is considered the current definition of the requirement. In TDD, if a test fails, you do not change the requirement (the test); you change the code to meet it. Modifying requirements mid-cycle without updating the test first would break the TDD discipline of having tests drive development. Requirements are formalized in the test; thus, altering them without changing the test would leave the test failing for a different reason, violating the principle that tests define the acceptance criteria.
Option D, "test," is the most common misconception in TDD. Changing the test after it fails is considered "moving the goalposts" and is strictly prohibited in genuine TDD practice. If a test fails and the developer changes the test to make it pass without changing the code, they have merely created a false positive. The test no longer validates any meaningful behavior, rendering the entire automation effort useless. The integrity of the TDD process relies on the test remaining constant as the source of truth; therefore, the test is never altered to accommodate failing code. Instead, the code is refactored or rewritten until it satisfies the existing test assertions.
This distinction highlights the core mechanic of TDD: the test acts as a guardrail. The developer navigates the codebase by observing the test results. Failure is not an error to be hidden by adjusting the test; it is a signal that the code needs work. By exclusively modifying the code (Option C), the developer ensures that the software evolves in lockstep with its verified specifications, maintaining high quality and adherence to design intent throughout the development lifecycle.
Exam Strategy
When analyzing TDD questions, remember the mantra: 'Tests drive code, not the other way around.' If a scenario describes a failed test, the immediate technical response is always to update the production code. Avoid options suggesting test modification unless the scenario explicitly involves legacy code or non-TDD contexts. Focus on the 'Red-Green-Refactor' cycle where Red (fail) leads directly to Green (pass via code changes).
Related Analysis
Practice All 200-901 Questions
Access 105 questions with complete answers and detailed explanations.
View Full 200-901 Practice Test →