Monitoring and improving test coverage of applications
March 6, 2023
In our last post, we were discussing what to do when you encounter an application with 0 or low test coverage. Certainly, the most important step is to educate and get clients and the team on board, without this step everything we do further on is futile. But what should we do after we got everyone on board? How do we actually detect missing coverage and increase it in order to improve system quality and sleep more peacefully?
The initial step should always be to diagnose and assess how bad or good the situation is. Inefficient approach would be to manually go over test classes and try to figure out which test classes or which test cases are missing. But why not work smarter and automate the process by leveraging already existing tools? One of the recommended tools that is used in the Java ecosystem is called Jacoco - an open-source library for reporting code coverage.
In the sample below you can see a code coverage report for our sample project.
We can clearly see that components for calculating Price and VAT have not been tested properly. This situation is dangerous because there is no quality control or verification for crucial components such as PriceCalculator. A small mistake in such services can result in not charging enough money for products and thus lowering our profits or overcharging and damaging our hard-earned reputation. The probability of both of those errors can be significantly reduced by automated testing.
Tackling the issue
After assessing the test coverage, we gained a clear overview of the state our project is in. Now it is time to acknowledge the result and think of a battle plan to increase your test coverage. In SIGIT Agency we tackle the issue in three ways:
Prioritize test coverage
Prioritizing test coverage means that we stop doing any other work and focus only on writing tests and increasing the coverage. This approach is ideal in a scenario where you have time to focus on tests when there is less or no work planned for some period of time.
Write missing tests as you go
While implementing new features you will most likely interact in some way with existing code. It is imperative to add missing tests for existing code while adding new functionalities. This approach will slow down the pace of the new feature development but it will add much-needed quality assurance.
Depending on the report of code coverage it might make sense to prioritize writing the tests for business-critical functionalities – such as the PriceCalculator component in our example. The idea is to tackle tests of those crucial components immediately and for other components write tests as you go.
No matter which approach you choose, you should run diagnostics from time to time and see how the project is doing in terms of test coverage. Maybe the team has written all of the tests and you can resume with a normal development cycle if you happen to choose the first option. Or there is less workload and you can focus on ramping the code coverage up if you have chosen other approaches.
Whatever happened, you should strive to have a significant increase in test coverage between the last time you performed a full diagnostic. If there is not enough progress, an action plan should be evaluated and the writing of the tests should be prioritized.
In conclusion, monitoring and improving test coverage is a critical aspect of software development that ensures the quality of the system and reduces the likelihood of errors. By leveraging tools like Jacoco and adopting smart approaches to tackling the problem, developers can increase their test coverage and maintain a high level of quality assurance. It's important to remember that the process of improving test coverage is ongoing and requires periodic monitoring and evaluation. By making test coverage a priority and integrating it into the development process, developers can ensure that their systems are robust and reliable.