Tuesday, December 30, 2014

How to start TDD (Test Driven Development)?

This is a most common question I here in my training as soon as I say TDD is based on test first approach and we should write test before writing code. If you are already writing unit test for your code after writing code then moving on TDD is easy for you but many developers don’t even write unit test. If they write then they write functional test instead of unit test.

How to differentiate between unit test and functional test?
Developers often write functional test as unit test and ask how is different between tests written by testers. Answer is testers write more negative test cases compare to developers as part of functional test. Basically developers write test NOT to check a unit of code but to check a slice of functionality. This is not unit test. Unit test is for one unit of code like one method, one code block, interface, class or function and functional test is for a piece of functionality after integrating all related code. 

How to switch from functional test to unit test?
Until developer understand what unit test is and how to write an independent unit test to check piece of code, moving to TDD will not be easy. Usually developer start coding either from UI then middle tier OR database then middle tier so if you ask them to write business layer or controller directly without UI or DB, they struggle. I do see few developers have started writing unit test using unit testing framework but for different purpose. Purpose is to meet some agreed condition related to code coverage but if you go and look at it closely then again those tests are not unit test but functional test. Developers need to explore or get trained on writing unit test and practice it under guidance of coach for some time to make it habit. This will only happen when you understand importance of unit test and can think what exactly your code is supposed to do. Nowadays developers has good unit testing framework like junit, testing, nunit and ftest as well as need for unit test. So only missing thing is how to write good unit test.

Shift from code first to test first
This is another big area where many developer say how someone can write/think or execute test until code is not there. Since we all know our expectation from our code, so it is only about writing those expectations and nothing more. Believe in this idea that if I don't know how to test piece of code I am writing then most likely those code is not needed. TDD is not a testing practice but a development practice. It helps you to design code piece by piece to meet end goal. 

How to start writing TDD?
Take any existing application and try adding new code in system but think about how you will test your code? Whatever you can think, add those as unit test one by one and then write minimum code that can satisfy your each test. I have seen many team members going through this route and adopting TDD and I am sure you can also do that.


  1. Good explanation about TDD adoption approach. If some examples can be added to illustrate it here that will be great.