Sub topic 9
Apex test levels and coverage in CI
This chapter assumes you can write Apex tests and run sf commands from a terminal. You should also know that a deployment to production must meet a coverage requirement. Here you will choose a test level for each pipeline stage and read the coverage output.
The four test levels
The --test-level flag on sf project deploy start controls which Apex tests run during a deployment.
| Level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | No tests | Deploying to a sandbox or scratch org where speed matters. Not allowed for production deployments that include Apex. |
| `RunSpecifiedTests` | Only the test classes you name | Fast feedback on a small change. |
| `RunLocalTests` | All tests in your org except those from managed packages | The standard choice for pull request validation and production deploys. |
| `RunAllTestsInOrg` | Every test, including managed package tests | Rare. Slow, and it can fail because of code you do not own. |
If you do not set a level, Salesforce picks a default. In a production org, a deployment containing Apex runs local tests. In a sandbox, no tests run. Set the level explicitly so the pipeline behaves the same way every time.
Coverage rules
Production deployments that include Apex require at least 75% overall code coverage across the org's Apex classes and triggers. Every trigger must also have some coverage. Coverage is counted by lines executed during tests, not by assertions. A test with no assertions can still raise coverage, so coverage is a minimum bar and does not prove the code is correct.
Choosing a level per stage
A common pattern is to run RunLocalTests in the pull request check, so problems appear before merge. Production deploys use the same level, which means a change that passed validation will pass again. Developer sandboxes can use a lighter level.
Specifying tests
With RunSpecifiedTests, pass each class with --tests:
sf project deploy start \
--source-dir force-app \
--test-level RunSpecifiedTests \
--tests AccountServiceTest \
--tests OpportunityTriggerTest \
--target-org target \
--wait 60Reading coverage output
To see coverage outside a deployment, run the tests directly. Use --code-coverage to include per-class percentages and --result-format human for readable output.
sf apex run test \
--test-level RunLocalTests \
--code-coverage \
--result-format human \
--wait 20 \
--target-org targetFor machine-readable results, add --output-dir test-results and --result-format json. You can then upload that folder as a workflow artifact for later review.
Common errors
- Coverage under 75%. A new class lacks tests. Add tests that exercise its branches, then rerun.
- Tests pass locally but fail in CI. Tests may depend on org data. Use
@TestSetupor create records in the test itself instead of relying on existing data. - Timeouts. The
--waitvalue is in minutes. If it is too low, the CLI stops waiting while the org keeps running the job.
Exercise
A pull request changes one Apex class and its test. Your pipeline validates with RunSpecifiedTests naming only that test class. The check passes. After merge, the production deploy with RunLocalTests fails. Give one likely reason.
Show answer
RunLocalTests runs every local test in the org. The change may have broken an unrelated test that RunSpecifiedTests never ran, or overall org coverage may have fallen below 75%. Validating with RunLocalTests would have caught either problem before merge.