Site icon TestingDocs.com

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 =

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();
	}

}


 

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/

Exit mobile version