Essentially, Code Coverage is how much of your code running your test cases passes through. Say you wrote a function which takes the square root of a number, or returns -1 if the number has no valid square root.
public static double sqrt(double num) {
X if (num < 0) {
X return -1;
X }
return Math.sqrt(num);
}
You wrote the following test:
@Test
public void sqrtTest(){
assertEquals(2, sqrt(4));
assertEquals(3, sqrt(9));
}
While running your tests, we see that we never ran a case in which we entered
the if statement in sqrt
. This means that the if statement and inside code is
not covered by your tests, denoted by the X in uncovered lines. If we add
another test in which input was negative, we would cover the if statement as
well. If one branch of an if statement has extremely different behavior to the
other branch, it is important to write tests that cover both branches.
The higher your code coverage is, the more unlikely it is for a bug to be in your code. Code that is not covered is not tested! Your goal is to design tests that cover 100% of your code. However, for some homeworks, this will not be possible, so we will adjust our standards accordingly, but you will receive higher testing grades for tests that cover more code. You should still think about special cases when designing tests, because although certain code may be covered already, it may behave differently with different inputs.
IntelliJ already comes bundled with plugins that measure code coverage. Please double check that this plugin is activated/enabled:
In your project, you can measure coverage by right-clicking on your JUnit test class (e.g. MazeSolverImplTest.java
) and then clicking [Run with Coverage] or [More Run/Debug → Run with Coverage].
Once you run the tool, a new view should appear, where you can review the code coverage scores you got from running the test class.
For more information on using, configuring, or customizing the code coverage tool, you can visit the associated page on the IntelliJ website.