Tuesday, October 11, 2011

Viewing Apex code coverage

There are several ways to view the code coverage results from running the Apex test cases.

  • With the Eclipse Force.com integration Code Coverage Results
    This isn't ideal as you have to go from line to line and can't get an overall view of which lines aren't being tested.
  • Apex Test Result page (https://xyz.salesforce.com/setup/build/runAllApexTests.apexp)
    After running the Apex tests from within Salesforce clicking the "Class Code Coverage" "Coverage %" column will bring up a code coverage overlay.
  • The viewCodeCoverage page
    This is my current preferred approach. From the Apex Classes page (https://xys.salesforce.com/01p) click the percentage in the "Code Coverage" column. This will bring up a new page with the Apex Class ID in the query string: https://xyz.salesforce.com/setup/build/viewCodeCoverage.apexp?id=01pE00000008cZt
    One advantage of this page is that you get a drop down at the top of the page that you can use to examine each test case in isolation.
    Also, you can keep it open in a separate browser tab and just refresh it as required.

See Also:

  • At the time of writing there was a super frustrating bug in the Salesforce Code Coverage System. Eclipse would tell me the code coverage was one value, say 79% for the class in question, and the Apex Classes page would only have 61% in the Code Coverage column. The code coverage views above would show missing coverage on comments and closing braces. After some hunting around I found you must clear the Apex Test Results History (https://cs8.salesforce.com/07M, replacing cs8 with your Salesforce server instance). After which the two figures would line up again.