Tame the Deployment Tax: Faster, Smarter Test Runs
Salesforce orgs often face deployment slowdowns due to long-running and flaky Apex unit tests, especially as complexity grows. This content outlines practical methods to speed up and stabilize test runs by decoupling business logic from database calls using dependency injection, leveraging the upcoming RunRelevantTests feature to run only affected tests, and deferring test execution post-deployment to enable parallelization. Teams can adopt these patterns incrementally to improve deployment efficiency and maintain high development velocity in complex orgs.
- Use dependency injection to decouple business logic from database operations for faster unit tests.
- Apply database mocking to reduce test flakiness caused by external dependencies and row locks.
- Adopt RunRelevantTests to run only tests impacted by code changes and speed deployment tests.
- Defer test execution to post-deployment steps, especially in disposable orgs, to enable parallel testing.
- Annotate test classes properly with @IsTest(testFor=...) or critical=true for RunRelevantTests compatibility.
Almost every successful Salesforce implementation reaches a point where the development patterns that worked on day one start to break down. What began as a nimble environment eventually grows into a large, complex org (or set of orgs), often containing thousands of Apex classes and complex business logic. At that point, a once-straightforward “Run All Tests” deployment can balloon into a multi-hour process that grinds development to a halt. When this deployment tax becomes a burden, it changes the team’s relationship with the codebase. Maintaining high velocity requires architecting for deployment efficiency. Apex unit tests are commonly among the biggest bottlenecks in deployment. First, they take a long time to run. And second, tests that nondeterministically switch between passing and failing (sometimes called “flappers”) can cause deployments to fail hours into execution.