Code coverage is a measurement to find how much of the developed code is executed and is an essential metric of an application. The more thorough the testing, the lesser the chances of bugs.
Code coverage can be considered as an indirect measure of quality, though not necessarily a good metric of how well you are testing your code. Just because your tests are going through all the lines of code it doesn’t mean they’re testing every possible scenario, and thus your code can still be faulty. Code coverage is still important, because it gives you an idea of how much you’re testing - or not testing. At the same time, it’s very important to know what and how to test as well.
It would be measured during manual application runs, as well as integrated into automated regression tests for continuous coverage analysis. It gives us an important insight to how effective the tests are, and what parts of the code are executed.
Code coverage analysis shows you which source files, functions and lines of code have been covered by tests, are untested or only partially tested. This also helps a developer to know the dead code (if any) that is not in use and eliminate it or a Quality Analyst to know what additional tests need to be created and how to improve the existing tests. One can look at the code coverage report and find out specific areas of code which are not exercised by the tests. The tests can be unit tests, nightly automated tests or the team performing functional testing manually. It gives you a final report of the number of lines/blocks/statements of code which is executed with the packaged information which gives you a clear direction to focus on automating or performing exploratory testing.
With more projects adopting Agile development methodologies with smaller release cycles and lesser time for QA, this direction is crucial for the team to perform more tests. Many development teams have a good code coverage as part of its exit criteria.
This sample code coverage report is generated using code coverage tool EMMA.
There are two types of code coverage tools, one which adds statements to the source code and require to recompile the source code. And two, which instruments the byte code either before or while running it.
The following are some code coverage tools for various programming languages:
- Java: Cobertura and JaCoCo
- PHP: Clover and PHPUnit
- Python: coverage.py
- Ruby: SimpleCov
- Scala: Scoverage and Cobertura
EMMA was the popular tool used earlier for its support for large-scale enterprise software development. However, now we see EMMA replaced by JaCoCo, which is a free code coverage library for Java. Various code coverage tools for different programming languages can be found here.
The decision on which code coverage tool can be used for your application depends on various factors. Here is a great link that compares code coverage tools under various parameters.
To find our more about code coverage or how Innominds can help you with various questions or projects, please contact us today.