2

Does anyone have any advice on how I can go about querying test classes and dependent test classes?

Our Org is getting very large and finding the correct test classes for each class is becoming an issue. I am looking into ways to query all of our Apex Classes and store them in a table and link them to all dependent test classes that need to be run in order to achieve correct code coverage/dependence.

Is the MDAPI the best way to go about doing this? Is there any way I can use DX to query? Or should I just create one internal Apex Class to do all of the work?

Any help/advice would be greatly appreciated! Thanks.

  • 1
    you no doubt have figured out why it is best practice to have for each XXX.cls an XXXTest.cls that thoroughly unit tests XXX. – cropredy Oct 14 at 19:18
3

As a starting point, you will need to identify which Apex classes contain tests. From How to get a list of Test Classes in an org by sfdcfox:

ApexClass[] unitTests = [FIND '@isTest' IN ALL FIELDS RETURNING ApexClass(Id, Name)][0];

You can then use the Metadata dependency API via MetadataComponentDependency with the Tooling API. With this Tooling sObject you can determine the dependencies between those test Apex classes and the other apex classes in the org. In particular, you'll want to query with the MetadataComponentId field being the Id's of the test classes you've identified.

If you've recently run the test classes in the org you can also use ApexCodeCoverage. The ApexClassorTriggerId field with have the ID for the class the test case touched with it's last run. ApexTestClassId is the ID of the test class that was running. The problem with this approach is that the data is only available once the tests have already been run. And it will only be as current as the most recent test run.

1

You might be able to pull some of this through the Tooling API https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_apexcodecoverage.htm

0

Assuming you're following best practice of "ClassName" and "ClassNameTest", a simple SOQL query could provide some information. You'd probably have to build a class that first searches for classes without the word test in the name, then iterate through those results with a query using the "LIKE" operator.

SELECT Name FROM ApexClass WHERE Name LIKE '%HandleConvertQueue%'

This query results in both "HandleConvertQueue" and "HandleConvertQueueTest"

0

To make it easier, you should introduce naming standards.

Most of the common standard followed for test classes is

ClassName_TEST or ClassNameTEST

This solution may take time but its better.

Regards, Raj

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.