Sub topic 10
Automating Release Regression Tests
This chapter assumes you can use the Salesforce CLI, know how scratch orgs and sandboxes differ, and have written basic Apex tests. You will turn the manual regression checks from the previous chapters into a repeatable pipeline that runs against a preview environment.
Why automate
Each release gives you a short preview window. Running the same checks by hand in every preview org is slow, and people skip steps under time pressure. A scripted run gives you the same checks every time and a result you can compare with the last release.
Automation does not replace testing key user journeys with real people. It covers the parts that machines handle well: Apex tests, deployment validation, and a repeatable setup of a clean org.
What to automate
| Check | Tool | Runs against |
|---|---|---|
| Apex unit tests | `sf apex run test` | Preview sandbox or preview scratch org |
| Metadata deploys | `sf project deploy start --dry-run` | Preview sandbox |
| Flow tests | Flow tests run through Apex test level | Preview sandbox |
| UI journeys | A browser test framework of your choice | Preview sandbox |
Start with Apex tests. They are the cheapest to run and often catch behavior changes first.
The pipeline shape
Run the job on a schedule during the preview window, for example nightly. Also run it on pull requests, so a fix you make for the release does not break something else.
Creating a preview scratch org in CI
A scratch org definition with "release": "Preview" creates the org on the upcoming release.
{
"orgName": "Release Preview Org",
"edition": "Developer",
"release": "Preview"
}The CI job creates a short-lived org from that file:
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias preview-org \
--duration-days 7 \
--set-defaultKeep the duration short. The org only needs to live long enough for the test run.
Running the tests
Once the source is deployed, run all local tests and wait for the result:
sf apex run test \
--target-org my-preview-sandbox \
--test-level RunLocalTests \
--result-format human \
--wait 20RunLocalTests skips tests that come from managed packages, which you cannot change anyway. The --wait 20 option makes the command block for up to 20 minutes, so the CI job gets an exit code. A non-zero exit code fails the job.
For machine-readable output in a pipeline, switch the result format to junit or json and save the files as build artifacts. Most CI tools can display JUnit results directly.
Reading the results
A failing test during the preview window has three common causes:
- A release change. Behavior changed in the platform. Check the release notes and any Release Updates before you edit code.
- A test that was already fragile. It depends on existing data, record order, or a hard-coded ID.
- A deployment or setup problem. Missing metadata, permissions, or test data in the preview org.
Compare the failure against a run on a non-preview org with the same source. If the test passes there and fails on preview, the release is the likely cause.
Keeping the suite useful
- Keep a baseline. Record the pass count and run time from the last release, so a change stands out.
- Remove flaky tests or fix them. A suite that fails randomly trains the team to ignore failures.
- Add a test each time a release breaks something. Over several releases, the suite becomes a record of your real risks.
- Pin
sourceApiVersioninsfdx-project.jsonand raise it on purpose, after the tests pass on the new release.
Exercise
Your nightly job passes on your non-preview sandbox but fails two Apex tests on the preview scratch org. What is the most useful next step, and why?
Show answer
Read the release notes and Release Updates for areas touched by the failing tests. The pass on the non-preview org with the same source points to a platform change in the new release, not a defect in your setup. Confirm the cause, then fix the code or the test deliberately before the production upgrade.