Salesforce support following way to execute a test class inside Salesforce

  1. The Salesforce user interface

  2. The IDE

  3. The Developer Console

  4. The API

But apart from that we can run a test class in Salesforce from apex using ApexTestQueueItem (in beta release) which will submit your test class for test execution asynchronously.


 ApexClass[] testClasses = [SELECT Id FROM ApexClass WHERE Name LIKE '%Test%'];
 if(testClasses.size() > 0) {
    ApexTestQueueItem[] queueItems = new List<ApexTestQueueItem>();

   for (ApexClass cls : testClasses) {
    queueItems.add(new ApexTestQueueItem(ApexClassId=cls.Id));

   //it will submit them for execution
   insert queueItems;

 // Get the job ID of the first queue item returned.
 ApexTestQueueItem item = 
 [SELECT ParentJobId FROM ApexTestQueueItem 
 WHERE Id=:queueItems[0].Id LIMIT 1];
 system.debug('**************************' + item.parentjobid);