Toggle

# Overview

Statement Coverage is a code coverage metric that indicates the percentage of code statements exercised during the test run. Statement Coverage reports whether each executable statement is encountered. It is a White-box testing technique. It is also called Line Coverage.

## Statement Coverage

The mathematical formula is as follows:

Statement Coverage requires all statements in the code to be executed at least once. To achieve 100% statement coverage, every statement in the application source code should be executed at least once

### Example

Assume a software program has 70 lines of code. 40 statements are executed during the program run. Now we will calculate the Statement Coverage metric using the above formula.

Statement Counters based on the data:

Number of Covered statements = 40

Number of Missed statements = 30

Total number of statements = 70

Statement Coverage = (Covered statements/Total Statements)* 100;

SC = (40/70) * 100

= 57.14 %

## JUnit 5 Example

Let’s create a sample Java class and a JUnit 5 test. We will then run the test and compute the Code coverage by the JUnit Test. We have to design tests so that every line/statement in the program has to be executed at least once.

Sample Java class: Book

Sample JUnit Test: BookTest

### Java Statements

Java statements in the code include

• declaration statements,
• control flow statements like if, if-else, while, for, etc
• loop control statements like break, continue
• expression statements

### Steps

• Create a Java Project
• Create a Java Class
• Create a JUnit Test

## Code Listing

### Book.java

package com.testingdocs.codecoverage;

public class Book {
private String title;
private String author;
private double price;
private int pages;

//Constructor
public Book(String title,String author, double price, int pages) {
this.title = title;
this.author = author;
this.price = price;
this.pages = pages;
}

public String getAuthor() {
return author;
}

public String getTitle() {
return title;
}

//print book method
public void printBookDetails() {
System.out.println("Title        =" + title);
System.out.println("Author       =" + author);
System.out.println("Price       \$=" + price);
System.out.println("No of Pages  ="  + pages);
}
}


### BookTest.java

To create a new JUnit Test class:

File >> New >> JUnit Test Case

package com.testingdocs.codecoverage;

import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;

class BookTest {
Book book;

@Test
void testBookDetails() {
book = new Book("Java Testing","SKumar",30,450);
book.printBookDetails();
assertEquals(book.getAuthor(),"SKumar");
assertEquals(book.getTitle(),"Java Testing");
}
}


### Run Test

Right-click on the JUnit Test. Choose Coverage As >> JUnit Test

### Coverage Report

The coverage report can be found in the IDE Console window:

Project Properties >> Coverage

A statement or line is covered when the test executes it. The green highlight in the coverage report indicates that the tests cover the line. The red color highlight indicates that the tests do not cover the line.

The main advantage of this measure is that it can be applied directly to object code and does not require processing source code. Performance profilers commonly implement this measure. The disadvantage of statement coverage is that it is insensitive to some control structures.

Software Testing Tutorials:

https://www.testingdocs.com/software-testing-tutorials/