Skip to content

Latest commit

 

History

History
102 lines (81 loc) · 2.76 KB

07-2-B-Testing-Coverage.adoc

File metadata and controls

102 lines (81 loc) · 2.76 KB

Code Coverage using EclEmma

This tutorial covers the basics of EclEmma and retrieves code coverage metrics using it.

Preliminary

  1. Install EclEmma as a plugin in your Eclipse IDE from here.

Creating a Gradle Project

Note
We will create a Gradle project from scratch and be testing a simple method returnAverage(int[], int, int, int) .
  1. Create a new Gradle project in Eclipse by clicking on File > New > Other
    New Project

  2. Under Gradle, choose Gradle Project
    New Gradle Project

  3. Click on Next, then name your project tutorial7, click on Finish
    Project Name

    Note
    The project may take some time to be created.
  4. Create a new package instead of the default ones for both the source and test folders (e.g ca.mcgill.ecse321.tutorial7) and move the default generated classes (Library and LibraryTest) to this package.
    Create Packages

  5. Change the code in the Library class

    package ca.mcgill.ecse321.tutorial7;
    
    public class Library {
    
    	public static double returnAverage(int value[], int arraySize, int MIN, int MAX) {
    		int index, ti, tv, sum;
    		double average;
    		index = 0;
    		ti = 0;
    		tv = 0;
    		sum = 0;
    		while (ti < arraySize && value[index] != -999) {
    			ti++;
    			if (value[index] >= MIN && value[index] <= MAX) {
    				tv++;
    				sum += value[index];
    			}
    			index++;
    		}
    		if (tv > 0)
    			average = (double) sum / tv;
    		else
    			average = (double) -999;
    		return average;
    	}
    }
  6. Change the code in the LibraryTest class

    package ca.mcgill.ecse321.tutorial7;
    
    import static org.junit.Assert.assertEquals;
    
    import org.junit.Test;
    
    public class LibraryTest {
    
    	@Test
    	public void allBranchCoverageMinimumTestCaseForReturnAverageTest1() {
    		int[] value = {5, 25, 15, -999};
    		int AS = 4;
    		int min = 10;
    		int max = 20;
    		double average = Library.returnAverage(value, AS, min, max);
    		assertEquals(15, average, 0.1);
    	}
    
    	@Test
    	public void allBranchCoverageMinimumTestCaseForReturnAverageTest2() {
    		int[] value = {};
    		int AS = 0;
    		int min = 10;
    		int max = 20;
    		double average = Library.returnAverage(value, AS, min, max);
    		assertEquals(-999.0, average, 0.1);
    	}
    }

Retrieving Test Coverage Metrics

Note
We can straightforwardly manage code coverage using JaCoCo inside Eclipse with no configuration if we are using EclEmma Eclipse plugin.
  1. Run the Test in coverage mode using Eclemma. Click on LibraryTest, Coverage As, 1 JUnit Test
    Full Branch Coverage

  2. Verify that we have 100% branch coverage.
    Full Branch Coverage-Eclemma