Scheduling Run All Apex Tests and emailing results

2:23 PM

Credits:
 The code below has been adapted from the cookbook recipe http://developer.force.com/cookbook/recipe/automated-unit-test-execution. Re-posting this since the link appears to be broken.
 
STEP 1:
  • Create an object with API name AutomatedTestingQueue__c
  • Create a field on this object of type TEST(40) with API name AsyncId__c 

STEP 2:

Create the apex classes below.
  • Modify the SOQL query to pick test classes to execute. The query in this sample just picks any class with the name TEST in it.
  • Also update your email address to receive the results.

STEP 3:

Schedule the apex class 'AutomatedTestJobQueuer'. This will run the apex tests.





STEP 4:

Schedule the apex class 'AutomatedTestingJob'. This will send the email. Make sure you have enough gap between these two jobs to allow all tests to run. Sample here is one hour, you might want to increase this if you have many apex tests.

1 comments

  1. Hi,

    While writting test clas for this code. I'm getting an error. ""System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Apex tests cannot be enqueued during a test.: []".

    I'm just trying to insert ApextestqueueItem in test class. to get the ParentJob value. The parent Job we are adding into custom object custom field "AsyncId__c"

    Thanks in advance.

    ReplyDelete