2

As the CLI runs tests before deploying an Apex Class, it has to know which tests are associated with a certain Apex class.

Is there a way to retrieve a list of test classes that are associated with a specific Apex class?

I tried to find them using sf project deploy preview, but without success.

2 Answers 2

4

No, there is not.

About the closest thing we have to that is to run all tests in an org and then query data from the ApexCodeCoverage object in the Tooling API (and also the ApexClass object, so you can correlate on the class id to get an actual name).

You could use the developer console to query that data, or alternatively open the individual classes you're curious about (in dev console) and click the "Code Coverage" dropdown to get the list of tests that cause a given class to execute at least one line of code or method.

In general though, this is probably best done by using a naming convention for your test classes. For unit tests (that ideally only invoke code from a single class), name them <Class name here>Test or test<Class name here>. There may be other tests that give you some incidental coverage, but using a naming convention makes it dead simple to know which test is for which class.

1

In theory, you can query MetadataComponentDependency, as each unit test will depend on the class that it calls, but you may end up with some false positives or negatives, because this object can only detect compiler-time dependencies. You'll also want to leverage naming conventions to help identify which classes are associated with unit tests.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .