Sub topic 6
RunSpecifiedTests: targeted test runs
This chapter assumes you can write a basic @IsTest class, know the difference between a sandbox and production, and can run sf commands. It covers the second test level, RunSpecifiedTests, which runs only the test classes you name.
What the level does
With RunSpecifiedTests, the deployment runs the test classes you list and nothing else. A deployment with RunLocalTests can run hundreds of classes. This level can run one or two, so it finishes much sooner.
You choose the tests, so you carry the responsibility for choosing well. Salesforce does not work out which tests relate to your changes.
The command
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTest--tests is used together with --test-level RunSpecifiedTests. To name more classes, repeat the flag:
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTest --tests ContactServiceTestIf you choose this level but name no tests, the deployment cannot run, so always pair the two flags.
A class and the test that covers it
This pair is the example used throughout this tutorial. The test creates its own data and asserts a result. That is why the deployment can count the class as covered.
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');
}
}Deploying AccountService with --tests AccountServiceTest runs that one test class.
The coverage rule is per class
The 75% rule works differently here than in RunLocalTests. When you deploy to production with RunSpecifiedTests, every Apex class and trigger in the deployment package needs at least 75% coverage. Only the tests you named count toward that figure.
Two points follow from this:
- Coverage is measured per class and trigger, not as an org-wide average. One class at 40% fails the deployment even if everything else is at 100%.
- Coverage from tests you did not name does not help. A class might be covered by a test elsewhere in the org, but if you leave that test out, the deployment does not count it.
Classes that are already in the org but not in your package are not checked.
When to use it
- A small, well-understood change where you know exactly which test class covers it.
- A production release where a full local run would take too long, provided the named tests reach 75% on every class you deploy.
Avoid it when a change could affect code you are not testing. Skipping unrelated tests also skips the warning that your change broke them.
Common mistakes
| Mistake | Result |
|---|---|
| Naming a test that covers only part of a class | The class falls under 75% and the deployment fails |
| Assuming org-wide coverage counts | Per-class coverage is what is checked |
| Forgetting to add a new class's test to the list | The new class has no coverage |
Exercise
You deploy ContactService and AccountService to production with --test-level RunSpecifiedTests --tests AccountServiceTest. ContactService is covered at 90% by ContactServiceTest, which is in the org but not named. What happens, and how do you fix it?
Show answer
The deployment fails because ContactService has no coverage from the named tests, and only named tests count. Add a second flag, --tests ContactServiceTest, so that its coverage is counted.