23
Jun

Unit testing is anti-productive

   Posted by: Swizec   in Uncategorized

Salish Mist
Image by ecstaticist via Flickr

Today I finally got my feet wet with something I’ve been meaning to try on for size ever since reading Clean Code, during my trip to Vienna last autumn; wow has it really been that long already, where’d winter and spring hide?

Anyway, so at about midnight last night I finally embarked on the process of coding up a new feature for Twitulater, or at least its server-side bits, after some prep work. Now since this is a rather mission critical system that I’d very much like to be somewhat reliable and rock-solid, unit testing seemed to be the way to go.

Downloading PHPUnit gave me some headaches, because ubuntu’s package managers fucked something up. Why is it I can’t install Pear if there are broken packages for something completely different? Then came the figuring out of how to actually do unit testing and I must say, it was incredibly simple. Of course I did have eight months’ time for the principles grokked from Clean Code to seep in, but still.

Mostly I was suprised to finally confirm that I have been doing test driven development (TDD for you acronym junkies) for a long while. Just in reverse. Whereas now, with automated unit tests, I first write a test, then the code, I used to write the code then test the hell out of it. With PHPUnit testing has become much less of a hassle – run a command, make sure everything works. Nice!

However, unit testing has one large drawback. It took me a solid six hours of coding to produce … nothing. I have in my possession now 200 lines of real code that doesn’t implement much other than making sure users exist and authenticating them, and a bunch of tests taking up a full … 170 lines of code. This is of course after some refactoring and whatnot, the original ratio was always that there were twice as many tests as there was actual code. Yummie.

Now I’m sure many of you will bash me for being such a dirty little blasphemer, but fuck it. Despite all the headaches saved with testing, despite making testing a lot simpler and quicker. It still took me SIX BLOODY HOURS to create some functionality that never used to take more than two hours to code and debug.  This is quite horrible.

And to top it all off, I can’t even be certain there are no bugs in my code! I’ve reached the exact same level of debugged code as I normally would, except that now I can automatically test for everything I could think of going wrong, whereas before I had to invest a bit of elbow grease.

Then again, I probably won’t be abandoning unit testing for mission-critical things any time soon. There is a sense of gratification seeing the tests pass, there is also a sense of “oh my god that was easier than eating a pie” when you run 20 tests and it takes ten seconds instead of ten minutes.

Perhaps most importantly, however, is that with automated unit tests you never forget to run a test. So I guess, in the end, it’s worth it spending those extra long hours to have code you can prove works like you think it should any time. Without spending a bunch of effort on making sure it does.

Unit testing is here to stay, even if it’s a bit of a pain in the arse.

Reblog this post [with Zemanta]
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • TwitThis
  • DZone

Tags: , , ,

If you liked this post you should follow me on Twitter,
or see the music I like on last.fm,
or perhaps leave a comment, I like comments,
or go do your job because I know you're slacking,
or go write a blog of your own,
or tweet about something interesting,
or go out and have some fresh air,
or find a girlfriend,
or a boyfriend,
or a manbearpig,
or for fuck's sake stop reading this already,
no?
This entry was posted on Tuesday, June 23rd, 2009 at 15:58 and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

7 comments so far

Terry
 1 

Your title is a bit extreme considering that the time savings for having automated unit tests add up substantially over time for long term code compared to having to manually test your code.
Unit testing is actually *very* productive when you look beyond the one time cost of writing a test before you write your code and after that the investment pays off in spades compared to having to repeat manual tests or reason about the code whenever you make a change to a complex or mission critical system.
Unit test aren’t recommended for quick throw away scripts that you believe won’t be reused though. Funny thing is a lot of code does end up getting reused again somehow so it’s not an easy call either.

June 23rd, 2009 at 18:14
Ben
 2 

Thing thing you are missing is when it comes to maintaining code. When something does break and you come along and fix it your unit tests make dammed sure that you fix it properly. You also then create another unit test to capture that issue.

Now im not a fanatical unit tester. I do write them but really only use them for ensuring things will still work later on. That said however they have saved myself from releasing broken code many times.

June 24th, 2009 at 08:51
 3 

Writing tests take time and they slow you down. But this can be a good thing: while writing tests you’re forced to think not just to code. The author of “Code Complete” observes that he wouldn’t trust a coder who never just sits thinking rather than coding.

June 24th, 2009 at 09:32
swizec
 4 

Yeah I guess you all make a good point. Unit tests aren’t for when you’re coding, they’re for two years later when you’re having problems.

Dunno, maybe I have to get used to them still, but right now they serve mostly to pull me out of the flow and keep jarring me to think about things I don’t feel like thinking about right then. I’m trying to solve a problem, being forced to also solve the problem of testing the solution is just … overkill.

June 24th, 2009 at 10:50
Pawel D
 5 

Plus it is especially important in languages with dynamic typing. As you never know whether you have just destroyed one of the “old” functionalities implementing “new” one. For simple project though, I would not bother with all that TDD (its not that I would not bother with unit testing at all!); for really complex ones, developed by tens of devs – its a must (at least from my experience).
Additionally it took you considerable amount of time but you should be quicker with writting unit tests as you gain experience (YES – writting good unit tests in short time is a skill!).

June 24th, 2009 at 12:02
Jeff
 6 

Your solo coding doesn’t scale.

June 24th, 2009 at 19:51
 7 

Perhaps you just experienced a steep learning curve.

You wrote you were used to practice “reverse TDD”, but how do you test then ? manually ?
Then it is test-driven, however, it cannot be called TDD.

Do you think you would have achieved more by writing the test last ?

June 26th, 2009 at 12:59

One Trackback/Ping

  1. Cthulhu and other crazies » Blog Archive » The ten pros and cons of unit testing    Aug 04 2009 / 1pm:

    [...] of you may remember mywriting about how unit testing is anti-productive and those of you I have to disapoint, I still dont’ think it’s the best productivity [...]

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment