Sub topic 14
Choosing a level for each pipeline stage
This chapter assumes you know the four test levels, the 75% coverage rule, and the sf project deploy flags covered earlier. It shows how to match a level to each stage of a pipeline.
What each stage needs
Each stage answers a different question, so it needs a different level of proof.
- Local development: "Does my change compile and deploy?" Speed matters most.
- CI on a pull request: "Do the tests for this change pass?" Feedback must arrive in minutes.
- Pre-production validation: "Will this release pass the production gate?" It must match production rules exactly.
- Production release: "Can we ship now?" The release window should be short.
A mapping to start from
| Stage | Target | Level | Why |
|---|---|---|---|
| Local development | Scratch org or developer sandbox | `NoTestRun` | Fastest. Sandboxes and scratch orgs allow it. |
| Pull request check | Integration sandbox | `RunSpecifiedTests` | Runs only the tests for the changed classes. |
| Release validation | Production (validate only) | `RunLocalTests` | Same gate as the real release. |
| Release | Production | Quick deploy | Reuses the passing validation. |
RunAllTestsInOrg rarely fits a pipeline stage. Managed package tests are outside your control, and a failure in them can block a release you cannot fix. Use it for occasional audits.
Watch the coverage rules per level
The levels do not check coverage the same way. RunSpecifiedTests requires each class and trigger in the deployment to have at least 75% coverage from the tests you named. RunLocalTests requires 75% coverage across the org.
So a pull request check that passes with RunSpecifiedTests can still fail later under RunLocalTests. This happens if the named tests do not cover every deployed class, or if your change lowers the org-wide total. The production validation step catches this before release day.
Example pipeline commands
The commands below use the flags from the earlier chapters.
# Pull request check: only the tests for the changed class
sf project deploy start --source-dir force-app --target-org ci-sandbox \
--test-level RunSpecifiedTests --tests AccountServiceTest
# Release validation: full local test run, nothing saved
sf project deploy validate --source-dir force-app --target-org my-prod \
--test-level RunLocalTests
# Release: reuse the validation using its job ID
sf project deploy quick --job-id <validated-deploy-job-id> --target-org my-prodDecision criteria
- Is the target production and does the package contain Apex? If so,
NoTestRunis not accepted. UseRunLocalTestsorRunSpecifiedTests. - Do you need fast feedback? Name the relevant test classes and accept the per-class coverage check.
- Do you need confidence in the whole org? Use
RunLocalTestsand accept the longer run. - Is the release window tight? Validate earlier, then quick deploy.
If you set no level, the defaults apply: NoTestRun for a sandbox, RunLocalTests for production with Apex. Setting the level explicitly in pipeline scripts makes each stage's behavior visible and stops it changing with the target org.
Exercise
A pipeline's pull request job deploys with RunSpecifiedTests and passes. The production validation with RunLocalTests then fails on coverage, even though no test failed. Give one likely cause and the stage that should catch it.
Show answer
The specified tests covered the changed classes, but the org-wide coverage under RunLocalTests fell below 75%, or other classes had gaps the named tests never checked. The production validation stage (validate with RunLocalTests) should catch it before release, so the fix is to add or improve tests and validate again.