Unit Testing is Not a “Figure It Out Later”

Technorati Tags: ,
del.icio.us Tags: ,

If you buy into the excuse that “We don’t have enough resources or time to start Unit Testing now”, then you buy into the hack it now, fix it later approach and I don’t respect that.  Unit Testing should be something you learn NOW especially when you have a fresh opportunity do so such as a on a new web project that’s being coded from scratch.  There are not many projects which you can start fresh on and include Unit Testing like this, and when you have that chance, do not let it pass away.  Businesses find it easy to keep adding features to produce scope creep but they can’t fit in extra time for adequate testing which I feel is just appalling.

If someone tells you to wait and do it later on the “next project”, it’s really not their decision.  Unit testing is for programmers and is done while you code.  It’s a decision done by the programmer because he/she cares about better code and less bugs for the good of the team, the business, and its users. 

Unit testing is not a big of a deal.  All you are doing is creating very small little tests which only take a couple minutes to code up per method you are testing.  If I take and create 5 controller methods today for my MVC Web project for example then I expect to take 10 minutes out of my day to create 5 small code snippets for testing those methods.  To me, that is not a big deal.  Even if I took 20-30 minutes, it’s not a big deal considering all the time I spend on bullshit with other aspects (email, etc.) in the office.  I know that in the end I’ll be able to run these tests and have them tell me where my code breaks when introducing new code in the future. 

Now why would I not want this added comfort and savings in the future when I’ll have less debugging because my tests tell me if code fails instead of me manually debugging code that I have no clue why it’s failing?  It seems to me Unit Testing is something that’s obviously an item that should be required of every programmer on any team.  When we have tests we can run, we can see where code breaks immediately instead of spending 20-30 minutes in some cases doing the contrary…moving through the debugger finding bug after bug and fixing bug after bug and never knowing how many MORE bugs there are to fix.  That’s going backwards.  Leaving testing to last is ignorance.  So I promised myself this time through I’m adding unit tests to my new MVC project.  My mentality is not one which can allow things to just go array and I can’t stand when process is backwards.

Testing last or not testing at all to me asks the question why are we coding in the first place if we are doing a half assed job at it?  Not testing (QA, usability, acceptance, and unit) is simply saying we don’t give a shit about quality and the business enough to do this.  And that we enjoy or are willing to put up with constant fixing of code.  I know that personally I did not go into IT with the expectation to keep fixing bugs, I like spending my time on more important tasks for the business.

And who wants to sit all day and talk to QA about bugs that we probably could have caught via Unit Testing?  QA's job is not to catch code bugs, it’s to test whether the software has met the business requirements.  It is our job as a programmer to add that added layer of testing before it gets to QA.  And why would we not want to help ourselves by testing?  Why would we not want to save ourselves time through the development process?  It’s just a done deal to me and completely stupid not to unit test.  It’s not hard to write a few lines of code to build confidence and trap errors which would have taken us much longer and surface many more surprises when not doing so during any project and it’s just beyond my comprehension why developers or management fight this very topic. 

And remember, Unit testing is very misunderstood.  People think it takes a lot of time to invest in doing so but it’s very small in comparison to the cost and time savings reaped when it’s done concurrently in development as you go.

A couple of books I’m currently reading and highly recommend.  They are very short reads so you can get to the points/techniques quickly:

Pragmatic Unit Testing in C# with NUnit, 2nd Edition

ISBN: 0977616673
ISBN-13: 9780977616671

The Art of Unit Testing: with Examples in .NET

ISBN: 1933988274
ISBN-13: 9781933988276

Today I promise that “I will never write an application again without unit tests”, and you should too. 

If you care about code quality, business process, the business you work for, and respect for yourself as a quality developer, less bugs for you and your end users, and ultimately getting shit done and going home sooner because you had less bugs today to work on, therefore I can now spend more time with the family, then you’ll promise yourself the very same thing starting today.


Print | posted on Saturday, June 06, 2009 1:00 AM

