• TestingDocs
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

Method or Function Coverage

Overview

Method or Function Coverage is a metric that checks if each method in the application source code is executed or covered by the tests.

Method Coverage

Mathematical formula for Method Coverage is as follows:

Method Coverage =

\LARGE \frac{\text{Number of Covered Methods}}{\text{ Total number of Methods}} * 100

Example

Sample Method Counters data:

Covered Methods = 6

Total Methods  = 8

Method Coverage = (6/8) * 100

= 75%

Missed methods = Total Methods – Covered Methods

= 8 – 6 = 2 methods.

 

Code Listing

Let’s write a simple class with 5 methods. We will execute only some in the code and check the method coverage report in Eclipse IDE. In this example, we will just invoke the methods using the main method.

public class MethodCoverage {

 public void methodOne() {
 System.out.println("methodOne is covered.");
 }

 public void methodTwo() {
 System.out.println("methodTwo is covered.");
 }

 public void methodThree() {
 System.out.println("methodThree is covered.");
 }

 public static void main(String[] args) {
 MethodCoverage mc = new MethodCoverage();
 mc.methodOne();
 }

}

 

Method Coverage

Right click on the code editor and choose >> Coverage As >> Java Application. 

There are 5 methods in the given code. Three user defined instance methods. The main() driver method. One constructor method that is implicitly invoked during the object creation with the new operator.

The exercised methods are highlighted with green color in the coverage report. The methods that are not exercised are highlighted with red color. We can notice that the methodOne() is covered.

Covered methods = 3 [ methodOne(), main(), Constructor  method]

Total methods = 5

Missed methods = 2 [ methodTwo() , methodThree() ]

Method coverage = (3/5)*100

= 60%

—

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

‹ Branch Coverage› How to Write Test Cases

Recent Posts

  • How to secure your SQL Database: Tips and Tricks
  • Shaping the Future of Development: Exploring Key Trends in Software Engineering
  • Improving Java Performance with Multithreading
  • Difference between PHP and JavaScript?
  • Bing Conversation Styles
  • ChatGPT Introduction
  • Open Source AI Frameworks
  • Artificial Intelligence Tools
  • Top AI Music Applications
  • Top AI Website Design Tools

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com