Building Enterprise JavaScript Applications
上QQ阅读APP看书,第一时间看更新

Test-Driven Development

Test-Driven Development is a development practice created by Kent Beck, it requires the developer to write tests for a feature before that feature is implemented. This provides some immediate benefits:

  • It allows you to validate that your code works as intended.
  • It avoids errors in your test suite, if you write your test first, then run it, and it does not fail, that's a prompt for you to check your test again. It might just be that you have inadvertently implemented this feature by chance, but it could also be an error in your test code.
  • Since existing features would be covered by existing tests, it allows a test runner to notify you when a previously functional piece of code is broken by the new code (in other words, to detecting regressions). This is especially important for developers when they inherit old code bases they are not familiar with.

So, let's examine the principles of TDD, outline its process, and see how we can incorporate it into our workflow.

There are different flavors of TDD, such as  Acceptance Test-Driven Development  ( ATDD ), where the test cases mirror the acceptance criteria set by the business. Another flavor is  Behavior-Driven Development  ( BDD ) , where the test cases are expressed in natural language (that is, the test cases are human readable).