Understanding Why Tests Initially Fail in Test-Driven Development
Test-Driven Development (TDD) is a software development process where developers write tests before writing the actual product code. This process ensures that the code meets the specified requirements from the outset.
Exam Question
Why does a test written using Test-Driven Development initially fail?
(choose the best answer)
A. Because the product code to satisfy the test does not yet exist.
B. Because the test has not been refactored.
C. Because it has to be put into an automated test harness to be run.
D. Because the tests are checked in before the product code exists.
Correct Answer
A. Because the product code to satisfy the test does not yet exist.
Explanation
Correct Answer
A. Because the product code to satisfy the test does not yet exist:
In Test-Driven Development (TDD), the first step is to write a test that specifies a new piece of functionality or behavior that the product code should have. Since the product code to implement this functionality has not yet been written, the test is expected to fail. This failure is intentional and confirms that the test is properly detecting the absence of the required functionality. After the test fails, the developer writes the minimum amount of code necessary to pass the test.
Why the Other Options Are Less Appropriate
B. Because the test has not been refactored:
Refactoring occurs after the test passes, not before. The purpose of refactoring is to clean up the code, improve its structure, and remove any redundancies. The initial failure of the test is not related to refactoring.
C. Because it has to be put into an automated test harness to be run:
While tests in TDD are often run in an automated test harness, the initial failure of the test is due to the absence of the corresponding product code, not the need for an automated testing environment.
D. Because the tests are checked in before the product code exists:
This option describes a situation that might occur in a version control system, but it is not the reason why the test fails in the context of TDD. The test fails simply because the functionality it is testing has not been implemented yet.
Relevance to the PSD Exam
Understanding why tests initially fail in Test-Driven Development is crucial for the PSD exam. It emphasizes the importance of the TDD process in ensuring that code is driven by well-defined tests, leading to more reliable and maintainable software.
Key Takeaways
- TDD starts with writing a test that defines a new piece of functionality.
- The test initially fails because the product code to satisfy the test does not yet exist.
- This failure confirms that the test is valid and properly detecting the absence of the required functionality.
Conclusion
In Test-Driven Development, the initial failure of a test is expected and intentional, indicating that the product code to satisfy the test has not yet been written. This process ensures that development is guided by well-defined tests, leading to higher quality and more reliable software. For more information on preparing for the PSD exam, visit our Professional Scrum Developer PSDâ„¢ Exam Prep.