Monday 11 March 2019

Export Salesforce Apex Test Coverage

Salesforce is a very dynamic platform which provides us(Developers - wink :P) different ways to accomplish the tasks. The sole choice lies with the developer to adapt the method in such a way to perform the task in an automated fashion. 

Problem Case:

The main block which everyone doesn't like(as per the feedback from my friends) is the Apex Test Coverage as it hinders the developers to deploy to other instance(although it is a best practice to write the test coverage above 85%) but what if when we are working on a project where we are dealing with huge no of apex classes, how to deal with the coverage of so many classes. Unfortunately, Salesforce do not provide us a direct way where we can export it in portable format. Here in this post i will try to explore new technique by which we can export the Latest Org Test Coverage.


Pre-Requisites:
1) The Apex page and Apex class should be present in Org(Our technique to export the coverage in excel)
2) We should have executed Run All Test in Org before exporting coverage
3) The Admin user should be used to export the results

Solution:
Salesforce exposes the Test coverage results on classes through Tooling REST API, hence there are many ways to consume the API and create our utilities such as - create java based code to connect to SF and get results in local to parse the response. Here is my case, I have developed a Salesforce VF page which is rendered as excel doc after querying & parsing Response from Salesforce Tooling API to fetch coverage results.

API details:
/services/data/v43.0/tooling/query/?q=Select+id,ApexClassorTrigger.Name,NumLinesCovered,NumLinesUncovered+from+ApexCodeCoverageAggregate+order+by+ApexClassorTrigger.Name

In above query, we are querying results from "ApexCodeCoverageAggregate" and fetching the response to parse it into table(with some obvious calculations).

Code:
SFDC_ExportTest - GitHub Repo

No comments:

Post a Comment