How to get a list of Test Classes in an org

I need to get all test classes (classes with isTest Annotation) in an Org..
Is there anyway to filter test classes out of all the Apex Classes


You can use a SOSL query to find your test classes. Note that this is not foolproof, as SOSL doesn’t differentiate between comments and non-comments.

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

I actually use this technique in my orgs to automate regression testing (runs once every 24 hours, and emails a result file). Just avoid using the word ‘@isTest’ in your production code. In my processor, I also specifically exclude scheduler that calls this query to avoid having it run itself (harmless, but doesn’t need to be run).

Source : Link , Question Author : Chathura , Answer Author : sfdcfox

Leave a Comment