Unit Testing — Why is it a must?

The answer is ABSOLUTELY.

And just as the saying goes, “an apple a day, keeps the doctor away”, also having a solid understanding and implementation of unit testing could minimize our bug count and help us and others during and after the development process.

What is Unit testing?Well, we could elaborate extensively on the definition of unit testing, but the easiest approach is to look at the name.

Yes, that’s it, we test our code in separate units and ensure that the correct inputs/parameters yield correct outputs and vice versa.

 Unit testing goes hand in hand with writing testable code, and testable code should be organized and as independent as possible.

To visualize this, imagine having a fur ball after your cat is done playing with it and trying to make sure that every thread is intact.

It’s a nightmare, right?Now imagine if these threads are organized and separate from one another, this would make the process so much easier.

So what are the benefits of implementing unit testing?Unit testing has many advantages, the most obvious ones — aside from what we mentioned above — being:1- It helps us validate our work and making sure our code functions as expected and there are no major issues lurking in the future.

  in other words “correct inputs = correct outputs”.

2- Writing test cases helps anyone who might handle the code after us understand what is it we are trying to achieve and how the acceptable flow of our logic.

3- Writing a testable code also ensures a smooth refactoring of our code, including adding new features in our code without starting the Armageddon.

How would we have a testable code?We said before that “Unit testing goes hand in hand with writing testable code”.

So the process, in summary, is that the building blocks of our code -classes- would be as separate from one another as possible.

And if they do have dependencies, we should use what we call “Dependency Injection”.

Dependency Injection is the process of feeding our class the dependency it needs at the time of the initialization.

 So in swift for example, it would look something like this:class Bicycle { var wheel: Wheel?.init(wheel: Wheel) { self.

wheel = wheel } func startPeddling() { wheel?.

startRolling() }}class Wheel { func startRolling() { // Bike moves }}What’s the worst that could happen?Well, let’s say you’re working on an e-commerce app.

Now you sure have functionality that your user can add items to his cart.

So you have a function to which you pass a list of items and it does some calculations and then returns his order receipt.

Something like this:func generateOrderReceipt(items: [Item]) -> Receipt { //some calculations return receipt}It would only make sense that the value of the receipt — after adding shipping or taxing- is more than the collective sum of the products, right?.Or you would lose money!Now let’s say you have decided to add a coupon feature:func addCoupon(coupon: Coupon) -> Receipt { //some calculations return receipt}Here imagine if your user discovers at one case that he now has to pay more money after adding a coupon!.So the reasonable conclusion is that it should be less than the value of the original receipt.

Even though the parameters/inputs are correct, the return/yield is completely wrong.

Conclusion:Unit testing and writing testable code should not be an option, it should be a requirement.

It helps us developers as well as our users.

Of course, you might be a coding guru that makes very few mistakes.

Nevertheless, you still make them, and you don’t know what your next mistake could be or how much it would cost you.

Happy coding!.

. More details

Leave a Reply