Unit Testing – The good, bad & ugly

Unit testing is common to all the codes that we write. We all do it, so it’s important to understand the most “looked forward” (sarcasm) aspect of development. However overlooked and underestimated, Unit Testing forms the most important part of any development cycle and hence it’s worth delving into. So let’s begin.

So the big question at hand is, what is Unit Testing?

A unit test is an automated piece of code that invokes a unit of work in the system and then checks a single assumption about the behavior of that unit of work.”

–       Roy Osherove

A fair definition but let’s break it down. I want to focus on two important constituents of this definition:

invokes a unit of work”

Here, a unit of work is a single logical use case of the system irrespective of its size. It could span a method, multiple methods, a class or even classes. All that matters is this combination achieves a single logical purpose.

checks a single assumption about the behavior of that unit of work”

Overloaded-Tricycle-Funny-Transportation-Picture

A unit test case works with something specific about a functionality, it doesn’t cover it entirely. It’s a bad practice to create something called testAllThings and call every method in the namespace.

Another important aspect of a unit test is that it works with the use case in isolation.

Which means it should not depend or be bothered by any external entity. So changing another part of the codebase should not affect a unit test case. Simply put, if you have a unit test case that doesn’t run without a proper setup or malfunctions in the presence of one, you haven’t written a unit test case.

Now before we dive further, lets first understand the need for a unit test. Why do we put in at least half as much time as we devote in development, on unit tests?

It helps us verify a functionality” one might say and I won’t disagree, it is an important thing to know what you have coded actually works. But however much we say it, one thing always bugs us.

IT SLOWS US DOWN

speedbreakersignage-300x280

Same time could have been spent more productively in other activities, right? Wrong. And I’m not saying this out of thin air. According to a press release by TypeMock, which makes unit testing products, unit testing saves about 70-80% time in debugging. So in the long run, it saves time. Go figure!

Added to that, unit testing offers great feedback on the modularity and design of your code. Whenever you’re struggling to work with a class and method in order to test it, there is a design problem. Hence it’s not only about making testing easier, but also making the code robust and easier to maintain.

Now that we know what unit testing is and why one should do unit testing, let’s look at some good practices:

Good Practices

ya

  • Test cases should be small and isolated. As mentioned above, they should test something specific about the code.
  • Try keeping test to assertion ratio near to 1. It makes it easy to identify any assertion which has failed. Having multiple assertions can make it cumbersome to verify which assertion went rogue.
  • Always avoid test interdependence. Each test case should handle their own build up and tear down. Test runners don’t generally run tests in any specified order. Thus, we can not assume anything based on the order in which we write the cases.
  • A test case approaches the behavioral aspect of the code. So it should be easily comprehensible in the sense that what we are testing and what to do in case of failure.
  • Mock as little as needed. Too many fakes create fragile tests which break when they code undergoes changes in production.
  • Avoid mocking chatty interfaces. Any change in order of calling may break the test.
  • It should be independent of external factors. You must not require a setup to run a unit test.
  • We must maintain a clear naming convention in writing unit tests. This simply makes our code more comprehensible.
  • While working on a continuous integration build, always add the unit test cases to the build so that whenever one test case fails, the whole build fails thus leaving no exemptions.

Bad Practices

naah

  • Using nondeterministic factors in Codebase: Such scenarios are difficult to test especially when they don’t reproduce/hold a constant value on each rerun. Eg Using Time as an authentication measure in code which would vary at each moment and also in different time zones.
  • Using side-effecting methods: Testing these could be as difficult as testing methods with a nondeterministic factor in them. Poorly designed untestable code introduce unwarranted complexities.
  • Impurity is highly toxic: Method depending upon nondeterministic or side-effecting methods themselves become the same kind and in the end complicate the entire codebase. Considering the effect it may have on a real-life complex application, we may find ourselves trapped in codebase filled with antipatterns, secret dependencies and all sorts of unpleasant scenarios.
  • Mocking interfaces and classes outside the codebase: If you don’t fully understand the proper usage of an inteface ,you should avoid mocking it. Rather you should wrap interaction with external api classes into specially created interfaces for the same purpose.
  • Working with the mutable global state: Method depending upon mutable global state not only requires you to consider the current value but also be updated about other code that may have altered its value earlier. Hence its advisable to avoid them.

So this was a brief version of Good, Bad and Ugly of Unit Testing, hope it was of some help.

References :

Art of Unit Testing

Code Better

Methods & Tools

Stackify

 

Pasted image at 2017_11_27 04_17 PM

Written by 

Ayush Prashar is a software consultant having more than 0.5 years of experience. He is familiar with programming languages such as Java, Scala, C, C++ and he is currently working on reactive technologies like Lagom, Akka, Spark, and Kafka. His hobbies include playing table tennis, basketball, watching TV series and football.

Leave a Reply

%d bloggers like this: