Sub topic 11
Setting test levels with sf project deploy
This chapter assumes you can write a basic Apex test class, know the difference between a sandbox and production, and have authorized an org with the sf CLI. It covers the flags that choose which tests run during a deployment.
The two flags
sf project deploy start uses two flags to control tests:
--test-levelselects one of the four levels:NoTestRun,RunSpecifiedTests,RunLocalTestsorRunAllTestsInOrg.--testsnames the test classes to run. Use it only withRunSpecifiedTests. Repeat the flag to name more than one class.
If you leave out --test-level, the org's default applies. A production deployment that contains Apex runs RunLocalTests. Other deployments, such as to a sandbox, run no tests.
Skipping tests in a sandbox
To get a fast deployment to a sandbox, skip the tests:
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level NoTestRunProduction rejects this level when the deployment includes Apex. Use it only for sandboxes and scratch orgs.
Running named tests
To run only specific tests, use RunSpecifiedTests with --tests:
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTestHere the deployment runs AccountServiceTest and nothing else. The 75% coverage rule still applies, but it is measured from the tests you named. Each Apex class and trigger in the deployment needs at least 75% coverage from those tests. If you name a test that covers only part of your changes, the deployment fails on coverage even though every test passed.
To name several classes, repeat the flag:
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTest --tests ContactServiceTestRunning all local tests
RunLocalTests runs every test in the org except tests from installed managed packages:
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunLocalTestsThis is the safe choice for production releases. Use RunAllTestsInOrg only when you also want the managed package tests.
The test the deployment counts
The test class below creates its own data and asserts a result. Without an assertion, a test can cover lines and still tell you nothing.
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');
}
}Reusing a passing run
To avoid running the tests twice, validate first. Then deploy the same changes without re-running tests:
sf project deploy validate --source-dir force-app --target-org my-prod --test-level RunLocalTests
# The command prints a job ID. Use it to deploy without re-running the tests:
sf project deploy quick --job-id <validated-deploy-job-id> --target-org my-prodValidation saves nothing to the org. The quick deploy must happen within the allowed window after the validation passes.
Common mistakes
- Passing
--testswith a level other thanRunSpecifiedTests. The class list has no effect, so check the level first. - Misspelling a class name in
--tests. The deployment reports that the class was not found. - Using
NoTestRunagainst production with Apex in the package. The deployment is rejected.
Exercise
You deploy to production with --test-level RunSpecifiedTests --tests AccountServiceTest. All tests pass, but the deployment fails. What is the most likely cause, and how do you fix it?
Show answer
The tests that ran did not cover at least 75% of each Apex class or trigger in the deployment. Add the test classes that cover the other classes with more --tests flags, or switch to RunLocalTests.