What is TDD?
Test Driven Development (TDD) is a software technique that relies on testing (in an automated fashion) the unit of work before implementing it.
It is based on the principle “Red-Green-Refactor”
- Red implies fail
- Green implies pass
- Refactor the code still ensuring that the test passes.
TDD has its roots in extreme programming. In TDD the software developer first writes a test that fails. The developer then writes the code to make the failing test pass. Finally, the code is refactored and the test is verified to ensure that it still passes.
There are quite a number of tools available for TDD. A combination of Nunit (for writing unit tests), NMock (a mocking framework) and Cruise Control.Net (for continuous integration) works well for .NET developers. The test contains assertion indicating the success or failure of the test.
Few Dos and the Don’ts for TDD
- Write unit test only for the intended target
- Choose a test name to clearly indicate the intention of the test
- Write the test as fail first and not pass first i.e. write test to fail and not pass in all cases.
- Write separate unit tests to test separate functionalities rather than reuse the existing ones.
- Do not write unit tests that run incredibly slow. Use mocks instead of slow interfaces.
- Avoid excessive code in setup to set the data for testing the target.
- Avoid God Objects (overdependence on functionality that are center of the implementation)
- Avoid over use of mock objects
- Do not compromise on encapsulation and data hiding to achieve 100% code coverage
- Do not use one unit test to generate data for another unit test
- Do not make environment specific assumption.
- Do not use logging or tracing if the test passes.
Few Useful links
http://weblogs.asp.net/scottdockendorf/archive/2004/10/06/238811.aspx
http://msdn.microsoft.com/en-us/library/aa730844(VS.80).aspx
http://codebetter.com/blogs/darrell.norton/articles/50337.aspx
http://martinfowler.com/articles/mocksArentStubs.html
No comments:
Post a Comment