Comments on this post

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
We started a project last year and 2,500 unit tests later, I don't think I could ever go back. Stuff just works better and *stays* working. I feel naked without unit tests now.

Now if only I could do integration tests in Silverlight...
Left by Mike on Jun 06, 2009 2:20 PM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
"If you care about code quality, business process, the business you work for, and respect for yourself as a quality developer, less bugs for you and your end users, and ultimately getting shit done and going home sooner because you had less bugs today to work on, therefore I can now spend more time with the family, then you’ll promise yourself the very same thing starting today."

AMEN!!!!!! I loved your article, but the last lines were just brilliant!! I printed these on the wall, just to make me think again, when I think that I'll don't have time for doing unit tests...

Thank you, these words are like spoken out of my mind.

PS.. I printed out a few copies of those last words, to put into offices of few friends of mine...!!!
Left by Kjartan Óason on Jun 07, 2009 2:06 AM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
@Kjartan

I am glad this got you pumped up. We all need to be motivated to get off our ass and care about quality of code, maintenance, and standards in a lot of departments today.
Left by espresso on Jun 07, 2009 3:27 PM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
@Mike

Yea, and I don't get why some managers don't see the light. Once you've done something well and in a controlled manor who would want to go back.
Left by espresso on Jun 07, 2009 3:28 PM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
@Mike

Yea, and I don't get why some managers don't see the light. Once you've done something well and in a controlled manor who would want to go back.
Left by espresso on Jun 07, 2009 3:29 PM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
Great article and I agree wholeheartedly with all your points. Once I started unit testing I find it almost impossible to work without unit tests - even when I'm writing a quick app to perform some one off task! I've become a TDD developer so that helps a lot in ensuring that your code is designed to be testable but of course the unit test authoring doesn't stop there.
Left by Charles Feduke on Jun 08, 2009 9:59 AM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
Rebuttal article compressed: Unit testing is INDEED difficult to learn, but not to do once you've learned it. Cost of learning unit testing and incremental cost of doing unit testing are hugely different things; you've ignored/downplayed the cost of learning. Shame.
Left by Peter on Jun 10, 2009 3:35 PM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
@Peter

"Rebuttal article compressed: Unit testing is INDEED difficult to learn, but not to do once you've learned it. Cost of learning unit testing and incremental cost of doing unit testing are hugely different things; you've ignored/downplayed the cost of learning. Shame."

Everything we do as developers has a learning curve. But does this mean we sacrifice future quality and testing because of this initial cost? I'd rather take in the cost of each developer learning unit testing over time than the cost to fix x bugs later downstream which will end up costing the business much more.
Left by Dave Schinkel on Jun 10, 2009 3:50 PM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
I'm saying, you mixed up an argument about learning how to build testable code with a response saying how writing unit tests doesn't slow you down.


People think it takes a lot of time to invest in doing so but it’s very small in comparison to the cost and time savings reaped when it’s done concurrently in development as you go.


You're not alone, everyone else ignores or downplays the learning cost. That doesn't make it right.

All I'm saying is: don't take the saber-rattling approach and tell people who aren't unit testing that it saves them time while simultaneously ignoring the incredible ramp-up cost. It's really difficult to get started doing TDD, and articles written in this style only serve to discourage people who can't figure out why it's so much harder than the article says it should be.
Left by Peter on Jun 11, 2009 1:59 PM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
@Peter

and I --still-- stand by my reply that if done in parallel every day when you code, it's not that bad. The ramp up costs I still say are not that bad when you do it as a team together.
Left by Dave Schinkel on Jun 20, 2009 7:14 AM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
That is a nice book.
Left by Stop Dreaming Start Action on Jun 21, 2009 11:15 PM

# re: Unit Testing is Not a “Figure It Out Later”

Requesting Gravatar...
article compressed: Unit testing is INDEED difficult to learn, but not to do once you've learned it. Cost of learning unit testing and incremental cost of doing unit testing are hugely different things
Left by kampanye on Aug 04, 2009 1:34 PM

Your comment:

 (will show your gravatar)
 
Please add 8 and 6 and type the answer here: