← Go back to all stories

What are the advantages of unit testing?

by | Jul 30, 2021 | Productivity

A unit test is a way of testing a unit – the smallest piece of code that can be logically isolated in a system. In most programming languages, that is a function, a subroutine, a method or a property. 

Many times when we start in the world of software development we forget how important unit testing is, and we skip it.

Years ago when I started as a software developer, I believed that it was enough to code and do some manual tests, for a long time I worked that way, but later when they requested a change, I only performed the tests for my changes and I did not verify if affected the functionality, so many occasions they reported some errors that were not in other modules or in the modified one; correcting these errors sometimes took longer than expected, so I should saved some time by doing it from the beginning.

So let me tell you why doing this test is helpful:

  • Agile work: It allows you to detect errors that would make you rewrite code or correct the issue without having to do everything from the beginning, before merging your changes.
  • Code quality: You get higher quality code by being able to detect and correct errors before merging. 
  • It’s easier to make changes: Unit tests must be repeatable, this allows you to make changes to your code easier and can perform the tests again to check if something was broken.
  • Integration: Many models are used in different modules or functionalities, so when making changes to a model, function or some other part of the code, it can be verified if the change breaks something in all the places where it is used.
  • The debugging process: When a  bug is found in the code, it is only necessary to break down the piece of code tested. This is one of the main reasons unit tests are done in small pieces of code, it simplifies the task of finding and solving the problem.
  • The design: If you make your test before writing the code it is much easier to know in advance what to do to fulfill the requirements of the design.
  • Reduce cost: If you write the code in the right way from the beginning, you can detect errors faster and optimize it avoiding checking the code multiple times before delivering it.

Unit tests by themselves are not perfect and running them doesn’t mean some errors would appear, but this is only one of the different tests that must be carried out.

  • Fifteen minutes of coding a unit test can save you hours of work

And you, are you running unit tests?

LISTEN