Test-Driven Development Refactoring Step
Short answer
C Refactoring code that is covered by existing tests is the standard post-pass step in the TDD Red-Green-Refactor cycle.
Question
Key Takeaways
In Test-Driven Development (TDD), the 'Refactor' phase involves improving code structure using existing tests as a safety net. Option C correctly identifies this action, distinguishing it from pre-test creation or general regression testing.
Expert Analysis
In TDD, after the code passes the test, developers refactor the code to improve its structure while keeping all tests green. This ensures code quality without changing behavior.
Test-Driven Development (TDD) is a rigorous software engineering discipline that fundamentally alters the sequence and intent of coding activities. It is not merely a testing methodology but a design-driven approach where requirements are translated into specific conditions before any functional implementation exists. The core cycle of TDD is commonly described as "Red-Green-Refactor." First, a developer writes an automated test case that defines a desired improvement or new function; this test initially fails because the functionality does not yet exist (the "Red" phase). Second, the developer produces just enough code to make the test pass (the "Green" phase). Finally, and most critically for the purpose of this analysis, the developer refactors the newly created code to remove duplication, improve naming, and enhance readability, ensuring that the test suite still passes (the "Refactor" phase).
Option C, "refactoring code that is covered by existing tests," is the correct answer because it describes the essential third step of the TDD cycle. Refactoring in this context is safe precisely because there is an existing, passing test suite. The tests act as a safety net, allowing the developer to change the internal structure of the code without altering its external behavior. If the refactoring introduces a bug, the tests will fail, alerting the developer immediately. Without existing tests covering the code, refactoring would be risky and speculative, potentially breaking functionality without detection. Therefore, the presence of existing tests is the prerequisite that enables the refactoring task.
Option A, "creating test scenarios based on continuous development," misinterprets the timing and causality of TDD. In TDD, test scenarios are created before the corresponding code is written, not based on ongoing development. They drive the development process rather than following it. Continuous development is a broader concept, often associated with CI/CD pipelines, whereas TDD is a specific unit-level practice focused on writing tests first.
Option B, "writing code without committing any coding violations," is incorrect because it confuses TDD with static analysis or linting. While TDD encourages clean code through refactoring, it does not inherently prevent coding violations during the initial coding phase. Violations are identified and corrected during the refactoring stage, guided by the tests. Writing code without violations is a goal, but it is not the specific task performed because TDD is used; rather, it is an outcome of the disciplined refactoring step.
Option D, "testing existing software before developing new code," describes regression testing or pre-existing validation, which is not the defining characteristic of TDD. TDD focuses on creating new tests for new functionality before that functionality is implemented. Testing existing software is a maintenance activity, whereas TDD is a creation activity. The key distinction is that TDD tests are written prior to the production code they validate, ensuring that every line of code has a corresponding requirement defined by a test.
The mechanism that makes Option C valid is the contract between the test suite and the codebase. The tests define the expected behavior, and the code implements it. When the code passes the tests, the behavior is verified. Refactoring changes the implementation details (how it works) but not the interface (what it does). Because the tests verify the interface, they allow the implementation to change safely. This is the unique value proposition of TDD: it enables structural improvements to the codebase at any time, provided the tests remain green. This supports long-term maintainability and reduces technical debt, which is a primary objective of modern software development practices outlined in certification objectives such as 1.3 (Describe the concepts of test-driven development).
Exam Strategy
When analyzing TDD questions, focus on the strict order of operations: Write Failing Test -> Write Passing Code -> Refactor. Look for options that describe modifying code structure after a test has passed. Avoid options that suggest testing happens after full development or that confuse TDD with general QA processes.
Related Analysis
Practice All 200-901 Questions
Access 105 questions with complete answers and detailed explanations.
View Full 200-901 Practice Test →