5 Reasons Why Unit Testing is fun–and Important

Yeah, you read that right. I may be weird, but I do think that unit testing is fun. It is also very important. I didn’t know much about Unit testing when I came out of school. I think colleges need to have a class dedicated to it. But anyway, I didn’t do much of it at my previous job since I was developing the Siebel CRM. Since Siebel is an Oracle product, it involves mostly configuration and customization using built components. We did some integration testing by building any new components into a local copy and making sure it worked against a local db before checking in any changes but that was it. Now, at my new position, I find myself being the major proponent of unit testing. The other developer on my team is fresh out of college practically and as with me the art of unit testing is lost on him.

I took it upon myself to introduce and spearhead the unit testing effort for our current project. What I have found is the more that I write unit tests, the more I like it. As the title mentions, I’m starting to have fun with it. Every new programmer needs to understand how to unit test their code, and these are the reasons why it’s worthwhile.

  1. Instant Gratification: Unit testing allows you to execute your code right away and know that it works. This is a very powerful tool. One of the reasons I enjoy programming is seeing the results of what I built. Unit testing allows you to instantly try out your code and see it working.
  2. Code Against Your API While or Before it is Built: Unit testing gives you the chance to code against your API/class while you build it, or in the case of TDD, before you build it. Your tests then become a template for future classes that use the API. They will interact with it the same way that a client class will.
  3. Leads to a Better Design: Since unit testing should start very early in the process of writing code, it can help you to design an API. Since the tests will become templates as to how to use the class under test, you can get a feel for how natural the API feels. This ultimately leads to a better design and helps you to choose good names for methods and also to determine which methods are really needed.
  4. Understand How Your Code Works: Unit tests really work out your code and tell you how your code will act under many different scenarios. Whether the tests pass or fail, you will learn something about how your code executes.
  5. Confidence in Your Code: You may have heard the phrase “Refactor without fear” and unit testing gives you this capability. This means that you can make changes to your code and instantly know whether those changes have broken it. When you have a good baseline of tests, you know your code works and you don’t have to be afraid of changes to it in the future.

For any new programmers (or programmers new to unit testing), I highly recommend the book The Art of Unit Testing With Examples in .NET by Roy Osherove. You can find it on Amazon here. This is one of the only and arguably the best book about unit testing ever written. I cannot say enough about it and it is a must-read.

I hope this post has shown you why I have come to enjoy and champion unit testing in my current position and hope this moves some other young programmers to do the same.

Print | posted on Wednesday, September 7, 2011 12:22 AM


# re: 5 Reasons Why Unit Testing is fun–and Important

Left by Samuel Langlois at 9/7/2011 11:15 AM
Gravatar Nice post! Out of curiosity, do you use TDD at your work place? What is your opinion about it?

# re: 5 Reasons Why Unit Testing is fun–and Important

Left by Justin Boyer at 9/7/2011 10:47 PM
Gravatar @samuel,

My current company does not officially follow TDD strictly. I understand what TDD has to offer and use the principles in my unit tests, but I do not strictly follow the Red-Green-Refactor methodology. When creating core business logic or an API, I do try to write tests first to help understand how it will function. If I don't do that I write them immediately after I write the code under test.

# re: 5 Reasons Why Unit Testing is fun–and Important

Left by Gourav Tiwari at 12/20/2011 1:25 PM
Gravatar Great post and love to see that even when your organization doesn't allow you, you are unit-testing your code. I think you already know what TDD, is but may be BDD with Cucumber would give you more control on features and also as even BA can write test plans in BDD, it should be easy to convince top guns in your company on automated testing.

# re: 5 Reasons Why Unit Testing is fun–and Important

Left by Joe at 6/22/2014 3:16 AM
Gravatar thanks for the answer

Your comment:


Copyright © jboyer

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski