TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Software Testing

Statement Coverage

Overview

Statement Coverage is a code coverage metric that indicate the percentage of code statements exercised during the test run. Statement Coverage is a White-box testing technique. It is also called as Line Coverage.

Statement Coverage

Mathematical formula is as follows:

 

\dpi{200} \text{Statement Coverage } = \frac{\text{Number of code statements exercised}}{\text{Total number of code statements}} \star 100

 

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 statement = 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 have 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

Add JUnit5 Library to Build Path

Run Test

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

JUnit 5 Code Coverage Java

Coverage Report

Coverage report can be found in the IDE Console window:

Statement Coverage Java

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:

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

Related Posts

PuTTY Tool UI

Software Testing /

Useful Tools for Software Testers

Errors Code Testing Development

Software Testing /

Error Density Metrics

Errors Code Testing Development

Software Testing /

Error Removal Effectiveness Metrics

RACI Chart

Software Testing /

RACI Chart

Android Calculator Icon

Software Testing /

Android Calculator Test Cases

‹ Software Testing Metrics› Branch Coverage

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version