Sub topic 6
RunSpecifiedTests: targeted test runs
RunSpecifiedTests is the test level where you name the Apex test classes a deployment runs. Nothing else runs. This chapter assumes you can write a basic @IsTest class, know the difference between a sandbox and production, and can run sf commands against an authorized org.
What the level does
With RunSpecifiedTests, the deployment runs only the test classes you list. It skips the rest of the org's tests, including tests that have nothing to do with your change. This makes it the fastest level that still enforces coverage, so it is the usual choice for a small production release in an org with a large test suite.
The trade-off is that you choose the tests, and Salesforce checks the result strictly. A missing or weak test class fails the deployment, and so does a test that exists in the org but that you forgot to name.
The following table shows how this level compares with the two levels on either side of it.
| Level | Tests that run | Coverage rule |
|---|---|---|
| `NoTestRun` | None | Not checked |
| `RunSpecifiedTests` | Only the classes you name | Each deployed class and trigger needs at least 75% on its own |
| `RunLocalTests` | All tests in your org, except installed managed package tests | 75% overall across the org's Apex |
The middle row is the one to remember. The next sections explain why.
The example classes
The examples use the class and test from this tutorial's running example.
public with sharing class AccountService {
public static Integer countByIndustry(String industry) {
return [SELECT COUNT() FROM Account WHERE Industry = :industry];
}
}
@IsTest
private class AccountServiceTest {
@IsTest
static void countsMatchingAccounts() {
insert new Account(Name = 'Acme', Industry = 'Energy');
Test.startTest();
Integer result = AccountService.countByIndustry('Energy');
Test.stopTest();
Assert.areEqual(1, result, 'Expected one Energy account');
}
}The test creates its own data, calls the method, and asserts a result. Because it calls countByIndustry, the test covers every executable line of AccountService.
Setting the level in the CLI
Pass the level with --test-level and the class names with --tests.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTestTo name more than one class, repeat the --tests flag once per class.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTest --tests ContactServiceTestThree rules apply:
--testsonly makes sense with--test-level RunSpecifiedTests. If you give--testswith another level, the CLI reports an error instead of quietly ignoring the flag.- If you choose
RunSpecifiedTestsand name no classes, the deployment has nothing to run. Always pair the two flags. - Class names are case-insensitive in Apex, but use the exact spelling from your source. This avoids confusion when you read logs and scripts later.
The per-class 75% rule
This rule is the most common source of surprise with the level.
RunLocalTests measures coverage across the whole org. If your org averages 82%, one weakly covered class can hide inside that average. RunSpecifiedTests has no such cushion. Salesforce computes coverage for each class and trigger in the deployment package individually. Each one must reach at least 75% from the tests you named.
Three details follow from this:
- Only the executed tests count. A class might have 90% coverage from a test you did not name. That test does not run in this deployment, so its coverage does not count.
- Only the deployed components are checked. Classes that are in the org but not in your package are not held to the 75% figure for this deployment.
- Coverage can come from indirect calls. If
AccountServiceTestcalls a trigger that calls a helper class, the lines run in the helper count toward the helper. A test class does not have to share a name with the class it covers.
The next diagram shows how a deployment with RunSpecifiedTests reaches a pass or a fail.
A failing example
Suppose your package contains AccountService and a new class ContactService.
public with sharing class ContactService {
public static Integer countByLastName(String lastName) {
return [SELECT COUNT() FROM Contact WHERE LastName = :lastName];
}
public static Boolean hasContacts(String lastName) {
return countByLastName(lastName) > 0;
}
}You deploy both classes but name only AccountServiceTest. That test passes and covers AccountService fully. It never calls ContactService, so that class has 0% coverage. The deployment fails with a coverage error for ContactService, even though a good ContactServiceTest might exist in your org.
The fix is to name the second test class too, as in the two-class command shown earlier. This is why many teams generate the --tests list from the package contents instead of typing it by hand.
Choosing which tests to name
A reliable list has three parts:
- One test class for every Apex class and trigger in the package. This satisfies the per-class rule directly.
- Tests for code that your change depends on. If you changed a utility class that five other classes call, run the tests of those callers. They exercise the changed behavior even though their own files are not in the package.
- No unrelated tests. Each extra class slows the deployment and adds a chance of failure from something you did not touch.
Be careful with a test that fails for reasons outside your change, such as one that depends on org data or on a validation rule added last week. Naming it makes your deployment fail. That is correct behavior, but it can surprise you in a release window.
Where the test classes come from
The test classes you name must be available when the tests run. There are two ways this works:
- The test class is already in the target org.
- The test class is part of the package you are deploying. Salesforce deploys the components first, then runs the tests in the destination.
If a named class is in neither place, the deployment fails because the test cannot be found. Typos in --tests produce the same error.
Where you can use it
RunSpecifiedTests is accepted for production deployments that contain Apex. This is useful because NoTestRun is not accepted there. In a sandbox you can use any of the four levels.
The next command is the contrast case. It skips all tests, which is fast but only valid in a sandbox or a deployment without Apex.
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level NoTestRunFailure behavior and validation
A failed named test fails the deployment. A deployment in Salesforce is all or nothing, so one failure means that no component is saved. You fix the test or the code and run it again.
You can combine RunSpecifiedTests with a validation. A validation runs the same tests and the same checks but saves nothing.
sf project deploy validate --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTest --tests ContactServiceTestWhen a validation passes, the command prints a job ID. You can then perform a quick deploy that reuses the passing result and does not run the tests a second time.
sf project deploy quick --job-id <validated-deploy-job-id> --target-org my-prodQuick deploy only works within the allowed window after the validation, and only for the same changes. Later chapters cover that window.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Coverage error for one class | Named tests do not exercise that class enough | Add its test class to `--tests` or improve the test |
| Test class not found | Misspelled name, or class is not in the package or the org | Check the spelling and the `--source-dir` contents |
| CLI error about `--tests` | Used `--tests` with a different `--test-level` | Set `--test-level RunSpecifiedTests` |
| Deployment fails on a test you did not change | Named test depends on org data or settings that differ in the target | Make the test create its own data |
Summary of the trade-offs
RunSpecifiedTests gives you speed and control. It also moves responsibility to you. You must name enough tests to reach 75% on every deployed class and trigger, and every named test must pass. Use it when the change is small and you know which tests matter. Use RunLocalTests when you want the org to decide.
Exercise
Your package contains the classes OrderService and OrderTrigger's helper OrderHelper. You deploy to production with --test-level RunSpecifiedTests --tests OrderServiceTest. OrderServiceTest passes, and OrderService has 95% coverage. OrderHelper has 90% coverage in the org from OrderHelperTest, which you did not name. Will the deployment succeed? Explain.
Show answer
It will fail. With RunSpecifiedTests, each deployed class and trigger must reach at least 75% coverage from the tests that run in this deployment. OrderHelperTest does not run, so its coverage does not count. Unless OrderServiceTest also exercises OrderHelper to at least 75%, OrderHelper fails the per-class rule. Add --tests OrderHelperTest to fix it.