Unit testing is not just a best practice

Sameh Muhammed - Oct 18 '22 - - Dev Community

Introduction

"If it works, don't touch it", for sure you heard this say in one place or another when you are working in software industry, and off course you knew that this say applicable on any area of software that has bad implementation which makes any change in this place causes unpredictable errors, so no one can work with. and indeed you know that writing unit test is solution for this kind of situations, but deadlines, market and customer needs always in your back so you think you don't have time for such kind of welfare. and most of developers thought that writing test cases is just a best practice they can follow in they free time, but here we discuss 6 points about writing unit test that may affect your whole career path.

Image description

First, What is Unit Test?

Unit testing is process which you are testing a piece of code (function/class) in isolation of any other part of your software, if there any integration/communication between any other part of program it's not Unit Test.

How unit test improves your skillset

- Providing you with safety net

When you write test cases for your functionality, you build a safety net for yourself if something goes wrong, this safety net will saves you and notify you that there is something not correct, so you will make your changes in more comfort and confident way and this will reduce the chances of bugs in production code and also save your time in future changes you don't need to waste your time in testing all things manually.

Image description

- Giving you fast feedback loop

It's very important to find bugs and errors in your software as faster as you can, each software has its' quality control team and this team has its' own process and priority for testing, so your code may take while to be tested and maybe they figure some errors in your code at time of deadline, so you are now stuck and have very narrow window to solve bugs or even worst your code may not be included in this deployment and maybe this reflected to your performance report, so unit testing providing fast feedback to you, so you can find and solve your bugs early.

Image description

- Improves your software performance

Executing test cases before pushing your code or deploying it to any environment is essential to make sure that everything is ok, so as big your software is as test cases will be bigger also, and this may affect deploying time of your software, there is a role that says "any test case you write must not exceed 1 millisecond of execution time", so if there any test case that exceed 1ms that is a sign of performance issue you should investigate, maybe this functionality does a lot of things or maybe there is a better algorithm for this problem.

Image description

- Providing you with high quality code

Most of developers writing code for end user only not for another developers to use, and with a lot of frameworks that facilitates our job by instantiating our classes and injecting our dependencies, this may lead to less readable code with tight coupling dependencies. When you try to use your classes manually in your test cases you will get a taste of feedback of how this code look like from the other side, is there a long parameter list that you have to reduce, or is there a lot of dependencies you put there and you can't work with your class without them, or is there a chain of dependencies that you have to create to use your class and this prevent you from writing test cases at all, all of this and more can be discovered from using your code, and by fixing and refactoring your code this leads to more readable and reusable code.

Image description

- It's essential part of automate deployments

Most software unicorn companies(Amazon,Google..etc) hits they production environment with new code daily, and this leads to more customer satisfaction and provides you with market competition also, but his can not be done without making sure that the new code is free from bugs and errors, and in order to achieve multiple deployments per day you have to reduce human interaction with this process and give this responsibility to your automation deployment pipeline which includes various kinds of testing paradigms which indeed includes unit test.

Image description

- Makes you good developer

When you write unit test and gain all that experience from previous benefits, you are now armed with skillset that will help you with any new project or task, you are now know exactly how you should start, know how you can enhance software functionalities, where are pitfall points you should solve or avoid, which will improve your productivity and toolbox at the end.

Image description

Conclusion

As we discussed writing unit test is not just a best practice is way beyond that and may affect your entire career path, and it doesn't waste your time but it buys you more time in future days of your software.

IF YOU LIKED THE POST, THEN YOU CAN SUPPPORT SUCH CONTENT WITH A CUP OF COFFEE, THANKS IN ADVANCE.

Buy Me A Coffee

. . . . . . . . . . . . . . . . . . . . . . . . . . . .