Create own Code Coverage Solution!
A few years ago, Salesforce removed Code Coverage from the Trigger and Class GUI screens. Encouraging you to go to Developer Console and view from there. But it is a small panel, and not easy to find the ones you are looking for.
But what if you could view it in the GUI again? And while you were at it, made the columns sortable? And create a matrix of those that are <75% coverage, just over, or safely over? While we are at it, we'd like to be able to export it to Excel too! Gee, you want dinner with that too? Well, all of this is possible by using VisualForce and the Metadata API. Let's take a look...
Solution Background
We're not going to get into the nuances of the entire solution. That would take too long, and what fun would that be? Instead, we'll talk about the highlights, and then provide the code for the 2 pages and 3 classes.
Requirements
As mentioned in the beginning, we want the below:
- Show us all Classes/Triggers and their coverage.
- Make the columns sortable.
- Have a matrix so that I know how many classes/triggers are within/outside standards.
- Make this representation exportable to Excel.
So, essentially, our solution needs to look like:
So, first things first. Let's look at the Code Aggregate Class.
Code Aggregate Class
This is just a storage class to hold data representations and to return the data returned from the Metadata call properly. Later, we'll see the code.
Now we need to CONSUME this class. We're now ready for the Controller Class.
Controller Class
We have obviously a bunch of unremarkable properties and variables to create. But, there are a couple of key methods that drive the solution.
codeCoverage - this is both a constructor and a method that gets called from the constructor. It re-initializes the totals, gets the current coverage from the ApexCodeCoverageAggregate object, makes the request, and feeds the applicable properties/variables. In others, all of the heavy lifting is done here.
Within this class is where the compares for coverage is being accomplished and stratified. The "limits" for that coverage (i.e. <75%, 75-85, etc) are hard-coded. This could easily be moved to a Custom Setting so that you could dynamically change the aggregation if desired. For us, the above limits seem to work well to focus on where work should be targeted.
apexWrapper - this class is both a wrapper to represent the coverage results, and allows each of the columns to be sortable.
Later, we'll see the code.
Now that we have a Controller, we need VisualForce page to represent the logic.
VisualForce Page
This page has a number of unremarkable style definitions and then maps out the page. As you might guess, the work should be done with repeats and/or tables. In this case, the Controller is readying the data allowing the page to handle GUI interactions which it does well. This page is rather simple and straight-forward so not much discussion is needed.
Generate Coverage to Excel
Some of you may not be familiar with how to do this, but it's really pretty simple. You create a bunch of styles for the desired styling of the sheet (if desired). The actual HTML and APEX after that is really quite simple. The KEY is generating it as a text/XML file and then giving the filename an XLS extension as noted in the first line. Yes, Excel natively knows how to open an XML formatted file.
Later, we'll see the code.
Test Class
Of course, you need a Test Class to handle all of this. The link later will give you an example of one, but you’ll probably want to make it more encompassing with Asserts, etc.
Conclusion
Is that it? No, you might have some things you want to personalize it with. Include Namespaces, change coverage limits, add email or task notifications, etc. In addition, you need to probably create a Remote Site settings entry so that your page is able to call out. Remember, with the Metadata API, you are actually making a callout to yourself. For example, mine looks like:
https://c.cs17.visual.force.com
and I have another in another instance that looks like:
https://citrite--fdev--metadatasearch.cs93.visual.force.com
Yours might be different depending on the instance, domain configuration, etc.
This, of course, gets you going. The fun comes from customizing it the way you want. Give it a shot. Once you have it and run it in Production, you can manage your APEX so much more quickly and focus your work.
Download!
Wait! Didn't I mention we'll see the code later? Yes, I did. BUT, LinkedIn doesn't support so many characters in a "Snippet". Therefore, I've created a Dropbox folder with the files that you can get by going to this location below. Happy coding!
Thanks for sharing Robert!
Woah. Thanks for sharing.
“Gee, you want dinner with that too?” Thanks for writing and publishing, Robert.