Introduction
As developers, we frequently hear QAs, BAs, or POs saying, "How come you didn't catch such an obvious bug?!". That is not pleasant to hear, and as developers, we always want our user stories to pass the QA stage in one go. You may employ multiple practices at the team level, such as having a peer perform a round of testing before forwarding it to the QA stage. It can be difficult at times as your colleagues may be busy with their work. Unit testing your code is another approach you should consider (consider).
What is a unit test?
Unit testing is the software testing technique in which individual statements or functions of a software application are tested in isolation from the rest of the application to ensure they operate as intended.
Unit tests are similar to writing functions that will call or use part of the code that you want to test. The outcome of these tests will either be a success or failure, validating whether your code is doing what is expected.
When reading about unit tests, you frequently find references to code coverage or simply coverage. This term indicates what percentage of the function you wrote is covered by the tests. It is worth noting that major programming languages like Javascript, Java, or Kotlin allow you to do so.
There are different types of code coverage.
Why should we write unit tests?
Writing unit tests will benefit you in a variety of ways, in addition to allowing you to capture bugs before submitting your story to QA.
1. It assists in determining what to refactor
It can be difficult to identify which part of your code to move to a new function and which to keep in the current function.
While writing unit tests, you may come across different scenarios to cover that are outside the function's scope.
This indicates that the code producing these scenarios should be relocated to a new function. This new function can be unit tested on its own.
In addition to assisting you in making your code more modular, unit tests encourage you to write cleaner code. Any messy code and logic will make writing tests more complex.
2. It makes you a better developer
Being able to refactor your code is a characteristic of a good developer, but there are other skills that unit testing may help you fine-tune. It not only helps in testing your code by yourself, but it also helps in testing your code early. This will increase the reliability of your code and demonstrate that your work provides a solid foundation.
It will encourage your team to build new features on top of yours. The tests also serve as a safeguard for future code changes, ensuring that existing functionality is not inadvertently broken. Any change in your code's logic will cause the tests to fail.
3. Helps to open the door to CI/CD
Unit tests are critical to a continuous integration and delivery (CI/CD) pipeline because they ensure that code changes can be deployed safely to production environments. Many teams integrate the process of running the unit tests on the pipeline and have the latter checked for coverage. When all tests are successful and the code coverage meets the benchmark (as agreed upon by the team), the feature is ready to be sent to staging and near-production environments.
4. Unit tests can act as a documentation
A well-written code will result in well-written and detailed unit tests. Reading these tests will be similar to learning about the function's capabilities and limits.
Writing tests for both positive and negative scenarios is a good practice.
The better the tests are written, the less documentation is required.
5. Saves time and money
This is a selling point to get your BA or PO onboard. Shifting left implies catching bugs earlier. The earlier bugs are found, the less expensive it is for the company. It is preferable to spare 20 minutes in writing tests
for a feature that has not yet been shipped to production rather than shutting down the live application for hours to debug and fix.
Note. Unit tests are a real value-add to a project. It facilitates the life of all actors across the chain.
Few important points to consider
1. It has a learning curve
At first, writing unit tests could seem complex. It has its syntax, which is dependent on the framework being used. Since different programming techniques, such as API calls and async functions, are used to write programs, writing tests that cover all of these involves a serious learning curve. As with anything, learning it will take some time.
2. Writing tests are sometimes time-consuming
As mentioned earlier, there are different types of code coverage, like line and branch coverage. Making sure that your tests cover important aspects of your functions/codes is a good practice.
As a result, you would have to write as many tests as required, which very often takes time.
More importantly, each unit test should test only one scenario, for example, a test only for the if part and another for the else part. This makes it simpler to spot and fix issues when they arise. This also requires time.
3. Consult the business before implementing unit tests in your project.
The business could be cautious about including this effort in the story's estimation since they do not fully comprehend the testing aspects of a developer's job. However, it is crucial to emphasize that testing is a necessary part of the job.
We cannot consider our job complete until we are confident that our story is ready to be shipped with minimum QA intervention.
Conclusion
To sum up, unit testing is essential for any software developer aiming to create robust and reliable applications. Unit testing has many advantages, including identifying bugs early in the development cycle and reducing the risk of regression. Moreover, unit testing can also make you a better developer by enhancing the quality of your code, your refactoring abilities, and the overall layout of your project.
Note. that 100% coverage is not always necessary, but unit tests should at least cover important aspects of your code. (This is a typical interview question.)
When implementing unit tests, it is important to remember a few things, including the learning curve, the amount of time required to write them, and the business decision. Incorporating unit testing into your development workflow with these factors in mind can lead to better code, more confident releases, and, ultimately, happier users.