Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Possible Duplicate:
How can I get code coverage programatically through Apex?

How to query CodeCoverage of a class by writing an SOQL inside Apexclass? Is there an sobject which stores the information of Codecoverage of each Apex class? if so what object and what field has this value?

--- edited ---- Thanks @grigriforce. I understand its a duplicate question.. but I need something here but if this is the case then how is SF able to list the Apex classes view where it lists the classes with Code Coverage for each class. I have been asked to develop a page which lists all apex classes which has coverage less than 75% and also to make the classes searchable. For doing this I developed a page which will allow users to search apex classes and list it in a pageblock table but If i need to run each test and get the codecoverage and display it, then my page will hang.I can run all tests and use my page. Any other suggestions how I can display this in my page.

share|improve this question

marked as duplicate by Peter Knolle, Mike Chale, Daniel Blackhall, Saariko Jan 24 '13 at 14:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
whoops. forgot to check for duplicates. –  Greg Grinberg Jan 23 '13 at 23:12

1 Answer 1

This is impossible since code coverage is not stored in an sObject that is accessible to us.

Even if you could run a soql query this would quickly become out of date since code coverage is only re-calculated when you run tests . If you change your classes in the meantime it will be out of date. (source)

I would recommend instead building a visualforce page that uses the AJAX connector to make a runtests() call to the SOAP api. The following pseudo-code should give you the code coverage results:

RunTestsResult res 
if (res.getCodeCoverage() != null) {
    Integer linesNotCovered = 0;
    Integer TotalLines = 0;
    String class = '';
      for (CodeCoverageResult ccr : res.getCodeCoverage()) 
      {
         TotalLines = ccr.getnumLocations();
         LinesNotCovered = ccr.getLocationsNotCovered().size();
         class = ccr.getName();
      }
   }

RunTestResult documentation

share|improve this answer
    
Thanks @grigriforce but if this is the case then how is SF able to list the Apex classes view where it lists the classes with Code Coverage for each class. I have been asked to develop a page which lists all apex classes which has coverage less than 75% and also to make the classes searchable. For doing this I developed a page which will allow users to search apex classes and list it in a pageblock table but If i need to run each test and get the codecoverage and display it, then my page will hang.I can run all tests and use my page. Any other suggestions how I can display this in my page. –  Sathya Jan 28 '13 at 20:44
    
grigriforce is pointing you in the right direction here but this method is not going to work while hosted on salesforce.com because of callout timeout limitations. Some unit tests take a long time to run. You could use your own box to do what grigriforce is describing and then to save the results to a table in your org. –  Phil Rymek Jan 29 '13 at 22:30
    
@Sathya Why don't you just create a new listview under ApexClasses in setup with the following filter: Code Coverage STARTS_WITH 0,1,2,3,4,5,6,70,71,72,73,74 and Code Coverage NOT_CONTAIN 100 –  Greg Grinberg Jan 30 '13 at 3:12
1  
@PhilR I didn't make it clear from my code sample but I was recommending that the tests should be run from js with the ajax connector to avoid these limits. In any case it looks like the actual requirement can be met with a simple listview –  Greg Grinberg Jan 30 '13 at 3:15
    
i understand @grigriforce but i have implemented the full text search feature as well for the code, i mean if they enter some keyword in the text box it will search all the classes having that keyword and list down the class name and its attributes, there I need to show the coverage as well.. Thats the reason I need to query the stored test coverage results. The link that you gave blogs.developerforce.com/developer-relations/2012/11/… says "We Store Your Code Coverage". So even if the coverage is outdated, i can just display the results after previous runtest –  Sathya Jan 30 '13 at 19:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.