I've to admit it: I never wrote many tests for the code I wrote. Probably one of the reason is that the last time I wrote something from scratch, with enough time to convince the other members of the team to try a new approach, was before the TDD became known to the general audience. I only wrote some test for small part of the code, or to expose some bugs and later fix them.

But 2 months ago, I had to design from scratch a small web application: and again I didn't have the time to sit down, look around and try a TDD approach for it. So I ended up writing my Data Access Layer, my Business Layer and testing them trying them out with a console application that acted as a client for the DAL and the BL.

But while writing in my console application the code to call the DAL, I noticed that it was the same code that I would have written inside a unit test. The difference is that you cannot check automatically if the console application behave correctly, but you have to do it manually, stepping thorough the code and checking if the database is populated with the correct data. 

That was a small project so I went on with the console application approach, but I promised that my next project would have been developed using the TDD approach.

Two weeks ago I started a new project, and I decided to use this approach: I write my DAL and, instead of trying it with some code that will be thrown away, I test the functionalities inside a unit test. And it's even easier to work with because you can choose which test to run, you can have a coverage report on your library and, if the application is well designed, you can even test the UI or the Business Logic without hitting the database, using some mocking library.

So, if you want to find a practical reason to start writing unit tests think about this:

If you write any library (a DAL, some Business Logic, some general purpose utilities), you have to try it somehow. And to do it you have to write some code just for testing: so why don't you put the same code inside a unit testing project? With the same effort you will have "best-practice" testing procedure to improve a lot the quality of your code.

This was the motivation that worked for me. I hope it will convince also other people to write more tests for their code.

Technorati tags: ,

kick it on DotNetKicks.com