Salesforce Continuous Integration

Running Test Classes Nightly

How many times have your deployments been held up due to test class failures?  Or you've had to frantically update a test class at the last minute?  Wouldn't you rather know as soon as a test class fails, so you can fix it right away?  Welcome to Salesforce Continuous Integration!


With a little bit of setup, you can automate all your test classes to run every night, receive an email every morning with the results and even track your test class performance over time.

1. Install Force.com Migration Tool

Force.com Migration Tool has some pre-requisites such as Java and Ant and then you have to install the Force.com Migration tool itself.  This process can be a little painful, but you only have to do it once and I promise it is worth the effort.  Details can be found here

2. Customize Force.com Migration Tool

Within your build.xml file, you'll need to set up a deploy command that deploys nothing, but runs all local tests.  It will look something like this:

 

<target name="runTests">

<sf:deploy 

  username="${dev.username}"

  password="${dev.password}"

  serverurl="${dev.serverurl}"

  deployRoot="EmptyPkg"

  checkOnly="true"

  testLevel="RunLocalTests"

  pollWaitMillis="25000"

  maxPoll="720">

</sf:deploy>

</target>


Note that the variables "${dev.XXXXXXX}" need to be defined in your build.properties file.


You will need to create a folder called "EmptyPkg".  Within this folder, create an empty Package File (i.e. a text file named package.xml) with the following contents:


 <?xml version="1.0" encoding="UTF-8"?

     <Package xmlns="http://soap.sforce.com/2006/04/metadata">

     <version>39.0</version>

</Package>


This won't deploy anything, but will run all your tests.  At this point, you can try running the tests via Ant using the command line to make sure it is set up properly.  The command would be:

     ant runTests

3. Install Jenkins

Install Jenkins.  Jenkins provides an environment to schedule jobs, email out the results and manage the process.  Of course, you can do a whole lot more with Jenkins, but we'll just be using some of it's most basic features.

4. Configure Jenkins to call Force.com

Once you have Jenkins installed, set up a job, go to the Build section of the job and configure it to call your ANT command set up in Step 2.

  • In the Targets section, specify the target name such as runTests.
  • I recommend selecting the "Advanced..." button and specifying the path to the Build File and the Properties file.
  • Configure the Build Trigger to run a scheduled job nightly (such as H 19 * * * to run at 7pm every night) and add E-mail Notifications so you get notified of the results.

If you have made it this far, I recommend one extra step of installing "Log Parser Plugin" for Jenkins.  This will parse the test results and create a "Parsed Console Output" link within each run to easily see the errors for that run.


After you install Log Parser Plugin, set up a parsing rule file which contains a line like the following to list the test errors:

    error /^[0-9]+\.\s/

Add this Parsing Rule as a Post-build Action to your job.


Repeat this by setting up a Jenkins job for each of your tiers.


Now, all your tests automatically run every night in every tier and you get notified of any problems every morning!


You may be saying, if it runs nightly, is it really Continuous Integration?  Well, no it isn't really continuous.  You are welcome to set it up to run more frequently, or even after every compile if you wish, however, we have found that compiling more frequently than nightly hinders development.  Knowing that something broke yesterday is timely enough that issues are identified and corrected quickly without getting in the developers way.