Don't Get Bitten By Failing Apex Unit Tests
[ testing ]

This will be a short post about something basic that all Salesforce teams running custom Apex code should have in place - a mechanism that runs all of your Apex unit tests daily.

Generally speaking, no one can argue with this well-established best practice. The sooner you are aware that some change in your environment is causing problems with your Apex logic the better - so running your tests regularly just makes sense.

If a specific example would be helpful motivation, I’ll share what finally pushed me to put my client’s Apex tests on auto-pilot. My client had an initiative to re-factor the company’s sales process, involving changes to the Opportunity object fields and validation rules. There were no Apex logic changes involved, so this effort was led by the admin team. Shortly after this effort, a rollout of unrelated new Apex logic was scheduled for our regular Thursday afternoon deployment window. You can likely see where this story is headed.

As the team moved into deployment of the new Apex logic, they were surprised by 20+ apex test failures related to the declarative changes associated with the sales process re-factor project. The result was a stressful deployment window as the team had to diagnose and mitigate a pile of unanticipated Apex errors.

Lots of good lessons learned from that experience - including the importance of inter-team communication, as well as the need for automated unit testing. Had this automation been in place, we would have discovered the test failures days before our deployment window and addressed them.

The Solution

Most of the guidance on the web around automating unit testing involves standing up a continuous integration tool like Jenkins, and using it in concert with the ANT migration tool. This was a heavier solution that what I was looking for.

Then I came across a very interesting solution to this problem from Ravi Dutt Sharma, that is exclusively built within Salesforce - check out the github repository.

But I also wanted to keep my solution separate from Salesforce, so I ended up going my own way, and it was so straightforward that I was shaking my head that I didn’t implement something like it earlier.

I ended up writing a small ruby script that authenticates to Salesforce using the fantastic restforce gem, and then calls the Tooling API to kick off a test run. It waits for the job to complete, and then retrieves the results and uses mailgun to send a nicely formatted email (thanks to Ravi Dutt Sharma for the email formatting!).

For now I have this running via cron on my own machine every M/T/W/F (no Thursdays as we release that day, and the tests already get run) at noon.

It’s not an ideal solution or an “enterprise” solution (if my laptop is off at noon, the tests don’t run :)) - but it is easy and lightweight and gets the job done.

If you are interested in seeing the script, here is the github repository.

So don’t wait - get your Apex unit tests running regularly ASAP!