To reduce Apex component deployment time, use the new RunRelevantTests
test level to run only the tests relevant to your code changes. This feature automatically
determines which tests to run based on an analysis of the deployment payload and the payload
dependencies. For fine-grained control, you can annotate test classes so that they either run
regardless of the deployment payload, or run when specified components are new or modified. This
targeted approach boosts the speed and reliability of your deployment while maintaining code
quality and coverage requirements.
Where: The RunRelevantTests test level is available in all API
versions. The @IsTest(critical=true) and @IsTest(testFor='...')
annotations are available in API version 66.0 and later. The feature is available in Lightning
Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer
editions.
RunRelevantTests test level and the associated
@IsTest() annotations are pilot or beta services that are subject to the Beta
Services Terms at Agreements — Salesforce.com or a written Unified Pilot Agreement if
executed by Customer, and applicable terms in the Product Terms Directory. Use of these pilot or beta
services are at the Customer’s sole discretion.
Why: The new RunRelevantTests test level addresses the limitations of
the existing RunLocalTests and RunSpecifiedTests test levels.
RunLocalTests, the default test level for Apex production deployments, runs all
Apex tests in the org except the ones that originate from installed managed and unlocked
packages. In orgs with extensive test suites, RunLocalTests can result in long
deployment times even for small changes. In contrast, RunRelevantTests runs only
Apex tests relevant to modified components in the deployment payload, so the number of tests that
run scales proportionally to the deployment size. RunRelevantTests also
addresses a drawback of the RunSpecifiedTests test level. With
RunSpecifiedTests, you must manually determine which tests are applicable to
the changes, which often requires custom DevOps tooling. In contrast, the
RunRelevantTests test level engine analyzes the deployment payload and
automatically runs a subset of tests based on that analysis.
How: To specify the RunRelevantTests test level when you deploy Apex
components, use one of these methods.
- To deploy by using a file-based call, set the
testLevelfield on theDeployOptionsobject toRunRelevantTests. Then passDeployOptionsas an argument to thedeploy()method. - To deploy by using the
deployRequestREST resource, define adeployOptionsobject in the POST request body and set itstestLevelfield toRunRelevantTests. - To deploy by using the Salesforce CLI, use the
sf project deploy startcommand and set the--test-levelflag toRunRelevantTests.
If a test class must always run regardless of the classes or triggers in the deployment
payload, annotate the test class with @IsTest(critical=true).
If a test class must always run whenever specified classes or triggers are new or changed in
the deployment payload, annotate the test class with
@IsTest(testFor='ApexClass:ClassName,
ApexTrigger:TriggerName').