A simple scenario (I)

Let us assume I have 3 classes A,B,C.

Each class has 100 lines on them.

Total Number of Apex lines written (for simplicity sake let us assume that none of the 300 lines are comments, @Test etc)

Q : So every uncovered line of each class in this simple scenario will contribute to a loss 0.33 % in overall code coverage...am I right ?

A complex scenario (II)

Class A : 50 lines

Class B : 100 lines

Class C : 150 lines

Again for simplicity sake let us assume that none of the 300 lines are comments, @Test etc

Q : How much loss of test coverage % will be caused by each uncovered code line in this scenario ?....Is it once again 0.33 % ??

My objective is to understand whether the entire test coverage is calculated simply like

Test Coverage % = (Covered Lines across all classes / Total Lines across all classes) * 100

Or any other way is involved ??

  • n.b. one could argue that if you write testmethods that do asserts to make sure that your code does what it is supposed to do and thus provides a regression test suite, that code coverage goals will be more than adequately achieved :-)
    – cropredy
    Dec 11, 2014 at 19:18

1 Answer 1


The Metadata API returns lines covered and not covered per class and when these are added up they do correspond to the web UI numbers. So yes the overall coverage appears to be just the sum of covered lines divided by the sum of the total lines across all the classes and triggers.

private String coverageSummary(RunTestsResult results) {
     int allCovered = 0;
     int allTotal = 0;
     StringBuilder sb = new StringBuilder(4096);
     CodeCoverageResult[] coverages = results.getCodeCoverage();
     for (CodeCoverageResult coverage : coverages) {
          int total = coverage.getNumLocations();
          int notCovered = coverage.getNumLocationsNotCovered();
          int covered = total - notCovered; 
          allCovered += covered;
          allTotal += total;
          sb.append(coverageLine(coverage.getName(), covered, total));
     return coverageLine("Total", allCovered, allTotal) + "\n" + sb.toString();

(From https://code.google.com/p/force-deploy-with-xml-report-task/.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .