Why should I practice Test Driven Development and how should I start?

前端 未结 4 1453
余生分开走
余生分开走 2020-11-28 20:56

Lots of people talk about writing tests for their code before they start writing their code. This practice is generally known as Test Driven Development or TDD for short. Wh

4条回答
  •  死守一世寂寞
    2020-11-28 21:30

    Benefits

    1. You figure out how to compartmentalize your code
    2. You figure out exactly what you want your code to do
    3. You know how it supposed to act and, down the road, if refactoring breaks anything
    4. Gets you in the habit of making sure your code always knows what it is supposed to do

    Getting Started

    Just do it. Write a test case for what you want to do, and then write code that should pass the test. If you pass your test, great, you can move on to writing cases where your code will always fail (2+2 should not equal 5, for example).

    Once all of your tests pass, write your actual business logic to do whatever you want to do.

    If you are starting from scratch make sure you find a good testing suite that is easy to use. I like PHP so PHPUnit or SimpleTest work well. Almost all of the popular languages have some xUnit testing suite available to help build and automate testing.

提交回复
热议问题