Sub topic 13
Validation and Test Gates Before Release
This chapter assumes you can create work items, commit changes, open pull requests and promote work items through pipeline stages in DevOps Center. It also assumes you know the basics of Apex tests and code coverage. Here you will learn what is checked at each point in a pipeline, which test level applies, and how to add checks that DevOps Center does not run for you.
What a gate is
A gate is a check that must pass before a change moves to the next stage. DevOps Center gives you three places to put one:
- The pull request, where reviewers and automated checks look at the code.
- The deployment to a stage's org, where Salesforce validates metadata and can run Apex tests.
- The promotion step, where a person decides to continue.
Gates catch different problems. A pull request review finds design and security issues. A deployment catches missing dependencies and failing tests. A person at promotion decides whether the timing is right.
What DevOps Center validates
When you promote a work item, DevOps Center merges the work item branch into the stage's branch and deploys the changes to that stage's org. Salesforce checks the metadata during the deployment. Common failures at this point include:
- A component references another component that is not in the deployment, such as a custom field used by a flow.
- A component fails a platform rule, such as an invalid formula.
- An Apex test fails, or code coverage is too low.
If the deployment fails, the work item stays at its current stage. Fix the problem in the development environment, commit the fix to the same work item branch, and promote again.
Test levels and the production rule
Salesforce decides which Apex tests run based on a test level. The table lists the levels you will meet.
| Test level | What runs | Typical use |
|---|---|---|
| NoTestRun | No tests | Sandboxes and metadata-only changes. Not allowed for Apex deployments to production. |
| RunSpecifiedTests | Only the tests you name | Fast checks on a small change. |
| RunLocalTests | All tests in your org, excluding managed package tests | Standard level for validation before production. |
| RunAllTestsInOrg | All tests, including managed packages | Rare, slow, used for full audits. |
Production has two fixed rules. A deployment that contains Apex classes or triggers must run tests. The tests must reach at least 75 percent overall Apex code coverage, and every trigger needs some coverage. Sandboxes do not enforce the coverage rule, so a change can pass in UAT and fail in production. Test earlier to avoid that surprise.
Running a validation yourself
You can check a change against a stage's org without deploying it. The Salesforce CLI does this with a validation-only deployment (a dry run). This is useful when you want to know whether a pending promotion will pass before you start it.
sf org login web --alias uat
sf project deploy start --source-dir force-app --target-org uat --dry-run --test-level RunLocalTestsThe command compiles the metadata and runs the tests in the uat org, then rolls everything back. Nothing changes in the org. A dry run reports the same errors a real deployment would report, but only for the source directory you pass.
Seeing what a promotion would carry
Before you promote, compare the branches of two stages. The commits that appear in the earlier stage but not in the later one are the changes you would release.
git fetch origin
git log --oneline origin/main..origin/UATIf the list includes a commit you did not expect, stop. It usually means another work item reached UAT earlier and is still waiting for approval. Releasing the pipeline stage then releases that work too, as the next section explains.
Gates and bundling
DevOps Center promotes work items, but each stage branch holds the combined result of everything promoted so far. When a work item was merged into UAT before yours, and your work item depends on it, the two must be promoted together. If you skip the first one, your promotion can fail because a dependency is missing.
Keep these habits:
- Promote work items in the order they were merged.
- Promote small groups of related work items together when they depend on each other.
- Do not leave old work items sitting in a stage for weeks. The longer they wait, the more the stage drifts from production.
Adding checks DevOps Center does not run
DevOps Center runs deployments and Apex tests. It does not run static analysis, linting or UI tests as part of a promotion. Add these checks in the Git provider instead, so they run on the pull request:
- Run a code scanner such as Salesforce Code Analyzer on changed files.
- Run Lightning Web Component unit tests with Jest.
- Require the checks to pass in the branch protection settings of the repository.
- Require at least one reviewer approval before merging.
Branch protection is the control that turns a check into a real gate. Without it, a failed check is only a warning that people can ignore.
A .forceignore file keeps local tooling files out of deployments, so your lint settings and test folders do not travel to the org:
# .forceignore
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**
.sfdx
.sfJest tests live in __tests__ folders. Ignoring them keeps the deployment small and avoids deploying files that Salesforce does not need.
Common errors
- Coverage passes in UAT and fails in production. UAT had no coverage rule or ran fewer tests. Use RunLocalTests in a pre-production stage.
- Tests fail only in the deployment. The sandbox has test data or settings that the target org lacks. Make tests create their own data and avoid relying on existing records.
- Tests pass alone and fail together. Two tests depend on shared data or on run order. Isolate each test's data.
- The deployment times out or runs very long. An org with many tests can take hours with RunLocalTests. Use RunSpecifiedTests in early stages and keep the full run for the last one.
- A required check was skipped. Branch protection was not enabled on the stage branch. Enable it for every branch that maps to a stage.
Choosing a gate strategy
Match the strictness of the gate to the cost of failure.
- Early stages should be fast. Run specified tests so developers get feedback in minutes.
- The stage just before production should mirror production. Use RunLocalTests and real-sized data if you can.
- Production promotions should happen at planned times, after a validation that passed recently on the same content.
Be consistent. A gate that people can bypass during a busy week will be bypassed every week.
Exercise
Your team promoted a work item with a new Apex class to UAT without problems, but the promotion to production failed. The error says the org's code coverage is 62 percent. Give two reasons the UAT promotion could pass, and describe two changes to the pipeline that would catch the problem before production.
Show answer
UAT passed because sandboxes do not enforce the 75 percent coverage rule, and the UAT deployment probably ran no tests or only a few specified tests, so low coverage was never measured. To catch it earlier, run RunLocalTests in the stage just before production (or in UAT), and use a CLI dry run with --test-level RunLocalTests against that org before promoting. You can also add a pull request check that runs the Apex tests so coverage problems appear during review.
Exercise
You run git log --oneline origin/main..origin/UAT and see five commits, but your team planned to release only one work item. What does this tell you, and what should you do before promoting?
Show answer
Four other commits are already in UAT but not in main, so promoting would release them too. Find which work items they belong to, confirm whether they are approved and tested, and either promote them together or resolve them first. Do not promote until the contents match what the team intends to release.