In Salesforce, whenever Apex and triggers are deployed all of the apex tests are run as part of the deployment process. In an org with a lot of test classes and customizations it can take a really long time to run the tests which will increase the amount of time it takes for deployments.
There’s a lot of advantages to running all of the tests especially if they actually have asserts. The biggest advantage is that automated tests are proven to help reduce the number of errors that occur in a production system. Unfortunately, when user’s raise issues in production that require an immediate fix it can take hours to finally get a hot fix deployed to production.
Tips for Speeding Up Deployments
Speeding up the deployment time is possible, although it’s not going to be easy to do and it will be time consuming.
Modify Test Classes to Remove SeeAllData
By explicitly marking tests to use test data it reduces the amount of time that it takes to have tests execute. Basically all of the steps listed in the Salesforce Order of Execution occur except committing the records to the database.
Apex Classes created after version 23 are automatically using SeeAllData = false unless the class or methods are marked explicitly with SeeAllData = true. There are some objects that cannot be queried with SeeAllData = false like the Standard Pricebook.
Limit the Number of Users Inserted for Tests
Inserting users in tests seems to take a while. I recommend avoiding inserting users if you can. Users exist regardless of whether SeeAllData is set or not.
Avoid Using System.RunAs
From seeing thousands of tests running over the years, I’ve noticed that test methods with System.RunAs seem to execute slightly slower than methods without. If your organization has more than a thousand tests this can really start to add up if your company is doing frequent deployments.
Use @testSetup at the top of your test classes.
@testSetup is still relatively new to the apex language. By marking a method to be an @testSetup method, it can dramatically improve the performance of all test methods in that class because it will only need to insert the test data once.
Remove any Test Methods That Don’t Have Asserts and Aren’t Contributing to Code Coverage
If a test isn’t contributing anything to your org, there’s absolutely no point in having the test. I recommend removing any test that isn’t contributing because it’s just wasting time during deployments and when doing maintenance.
I consider myself to be a devops practitioner, so I like to deploy as frequently as possible to reduce the risk of large deployment problems.
In 2015, Salesforce added support to only execute some of the tests during a deployment called Quick Deployments. I really like being able to deploy a lot quicker, but there’s considerable risk that can come as a result of doing only quick deploys so I usually try to do full deployments regularly. I’ve put together a blog article with a few of the many other Salesforce deployment options.
Do you have any other tips that you think can help speed up deployments?