This tutorial covers the basics of EclEmma and retrieves code coverage metrics using it.
-
Install EclEmma as a plugin in your Eclipse IDE from here.
Note
|
We will create a Gradle project from scratch and be testing a simple method returnAverage(int[], int, int, int) . |
-
Create a new Gradle project in Eclipse by clicking on File > New > Other
-
Click on Next, then name your project tutorial7, click on Finish
NoteThe project may take some time to be created. -
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
andLibraryTest
) to this package.
-
Change the code in the
Library
classpackage 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; } }
-
Change the code in the
LibraryTest
classpackage 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); } }
Note
|
We can straightforwardly manage code coverage using JaCoCo inside Eclipse with no configuration if we are using EclEmma Eclipse plugin. |