[ACCEPTED]-Why should I practice Test Driven Development and how should I start?-tdd
There are a lot of benefits:
- You get immediate feedback on if your code is working, so you can find bugs faster
- By seeing the test go from red to green, you know that you have both a working regression test, and working code
- You gain confidence to refactor existing code, which means you can clean up code without worrying what it might break
- At the end you have a suite of regression tests that can be run during automated builds to give you greater confidence that your codebase is solid
The best way 7 to start is to just start. There is a great 6 book by Kent Beck all about Test Driven Development. Just 5 start with new code, don't worry about old 4 code... whenever you feel you need to refactor 3 some code, write a test for the existing 2 functionality, then refactor it and make 1 sure the tests stay green. Also, read this great article.
The benefits part has recently been covered, as for where to 6 start....on a small enterprisey system where 5 there aren't too many unknowns so the risks 4 are low. If you don't already know a testing 3 framework (like NUnit), start by learning 2 that. Otherwise start by writing your first 1 test :)
Benefits
- You figure out how to compartmentalize your code
- You figure out exactly what you want your code to do
- You know how it supposed to act and, down the road, if refactoring breaks anything
- 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 13 want to do, and then write code that should 12 pass the test. If you pass your test, great, you 11 can move on to writing cases where your 10 code will always fail (2+2 should not equal 9 5, for example).
Once all of your tests pass, write 8 your actual business logic to do whatever 7 you want to do.
If you are starting from 6 scratch make sure you find a good testing 5 suite that is easy to use. I like PHP so 4 PHPUnit or SimpleTest work well. Almost 3 all of the popular languages have some xUnit 2 testing suite available to help build and 1 automate testing.
In my opinion, the single greatest thing 5 is that it clearly allows you to see if 4 your code does what it is supposed to. This 3 may seem obvious, but it is super easy to 2 run astray of your original goals, as I 1 have found out in the past :p
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.