Code coverage percentage is determined by Code Covered and Executed/Code Covered (Executed and Not Executed). By increasing the code covered and executed or decreasing the total code covered, the code coverage percentage is increased.
The following tips may be useful in helping you increase coverage of your code.
Review Test Cases
- Ensure the tests you are running mirror how the code is to be used. Considering all cases can lead to more tests, but also more coverage.
- Create tests for each use case, as well as corner cases. Avoid repeated testing of code already tested.
- Use utilities to automate the testing process. Investigate tools that can read your source code and identify inputs, outputs, and program flow to develop relevant tests.
Remove Dead or Redundant Code
- Getting rid of code that does not need to be tested will decrease total code and increase coverage.
- To find dead code, inspect the sections of code that are not covered after a couple of testing runs and determine if this code is really needed.
- To find redundant code, you may need a tool to search for repeating blocks. Then determine if the code blocks can be consolidated.