Search code examples
unit-testingsalesforceapex-codeforce.com

Is there a good way to get a Code Coverage Report for Apex Code?


We're developing alot of enhancements to salesforce using Visualforce and Apex as part of a larger system, as part of our quality metrics we have to provide a report to management on our Code Coverage.

I'd like to get a report similar to the one produced by Run All Tests in the Force.com IDE but in HTML so I can display it easily via a web interface.

For the rest of our system we use Sonar http://www.sonarsource.org/ to produce the reports.

Does anybody know the best approach to this?

I've explored the API documentation but am unable to find out if the Coverage Percentages is stored against the classes so querying that isn't an option.

Any help or pointers would be greatly appreciated.


Solution

  • If you run the Apex tests yourself via the API there are objects returned indicating which lines hasn't been covered by the tests in that run. You can run the tests via either the synchronous or asynchronous methods.

    Then you can use the data to create a report in a format that you require. For example, I've used it to create a basic report in the FuseIT SFDC Explorer (Windows based and free). I'm just dumping out line ranges that aren't covered.

    FuseIT SFDC Explorer Code Coverage Results

    You will probably want to run all the tests in one run to get the complete code coverage of all the tests. For example, in the screenshot above I only ran one out of a much greater number of test classes. As a result it looks like the code coverage was much lower than the cumulative tests give. It does however show which lines an individual test class reaches.

    I've also heard good things about MavensMate for the Sublime Text Editor. Being open source you should be able to find how it integrates with the testing api and then generates the reports.