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 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:

     

    \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 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

    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

    The 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

    Trends in Software Engineering

    Software Testing /

    Shaping the Future of Development: Exploring Key Trends in Software Engineering

    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

    Software Testing /

    Open-Source Load Testing Tools

    ‹ Software Defect Metrics› Branch Coverage

    Recent Posts

    • ChatGPT Subscription Plans
    • Stellar Converter for Database
    • Stellar Log Analyzer for MySQL
    • Stellar Repair for MySQL
    • ChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI Features
    • Shaping the Future of Development: Exploring Key Trends in Software Engineering
    • Improving Java Performance with Multithreading
    • Open-source Vector Databases

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com