Salesforce testing best practice: why you should regularly run production tests
Jason Mann on March 16th 2016
Testing your code to ensure it’s working correctly is a core tenet of good Salesforce development. But when was the last time you ran unit tests in your production org, and why do you need to?
In this post we’ll explore why testing in Salesforce is important, why you should be testing regularly, and how test automation can help.
Testing is not a choice
Testing is important. It helps enforce business rules, checks the quality of development work, and reduces the chance of platform releases breaking things.
To ensure the smooth operation of your organizations and promote good practice, Salesforce enforces code-coverage requirements on any Apex code you want to deploy to Production.
As the Salesforce developer site puts it: “to facilitate and promote the development of robust, error-free code, Apex Code requires the creation and execution of unit tests. Writing test methods should be a critical part of Force.com development and they are required to ensure your success.” (source)
Testing prior to deployments is well-known and widely adopted, with many teams striving for 100% code coverage. What is less well-known is that it is equally important to regularly re-run your tests in Production after you have deployed.
Let’s look at why.
Changes in your orgs can cause silent test failures
As a productive platform, Salesforce encourages end users, as well as admins and developers, to make changes to their orgs. That means changes will be made directly in your production environment, no matter how stringent your release processes. Giving end users this ability is an empowering aspect of the platform, and not something we think you should try to prevent. It does pose certain challenges, however, which need to be carefully managed.
Aside from being difficult to track, undocumented changes may cause your existing unit tests to fail. For example, the interaction between workflows and Apex code means it’s easy to end up with a unit test that passes when deployed to Production, then later fails when someone adds a new workflow or validation rule.
There is no easy way to detect these lurking test failures, and undocumented changes can lie hidden until it comes to deployment time - causing unexpected failures and a few extra grey hairs.
Given the ability for users to make changes to environments, and the importance of regular testing to maintain test integrity, why isn't it common practice?
Regular testing is difficult
Simply put, running unit tests and tracking the results in Salesforce is difficult. A lack of in-built test automation means many teams decide not to re-run unit tests after a deployment, and it’s all too easy to forget about them altogether.
Although tests are automatically run when pushing a package to Production, there is no easy way to manage testing in your orgs once the changes have been deployed. Testing can be manually initiated for individual orgs via the Apex Test Execution page, but if you’ve got a complex organization the process can take hours and it often falls down the priority list.
So how can teams easily run unit tests in their Production environments to stay aware of any changes?
Test automation is the answer
Running Production tests on a regular cadence, with automatic reporting and failure tracking, and an easy way to stay informed of the latest results, is what all Salesforce teams should aspire to.
The new test runner feature in Gearset was designed to meet exactly this need. Simply choose an organization and Gearset will automatically run all unit tests every 24 hours, and notify you of the results. You can choose email, SMS, Slack and Chatter notification options to stay informed in the way that works for you. A full test history is saved and you can easily dive in to find exactly which tests failed, and why.
It’s a great way to rapidly improve deployment success rates and stay aware of the state of your orgs, without having to manually compare org differences or track every change by hand.
To get started with the test runner on Gearset, log in to the app and navigate to the Schedule tests menu to create your first monitoring job.
We’ll be diving into more detail on how to setup your first test monitoring jobs in our next blog post.