Salesforce Apex deployments to production environments require all test classes to run before any code can be deployed. This mandatory test execution is the most common reason production deployments are slower than sandbox deployments. While there is no single root cause for slow deployment speed, several factors contribute — including query complexity in test classes, batch class configuration, and test data setup patterns.
This article provides recommendations to optimize Salesforce Apex test classes and reduce deployment time.
The following approaches can significantly reduce Apex production deployment time.
Always include a WHERE clause in SOQL queries and ensure they operate on a small, specific subset of data. Queries without WHERE clauses or those scanning large data sets increase execution time.
Avoid querying the database in test classes when you can create test data objects directly in memory.
Instead of querying:
Account acc = [Select name from Account where site='Toronto'];
Create objects directly in memory:
List<Account> accList = new List<Account>();
Account acc;
For (int i=0 ; i<10 ; i++) {
acc = new Account();
acc.name = 'test';
acc.site = 'Toronto';
accList.add(acc);
}
Creating objects in memory eliminates database query overhead during test execution, which reduces deployment time.
Batch Apex classes use Test.startTest() and Test.stopTest() methods to execute asynchronous code synchronously during tests. Rather than having separate test methods for each batch class, consolidate all batch class tests within a single startTest/stopTest block. This reduces the overhead of multiple test execution contexts.
Instead of creating new objects in every test method, create a static utility class with methods such as getNewAccount() to instantiate test objects, or use the @testSetup annotation. Methods annotated with @testSetup create test records once per test class and make them accessible in every test method, reducing redundant data creation.
For additional reference, see Improve Performance with Custom Indexes using Selective SOQL Queries.
000384936

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.