2

I have extracted all the component from Salesforce Org so I want the names of all the TestClasses which covers all ApexClasses. I have created excel sheet such as Component Type, Component Name and I want to add 1 more column IsTestClass. IstestClass contain Yes or No. If Yes means component name should be ApexTest class or else No

I have all the ApexClass names, but I want only the apex test class name.

  • 4
    use naming conventions in your org, so that all test classes are next to each other? For example TEST_myClass1 and TEST_myVFController1. Like this all your TC will be next to each other – Novarg Apr 7 '17 at 6:49
  • 1
    Good suggestion by Novarg. @Amit, I am afraid that there is any direct way to find the details, that's why have a proper naming convention in the project is necessary. For classes, you can find in which test class they are being used, by searching your code in any IDE of your choice. Hope that your fellow developers have not tried to cover multiple classes/riggers in single test class. – Raul Apr 7 '17 at 7:02
  • @Novarg Thanks for the reply. But if suppose ApexClass name contains "test" keyword then how can we differentiate?? – amit patil Apr 7 '17 at 7:03
  • 1
    @Amit, You can scan the body of classes to check if they have @IsTest annotation. Problem would arise when you have the annotation in comments, so you would need to build a smart filter which ignore comments. – Raul Apr 7 '17 at 7:21
  • 2
2

You should definitely start using a naming convention where test classes stand out by looking at the name, but there are legitimate use cases for being able to programmatically query all test classes.

If you look at the developer console, they have their own _ui endpoint for performing such a query that you could use yourself, although it isn't officially supported:

$ curl -b 'sid=<session-id>' https://<instance>.salesforce.com/_ui/common/apex/test/ApexTestQueueServlet -d 'action=GET_TESTS' -X 'POST' -H 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8'

This will return normal classes that contain test methods.


If you want to use a standard api feature, another option would be to query the tooling api for code coverage, and all of the classes not present would be test classes:

$ curl -H 'X-PrettyPrint: 1' -H 'Authorization: Bearer <authorzation-token>' https://<instance>.salesforce.com/services/data/v39.0/tooling/query?q=SELECT+Coverage,ApexClassorTriggerId+FROM+ApexCodeCoverage

Normal classes with test methods will not be excluded.

2

Another option to identify if an ApexClass contains test methods or not is to utilize the SymbolTable via the Tooling API.

In particular, look at the annotations and/or modifiers on the class and methods. They will tell you exactly if the @IsTest annotation is present or the testMethod modifier applied to a method.

  • @ Daniel Ballinger Thanks for your suggestion. For symbol Table which tooling object I have to use? Or tell me some sample query example. – amit patil Apr 7 '17 at 9:15
  • @amitpatil You can use either the SOAP or REST version of the ToolingAPI. Usage is addressed in another question - Symbol Table from Apex Class – Daniel Ballinger Apr 7 '17 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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