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.

5 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
    Replies
    1. Hi, Can you please help me with the test class

      Delete
    2. can you please help me with test class. i am also getting same error

      Delete
  2. Hi Everyone, Can someone please provide me test class for the above schedular.

    ReplyDelete
  3. Hello ForceTree, I need to add the test coverage of the failure test class also in email. And if the test coverage is less than 80% then it should show in red color. And if test coverage is more than 90% then it should show green color in email for that test class. Can you please help me with this requirement I really need this urgently. Please help me

    ReplyDelete