Unit test – purist versus practical


Whenever you ask a question on unit testing in a forum, there is always that one person whose only job is to point out what you are doing is not unit testing but integration testing. It is important to know the difference but it is more important to not lose sight of the goal, ensuring a reliable and a bug-free application. Also, you need to adopt a terminology that works for you and your team, rather than what purists think or say.

In absolute terms, if a test depends on anything that is not in your control, it is not a unit test. For example, if a method that you are testing uses a public function, a method from an included library, database or an external API, it is not a unit test but an integration test. For a test to qualify as a unit test, you need to mock all these dependencies and get them under your control, only then you can claim your test as a unit test. Now that we have the purists happy, let us move to a more practical worldview.

When a regular joe developer refers to a test as a unit test, what she means is, she is trying to test a functionality in a massive gnarly application that she thinks is a small independent unit. This unit might have some components that are not under her control. Instead of debating whether she is unit testing or integration testing, a better discussion is trying to figure out what is the intention of the test, what needs to be controlled/mocked and not. Helping her to figure this out and achieving this will add more value than debating whether a test is a unit test or integration test.

Fighting change

Ninja Fighter Sword

In my new workplace, I was assigned a brand new shiny MacBook. My first reaction was to ask for an Ubuntu laptop. My brain justified by giving several reasons, it is developer friendly, software that you run on a server runs as is in Ubuntu, etc. I was almost about to voice this opinion, but system two took over from system 1. It started asking questions along the lines of is this the reason you want an Ubuntu machine or are you just trying to avoid the unfamiliar? You have not used a Mac before and are you trying to run away from something new? The two debated for some time and settled on giving the Mac a chance. So far, the experience has been, and I am learning some cool things like using gestures for different actions, etc.

Whenever something new and unfamiliar comes across, the instinct for most of us is to fight it. Take a step back, analyze whether this is the primal part of the brain trying to fight against the unfamiliar or you have a valid reason not to.

Altruism FTW

Have you observed the way Google maps asks for info about local joints and places? They word it in such a manner that it sounds like you are helping others to make an informed decision along the lines of “Give us more info to help others”. What they are doing in effect is appealing to the altruism in all of us to generate more info to make their product better.

I think this is a great way to ask for more data in this world of user generated content. Instead of asking to review a restaurant how about wording it as “Review this place so that others can discover great food”. Instead of asking people to rate your app how about saying “Help your friends discover the app on playstore, rate us”. It would be interesting to A/B test this and see the result.