function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arthur Almeida 12Arthur Almeida 12 

self test - how to schedule test class in salesforce

I have a Unit Test Apex, and I would like to schedule this test for execution every day in a specific hour
how to make this?
Best Answer chosen by Arthur Almeida 12
Arthur Almeida 12Arthur Almeida 12
I solve my problem following this article: https://www.forcetree.com/2019/12/scheduling-run-all-apex-tests-and.html
 
global class SelfTest implements Schedulable {
    global void execute(SchedulableContext ctx){        
        List<ApexClass> testClasses = [SELECT Id FROM ApexClass WHERE Name = 'NAME_TEST_CLASS'];
        List<ApexTestQueueItem> queueItems = new List<ApexTestQueueItem>();
        for (ApexClass testClass : testClasses) { 
            queueItems.add(new ApexTestQueueItem(ApexClassId=testClass.Id)); 
        }        
        if(!Test.isRunningTest()) insert queueItems;    
    }
}

System.schedule('Self Test', cron, new SelfTest());

All Answers

PriyaPriya (Salesforce Developers) 

Hey Arthur,

There are many, many ways to automate it. You can subscribe to an app like GearSet to schedule automations, you can set up a CI/CD (Continuous Integration/Continuous Delivery) system to run tests on a schedule, you can use the basic core features of Windows or Linux systems to run a Windows Task or Cronjob, deploy a container to Heroku or AWS, etc, or you could write Apex code in a scheduled class to run at the desired time. Each solution will have different technical requirements, may require a subscription, etc.

For Scheduling Run All test, refer this : https://salesforce.stackexchange.com/questions/46073/scheduling-run-all-tests

Hope it was helpfiul.

Kindly mark it as the best answer, if it helps so that it can help others as well.

Thanks & Regards,

Priya Ranjan

Arthur Almeida 12Arthur Almeida 12
I solve my problem following this article: https://www.forcetree.com/2019/12/scheduling-run-all-apex-tests-and.html
 
global class SelfTest implements Schedulable {
    global void execute(SchedulableContext ctx){        
        List<ApexClass> testClasses = [SELECT Id FROM ApexClass WHERE Name = 'NAME_TEST_CLASS'];
        List<ApexTestQueueItem> queueItems = new List<ApexTestQueueItem>();
        for (ApexClass testClass : testClasses) { 
            queueItems.add(new ApexTestQueueItem(ApexClassId=testClass.Id)); 
        }        
        if(!Test.isRunningTest()) insert queueItems;    
    }
}

System.schedule('Self Test', cron, new SelfTest());
This was selected as the best answer