Line Coverage or Statement Coverage

Line Coverage or Statement Coverage

Line coverage and statement coverage are two metrics used in software testing to measure the effectiveness of test cases. They assess the extent to which test cases exercise a program’s code.

Line Coverage:

Line coverage, also known as line-based coverage or line-by-line coverage, measures the percentage of lines of code that are executed by a set of test cases. It considers whether each line in the code has been completed at least once during testing.
Line coverage is a relatively simple and coarse-grained metric. It doesn’t consider the specific logic within a line, just whether it has been executed.

Statement Coverage:

Statement coverage, also known as statement-based coverage, is a more fine-grained metric. It measures the percentage of individual statements in the code executed by the test cases.
In this metric, each statement in the code is considered separately, and the goal is to ensure that every statement is executed at least once during testing.

Statement coverage is more rigorous than line coverage because it focuses on the granularity of individual statements. In contrast, line coverage may overlook specific branches or conditions within a line of code. Achieving 100% statement coverage ensures that every part of the code has been tested, including different branches, loops, and conditional statements. However, it is essential to note that even 100% statement coverage only guarantees that some potential scenarios and corner cases have been tested, as it only checks if a statement has been executed, not whether it has been conducted under all possible conditions.
In practice, both line and statement coverage can be helpful, but statement coverage is often considered a more comprehensive metric for code testing. It is typically part of a broader set of code coverage metrics used by software developers and testers to assess the quality of their tests and the extent of code coverage achieved during testing.

Statement coverage is also known as line coverage. The formula to calculate statement coverage is:

Statement Coverage=(Number of statements exercised/Total number of statements)*100

Studies in the software industry have shown that black-box testing may actually achieve only 60% to 75% statement coverage, this leaves around 25% to 40% of the statements untested.

To illustrate the principles of code coverage lets take one pseudo-code which is not specific to any programming language. We have numbered the code lines just to illustrate the statement coverage example however this may not always be correct.

READ X
READ Y
IF X>Y
PRINT “X is greater than Y”
ENDIF

Let us see how can we achieve 100% code coverage for this pseudo-code, we can have 100% coverage by just one test set in which variable X is always greater than variable Y.

TEST SET 1: X=10, Y=5

A statement may be a single line or it may be spread over several lines. A statement can also contain more than one statement. Some code coverage tools group statements that are always executed together in a block and consider them as one statement.