Code coverage is a measure used in software testing to describe the degree to which the methods in a system have been tested. It is a useful measure to assure the quality of a set of tests, as opposed to directly reflecting the quality of the system under test.
Code coverage can help testers and developers to:
Discover methods and blocks of code that are not exercised by a set of tests
Create tests that increase code coverage
Quantify the overall code coverage of a system, which is one measure of quality
When a test method is executed, some of the methods that have been written by the application developers are invoked.
A block of code represents an interpreter operation; for example, an assignment statement, method call, property access, if instruction, and so on. A block can contain nested blocks, so a single instruction can contain multiple blocks.
A method consists of instructions, which in turn consist of blocks of code. For example, each of the following assignments correspond to a single block of code.
addr := "21 Somewhere Street";
addr := agent.addr1;
The following assignment requires a method to be executed before the assignment can be made. It corresponds to two blocks of code.
addr := agent.getAddress1();
All of the blocks of code may not be executed in a single execution of the method if it contains:
An if instruction with a condition that is not met
A return instruction that exits early from the method
In such cases, the testing could be expanded to call the method a number of times so that all pathways through the method are executed and therefore all blocks are executed.
A measure of the code coverage is obtained by comparing the number of blocks in a method that have been executed with the total number of blocks for all instructions in the method.