天天看点

Test-Driven Development

Test-Driven Development (TTD) suggests that tests express the requirements with which the code must comply. This technique requires developers to write an automated unit test before writing feature code. As feature code evolves, automated tests provide immediate feedback to the developer and confirm, through true or false assertions, whether the code is behaving correctly. Because each feature of the software subsequently possesses a corresponding test or set of tests, higher confidence in the quality of the code is often the result.

The TDD process for developing features includes the following steps:

  • Write a test Developers are required to first develop a test that maps to the particular feature requirement.
  • Run all tests and see the new test fail Assuming that a set of tests exist across the code base, the new test should inevitably fail due to the lack of new feature code.
  • Write code that makes the test pass Feature code is developed to meet the requirement of the new test.
  • Run all tests and ensure all succeed Once the new feature code has been written, all tests should be executed again to ensure that new tests pass.
  • Refactor code Any code written to satisfy the previous steps may not be optimized and should subsequently be refactored to remove duplication or other code bloat.
  • Rinse and repeat Continue until there are no other requirements left to implement.

Other agile methodologies encourage developer-written unit tests, but TDD stands apart in its rigid view of testing before coding. TDD provides an interesting value proposition as a process that ensures high code quality; however, it does not provide an end-to-end framework for managing the software development life cycle. The goal of TDD is simply to be a framework for addressing customer requirements with software through an iterative approach to testing and coding.

继续阅读