200-901 Question 9
Single answerA development team is building a new API for their application. They decide to write tests for each function before implementing the actual code. How does this approach align with the principles of test-driven development (TDD)?
- A
It helps ensure that the code meets the test requirements from the beginning.
- B
It eliminates the need for refactoring code after implementation.
- C
It focuses on writing tests only after the code is fully implemented.
- D
It encourages writing minimal code necessary to pass the tests.
Show answer and explanation
Correct answer: A
Explanation
The scenario describes a key principle of test-driven development: writing tests before implementing the code. This ensures that the code is designed around the test cases from the start, leading to better alignment with requirements and fewer bugs. While refactoring and writing minimal code are part of TDD practices, the primary focus here is on the sequence of writing tests first.
- A. Correct.
This is correct. TDD emphasizes writing tests before implementation to ensure the code is built to meet the test requirements, promoting better design and reliability.
- B. Incorrect.
This is incorrect. TDD does not eliminate the need for refactoring; in fact, refactoring is an integral part of the TDD cycle to improve code quality after the test passes.
- C. Incorrect.
This is incorrect. TDD requires writing tests before the code is implemented, not after.
- D. Incorrect.
This is incorrect. While TDD encourages writing minimal code to pass the tests, this is not the main principle emphasized in the scenario described.