Thursday, 19 July 2012

Test driven development

We were going through a very bad phase a little before production, when a lead came to our team and asked why so much time to implement only two pages of requirement? We were dumb stuck to hear such a question from a senior, the two paged requirement had more than 180 unit test cases attached to it, but we couldn't answer anything at that time...reason, we had no unit test to support the fact that the module was complex.

My god, I was into hell lot of problems...somehow we pushed code in production ...but it was real nightmare.


 From there on I thought...from now on I am going to do development and test hand-in-hand, piece-by-piece.
I made a note…
NO MORE RE FACTORING TO CREATE UNIT TEST CASES AFTER DEVELOPMENT.


One of my friends introduced me to the concept called TEST-DRIVEN-DEVELOPMENT (TDD)

From then, I am a better developer, a better lead and a better individual.


What is Test Driven Development?

In my terms I say, Test Driven Development is a development methodology process where the developer writes the unit test case for a particular feature before writing the code itself.

In TDD there are three rules:
Rule 1: Don't start writing a piece of code until the unit test is written and fails.
Rule 2: Don't write any more piece of code that can be tested with the unit test.
Rule 3: Re factor and test once the unit test passes.

What the rule essentially means is:
Understand the requirement; understand what feature you are going to develop, once you have understood 
Write the unit test case, run the unit test case. Till now we have not written a single line of code, so if the unit test is proper, it will fail to execute.
Now write the code to make the unit test case pass. Till now you are not bothered about the best practices to write the code...you are just writing code to make the test case pass.
Once, the unit test case passes, you can sit and refactor your code and run the unit test case again. 

This cycle of 
  • Write a test case; make it fail [RED]
  • Write the code, pass the test case. [GREEN]
  •  Refactor the code and test. [BLUE]


These are three fundamental parts of TDD. 



Why should we adopt Test Driven Development?

Ideally to avoid yourself getting into any kind of trouble like mine, you should implement TDD.

From my experience following are the advantages of TDD:
  • It helps you to better understand the functionality that you are going to develop.
  • It makes sure that all functionality is testable.
  • It helps you to make sure CODE = REQUIREMENT and you have not injected more code.
  • If you have followed the Unit test rules fairly well, then TDD will help you to make sure the code which you authored and that ran fine once , will run fine always.
  • Since the code which is written has undergone through testing it decreases the cost and time required to fix issues towards the end of development cycle.
  • The unit test cases have been written prior to writing the code, ensures that you have not compromised to make unit test pass. (This is the general thing we do when we leave unit testing after writing the code.
  • Since you have set of automated test cases written , you can add run these as a pre-build activity and enjoy.
  • This technique ensures quality and believe me it decreases schedule overruns.


General misconceptions.

When we(our team) started adopting this technique there were lot of people who were reluctant to adopt this saying it" ...adds to more time" , "cost and the project schedule will get disturbed..."."My code is very complex, it cannot be tested automatically." bla...bla...bla.

I will say all these are just excuses , implement TDD once and I am sure you are going to like it.

Is TDD good for all project types?

I won't implement TDD when:
  • I am working for an internal project
  • I have not informed client that we are going to implement TDD
  • I am not added unit testing in my project schedule
  • When the project is for a small duration
  • If automated testing will be carried
  • Even if a project has very detailed design doc available you can go with TDD , that won't cause any harm.

How can I implement Test Driven Development in .Net Projects?

We implemented TDD in our project using visual studio unit tests.You can also use NUnit for this.
Following articles give you a detailed overview using NUnit.


No comments:

Post a Comment