Deploy Apex Components Faster by Running Relevant Tests in Salesforce
Introduction
Have you ever waited ages for Salesforce to run every test in your org just to deploy a small change? You’re not alone. In large orgs with extensive Apex code, running all tests can dramatically slow down deployments, interrupt developer flow, and delay releases.
What if there was a smarter way to run only the tests that matter for your specific change?
In this article, you’ll learn:
- What Run Relevant Tests is
- Why it makes deployments faster
- How to use it with Salesforce CLI, API, and annotations
- Best practices and real-world tips
Table of Contents
- What Are Apex Test Levels in Salesforce?
- Why Run Only Relevant Tests Matters
- How Salesforce Determines Relevant Tests
- Using RunRelevantTests in Your Deployment
- Tips for Best Results
- FAQs
- Conclusion & Next Steps
What Are Apex Test Levels in Salesforce?
Before we dive in, it helps to understand how Salesforce runs tests:
Salesforce has several test execution options that define which tests run during deployment:
- RunLocalTests – runs all local tests not from installed packages
- RunSpecifiedTests – runs only the tests you list
- RunAllTestsInOrg – runs all tests in the org
- NoTestRun – runs no tests (only in non-production)
These test levels are used by Metadata API and tools like Salesforce CLI when deploying Apex.
Why Run Only Relevant Tests Matters
Traditionally, even if you changed only one line in a class, Salesforce might run thousands of tests — making deployments slow, especially in complex orgs.
The brand-new test level — RunRelevantTests — changes this by:
- Automatically running only the tests tied to the components you changed
- Reducing CI/CD wait times
- Improving developer productivity
- Maintaining code quality without unnecessary test execution
This feature was introduced in Salesforce Spring ’26 release as a way to optimize deployments.
💡 Analogy: It’s like scanning just the pages of a book that changed instead of rereading the entire book every time.
How Salesforce Determines Relevant Tests
Salesforce uses advanced logic to decide which tests are relevant:
✔️ Any test class in the deployment package
✔️ Tests that reference or depend on changed components
✔️ Tests annotated to always run or match changed code
You can influence relevance with two annotations (API v66+):
@IsTest(critical=true)— always runs@IsTest(testFor=’…’)— runs when those specific classes or triggers change
Example:
@IsTest(testFor='ApexClass:OrderService, ApexTrigger:AccountTrigger')
public class OrderServiceTest {
// test logic here
}
This tells Salesforce exactly which components this test is responsible for.
How to Use RunRelevantTests
Here are the common ways to use this test level:
1. Salesforce CLI
sf project deploy start --test-level RunRelevantTests
- Runs only relevant tests
- Faster feedback in your pipelines
2. REST API Deploy
In your REST deploy request:
{
"deployOptions": {
"testLevel": "RunRelevantTests"
}
}
3. Metadata API Deploy
Set testLevel=RunRelevantTests when constructing your deploy options in code.
Best Practices for Relevant Test Success
To make the most of this feature:
✔️ Annotate test classes with @IsTest(testFor=…) where possible
✔️ Mark always-run tests with critical=true
✔️ Keep tests focused on specific components
✔️ Use RunRelevantTests for small, frequent deployments
✔️ Reserve full test suites for major releases
FAQs
Q1: Do all tests still run if I use RunRelevantTests?
No — only tests tied to the changed components plus critical tests run with this level.
Q2: Can I use RunRelevantTests in production?
Yes — it’s supported for production deployments in Spring ’26 and later.
Q3: What happens if no relevant tests are found?
Salesforce still enforces coverage rules — tests that cover deployed code must run or deployment will fail.
Q4: Do I still need 75% code coverage?
Yes — Salesforce requires minimum coverage for deployed Apex, even if running only relevant tests.
Q5: Can I mix this with RunSpecifiedTests?
These options are separate; RunRelevantTests is the smarter, automated choice.
Conclusion
Running only relevant tests is a huge productivity boost for Salesforce developers. It:
- Saves time
- Reduces CI/CD bottlenecks
- Improves developer experience
- Keeps deployments fast but safe
If you’re working with large orgs or running frequent deployments, this should be part of your standard workflow today.