Statement Coverage
Overview
Statement Coverage is a code coverage metric that indicates the percentage of code statements exercised during the test run. Statement Coverage 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"); } }
Add JUnit5 Library
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 color highlight in the coverage report indicates that the line is covered by the tests. The red color highlight indicates that the line is not covered by the tests.
—
Software Testing Tutorials: