Why do I write unit tests?


To answer this question, I will start with a little story about a woodcutter.

Once upon a time, a very strong woodcutter asked for a job in a timber merchant and he got it. The first day, the woodcutter brought 18 trees.

“Congratulations,” the boss said. “Go on that way!”

Very motivated by the boss words, the woodcutter tried harder the next day, but he could only bring 15 trees. The third day he tried even harder, but he could only bring 10 trees. Day after day he was bringing less and less trees.

“I must be losing my strength”, the woodcutter thought. He went to the boss and apologized, saying that he could not understand what was going on.

“When was the last time you sharpened your axe?” the boss asked.

“Sharpen? I had no time to sharpen my axe. I have been very busy trying to cut trees…”

The people who know me would probably say that I do unit testing because of the sheer joy of it. Well, they are only partially true. There are other reasons, too :).

The first and most important reason I write unit tests is because I don’t like to test my code. Confused? Don’t be! The truth is that I don’t like to test any code more than once. That’s the reason why I’m making sure I don’t have to do it, if not necessary. I write my code in a such way that I can automate the testing process and let the computer do the testing for me.

Another reason is that, these days, I usually work in C#, Java and C++ mainly on the back-end and mobile. To run my applications I usually need to start a server, deploy to emulator or at least load some resources. Even if I do it right, it takes a few seconds for my application to start up; then it takes forever to navigate to a particular location of the application to actually start my testing scenario. When I get there, I notice, for example, that the comparison operation is wrong and now I must go back to the code to correct that, compile the app and start again. As it turns out, if my environment is set up correctly, it is actually easier to write the unit test then running my application and going through this whole process.

Abe Lincoln has a great quote that perfectly explains my reasons to “waste” time on setting up the environment and write unit tests:

“If I had six hours to chop down a tree, I’d spend the first four hours sharpening the axe.”

If you found this post useful, please feel free to share and give it a LIKE , so others can find it too! Also if you want more content like this please hit the FOLLOW button. Thanks 😊

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s