What is the difference between class coverage and line coverage while running unit tests in Android Studio?

Viewed 2121

When I am running the test classes for my code on Android Studio, I have come across three kinds of coverage.

  1. Class Coverage
  2. Method Coverage
  3. Line Coverage

While method coverage is fairly straightforward to me, I do not understand the difference between line and class coverage. How is class coverage calculated? Is there a formula that takes different metrics into account? How can I improve my class coverage if it has nothing to do with line coverage?

1 Answers

Class coverage includes all classes in the package of class you're being tested. If you don't use other classes when you run test cases then it will show 0% class coverage of those classes.

Suppose you've 2 classes in the package app

-app
  -ClassA.java
  -ClassB.java

for test

-app
  -ClassATest.java

If you run test cases of ClassATest.java then you'll get Class coverage 50%(1/2). Since both classes are in the same package, it included ClassB too.

Here's the funny part, if you create a ClassB object in any test case and run the same ClassATest cases again then you'll get Class coverage 100%(2/2) since you've used both classes.

Line coverage covers the actual lines of code. IntelliJ marks all the lines Green & Red for Covered & Uncovered respectively, so you can check that easily which lines it has considered.

Related