Sub topic 14
Choosing a Level for Each Pipeline Stage
This chapter assumes you know the four test levels, the 75% coverage rule, the defaults by org type, and the sf project deploy test flags covered in earlier chapters. Here you use them to design a pipeline: which level to run at each stage, and why.
Start from what each stage must prove
Each stage in a pipeline answers a different question, and each test level costs a different amount of time. Match the level to the question.
- Local development: Does my change deploy at all? You want feedback in seconds.
- Pull request validation: Do the tests related to my change pass? You want results in minutes.
- Integration or release candidate: Does the whole org still behave, and will production accept this package?
- Production release: Can I ship this package inside a short change window?
A level that is too weak lets a defect reach production. A level that is too strong makes developers wait and then stop running it.
The levels compared
| Level | What runs | Coverage enforced | Typical stage |
|---|---|---|---|
| NoTestRun | Nothing | No | Local dev, scratch orgs, throwaway sandboxes |
| RunSpecifiedTests | Only the classes you name with `--tests` | Each class and trigger in the package needs 75% from those tests | Pull request checks, hotfixes |
| RunLocalTests | All tests in your org except managed package tests | 75% across the org's Apex | Release validation, production |
| RunAllTestsInOrg | All tests, including managed package tests | 75% across the org's Apex | Rare audits, package upgrade checks |
Coverage is enforced on production deployments. A sandbox deployment that passes does not prove that production will accept the same package. That gap shapes most of the decisions below.
Stage 1: local development
Developers deploy often, so tests should not slow them down. In a scratch org or developer sandbox, NoTestRun is the default and is the fastest option. Run the relevant tests yourself with the test runner when you need them, rather than on every deploy.
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level NoTestRunNoTestRun is not accepted for production deployments that include Apex. Keep it out of any script that can target production. Add a guard, or give production-bound jobs their own commands (shown later in this chapter).
Stage 2: pull request validation
A pull request check should be quick and specific to the change. RunSpecifiedTests fits, because it runs only the classes you name.
sf project deploy start --source-dir force-app --target-org my-ci-sandbox --test-level RunSpecifiedTests --tests AccountServiceTestRepeat --tests to name more classes, for example one flag per test class touched by the change.
Two things go wrong at this stage.
- Missing tests. If a changed class has no test in your list, nothing exercises it. Build the list from the change, using a naming convention such as
<ClassName>Test, or from a mapping file in the repository. - Per-class coverage. With
RunSpecifiedTestsagainst production, each class and trigger in the package needs 75% from the tests you named. An org with 90% overall coverage can still fail if the one class you changed sits at 60%.
Because this stage targets a sandbox, it checks that the tests pass. It does not check production coverage rules. Treat it as a fast filter, not as a release gate.
Stage 3: release candidate validation
Before a release, you need confidence that production will accept the package. Do not use a sandbox deploy to answer that question. Run a validation-only deployment against production, or against a full-copy sandbox that mirrors it, with RunLocalTests.
sf project deploy validate --source-dir force-app --target-org my-prod --test-level RunLocalTestsRunLocalTests runs every test your org owns, so it also catches regressions in code you did not touch. The cost is time. In a large org the run can take an hour or more, so run it on a schedule or on the release branch, not on every commit.
Stage 4: production release with quick deploy
A validation passes the tests without saving any changes. Quick deploy then reuses that result and skips the test run. Your downtime shrinks from the length of the test run to the length of the deployment itself.
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-prodQuick deploy has conditions you must check in the pipeline.
- The validation must have passed its tests and met the coverage requirements.
- The validated deployment must be recent. The Salesforce documentation sets a window of 10 days. Check the current documentation for your release.
- The package that goes live is the one that was validated. If someone merges more changes after validation, you must validate again.
A validation also goes stale in practice. If another team deploys to production between your validation and your quick deploy, the validated result may no longer describe the org you are deploying to.
Where RunAllTestsInOrg fits
RunAllTestsInOrg adds the tests from installed managed packages. Those tests belong to the package vendor, so a failure is often outside your control and unrelated to your change. Use it when you want evidence about the whole org, such as before a major platform upgrade or after installing a package. Do not make it a routine gate.
The pipeline at a glance
A pipeline script that enforces the choices
The script below picks the level from the stage name, and refuses NoTestRun for production. It assumes jq is installed and that TARGET_ORG is set by your CI system.
#!/usr/bin/env bash
set -euo pipefail
STAGE="$1" # dev | pr | validate | release
TARGET_ORG="${TARGET_ORG:?Set TARGET_ORG}"
case "$STAGE" in
dev)
sf project deploy start --source-dir force-app \
--target-org "$TARGET_ORG" --test-level NoTestRun
;;
pr)
sf project deploy start --source-dir force-app \
--target-org "$TARGET_ORG" --test-level RunSpecifiedTests \
--tests AccountServiceTest
;;
validate)
sf project deploy validate --source-dir force-app \
--target-org "$TARGET_ORG" --test-level RunLocalTests --json \
| jq -r '.result.id' > validated-job-id.txt
;;
release)
sf project deploy quick --job-id "$(cat validated-job-id.txt)" \
--target-org "$TARGET_ORG"
;;
*)
echo "Unknown stage: $STAGE" >&2
exit 1
;;
esacThe validate stage saves the job ID, and the release stage consumes it. In a real pipeline you would store that file as a build artifact so a later job can read it. Check the JSON field name against your CLI version, because output shapes can change between releases.
Common mistakes
- Validating in a sandbox and assuming production will pass. Coverage rules apply to production. Validate against production, or a copy that matches it, before release.
- Using
RunSpecifiedTestsfor the release. It can pass while org-wide regressions go undetected. It also holds each deployed class to 75% individually. - Hardcoding one level for every stage. A single level is either too slow for developers or too weak for production.
- Skipping the freshness check on quick deploy. An old or stale validation job ID causes the quick deploy to fail at the worst moment, in the change window.
- Tests that depend on org data. A test that passes in one sandbox and fails in production blocks the release. Tests should create their own data, as
AccountServiceTestdoes.
Exercises
Exercise
Your team deploys to production on Friday evenings, and the full RunLocalTests run takes 70 minutes. Your change window is 30 minutes. Which commands and which stage design fit, and what must be true for it to work?
Show answer
Run sf project deploy validate with --test-level RunLocalTests earlier in the week or on the release branch. During the window, run sf project deploy quick --job-id <id> to deploy without re-running tests. This works only if the validation passed its tests and coverage, it is within the documented 10-day window, and the deployed package has not changed since validation.
Exercise
A pull request check using RunSpecifiedTests against a sandbox passes. At release, the production validation with RunSpecifiedTests names the same test class and fails on coverage for a class the pull request changed. Give two likely reasons.
Show answer
First, coverage rules are enforced on production, not on the sandbox, so the sandbox pass did not check them. Second, with RunSpecifiedTests each class and trigger in the package needs 75% coverage from the named tests, so a changed class with weak coverage fails even if the org as a whole is above 75%. Fix it by adding tests for that class and naming them in --tests, or by validating with RunLocalTests.