Last month, the Force.com Developer Relations team held a Holiday Cookbook Contest to spur user-generated content. There were a good number of high-quality submissions that were reviewed, tested, and edited before being posted to the Force.com Cookbook site. You can see my own submission here.
I want to point out one cookbook recipe that is of particular interest, which is Luke Freeland’s recipe for automating the execution of unit tests in a Salesforce org. The big idea is to use Schedulable Apex to periodically add all Apex classes that start with “Test” as records in the ApexTestQueueItem object to be executed. The icing on the cake is that Luke created another scheduled job that will check the results of these tests, which are stored in the ApexTestResult object, and email them out.
While the idea of Continuous Integration is very common in traditional desktop development projects, it seems to have been largely forgotten in the migration to the cloud. If your Salesforce custom code base is small, then setting up a continuous integration strategy may be not that important, but many Salesforce users are increasingly demanding more custom code to be written to streamline their workflow in Salesforce.
I’ll give a quick object overview of Luke’s code for you to review if you choose to implement this solution and automatically execute your org’s unit tests.
AutomatedTestJobQueuer
+ execute
+ doExecute
+ createDaily4AMScheduledJob
+ enqueueUnitTests (Visible through API)
+ enqueueTests
AutomatedTestingJob
+ doExecute
+ createEvery15MinuteScheduledJobs
+ processAsyncResults
+ getCompletedAsyncJobsIds
- sendTestResultEmail
- getTestResultHtmlEmailBody
+ checkClassStatus
+ checkMethodStatus
After you understand the code and have it copied into your org, here are the commands to get the automation ball rolling.
Execute this Apex code to start the automated execution of Apex tests:
AutomatedTestJobQueuer.createDaily4AMScheduledJob();
Execute this Apex code to have test results emailed:
AutomatedTestingJob.createEvery15MinuteScheduledJobs();
Luke’s solution is quite barebones, so if you like an alternative solution with a bit more polish, you should definitely check out Matthew Botos’ Automated Testing for Force.com code on GitHub.
Matthew’s solution has a few advantages:
1) Ease of installation with the Salesforce package he supplies.
2) A nice UI which includes a Visualforce page and tab.