Sub topic 10
Automating release regression tests
This chapter covers how to turn the manual regression checks from the previous chapter into a repeatable pipeline. It assumes you can use Setup, know the difference between sandboxes and scratch orgs, have run a few Salesforce CLI commands, and have a Git repository with Salesforce source in force-app. Familiarity with a CI service such as GitHub Actions, GitLab CI, or Jenkins helps, but the ideas apply to any of them.
Why automate release testing
Salesforce ships three releases a year. Each one has a preview window in which you can test before production is upgraded. That window is short, usually a few weeks, and the same checks must run every time. Manual testing in that window has three problems:
- People skip steps when they are busy.
- Results are hard to compare between releases.
- Retesting after a fix takes as long as the first run.
A CI job fixes these. It runs the same checks against the same kind of environment on every trigger, and it records the results.
Automation does not replace all manual testing. It covers what is scriptable: Apex tests, deployments, and browser-driven user journeys. Exploratory checks on new features, layout reviews, and sign-off from business owners stay manual.
What to automate
Three layers give the best return, listed here from cheapest to most expensive.
| Layer | What it catches | Typical tool | Run time |
|---|---|---|---|
| Deployment validation | Metadata that no longer compiles or deploys on the new release | `sf project deploy validate` | Minutes |
| Apex tests | Behavior changes in Apex, triggers, and Flow-invoked code | `sf apex run test` | Minutes to hours |
| UI journeys | Broken pages, Lightning component changes, login and navigation problems | Browser test tools such as Selenium or UTAM | Slow |
Start with the first two. They need no extra tooling beyond the Salesforce CLI. Add UI journeys only for the few paths the business cannot lose, such as lead capture or order entry.
Choosing the target environment
The pipeline needs an org that is already on the preview release. You have two options.
- A preview sandbox. It holds your real configuration and a realistic copy of data. It is the best signal for how production will behave. It is long-lived, so concurrent runs can interfere with each other.
- A preview scratch org. It is created fresh for each run from source and is disposable. It runs only what is in your repository, so it misses configuration that exists only in production.
Many teams use both. The scratch org gives a fast, isolated signal on every commit during the preview window. The sandbox gives a final check close to production.
Creating a preview scratch org
A scratch org definition file with "release": "Preview" creates the org on the upcoming release.
{
"orgName": "Release Preview Org",
"edition": "Developer",
"release": "Preview"
}The release option only has an effect during the preview window. Outside it, Preview and Previous are not distinct from the current release, so a pipeline that depends on this value should be checked after each release cycle begins. Also keep in mind that scratch orgs are limited per Dev Hub per day, so a job that creates one on every push can run out of allocation. Run it on pull requests to a release branch, or on a schedule.
Create the org from the definition file:
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias preview-org \
--duration-days 7 \
--set-defaultA short duration keeps the org from using allocation after the run is done. In CI you usually delete the org at the end of the job, so the duration is a safety net for jobs that crash.
Running Apex tests
Run all local tests against the preview environment and wait for the results.
sf apex run test \
--target-org my-preview-sandbox \
--test-level RunLocalTests \
--result-format human \
--wait 20RunLocalTests runs every test in your org except those from installed managed packages. That is the right default for a baseline, because you are checking your own code. The --wait 20 option makes the command block for up to 20 minutes. If the tests take longer, the command returns before they finish and the job may look like it passed or failed without a result. Set the wait above your measured run time, and make the job fail if the command exits with a non-zero status.
For a pipeline you will usually want machine-readable output as well. Add --result-format junit or --result-format json and --output-dir test-results, and have the CI service publish the files. A JUnit report shows up in most CI dashboards as a list of failed tests.
A reference pipeline
The flow below runs on a schedule during the preview window. It first creates the scratch org, then deploys, then runs tests, and always cleans up.
Here is a GitHub Actions workflow that implements it. It assumes an authenticated Dev Hub whose auth URL is stored in the repository secret DEVHUB_SFDX_URL. Store the URL as a secret and never write it into the workflow file.
name: release-preview-regression
on:
schedule:
- cron: "0 5 * * 1-5"
workflow_dispatch:
jobs:
regression:
runs-on: ubuntu-latest
timeout-minutes: 90
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to Dev Hub
run: |
echo "${{ secrets.DEVHUB_SFDX_URL }}" > devhub.url
sf org login sfdx-url --sfdx-url-file devhub.url --alias devhub --set-default-dev-hub
rm devhub.url
- name: Create preview scratch org
run: |
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias preview-org \
--duration-days 1 \
--set-default
- name: Deploy source
run: sf project deploy start --target-org preview-org
- name: Run Apex tests
run: |
sf apex run test \
--target-org preview-org \
--test-level RunLocalTests \
--result-format junit \
--output-dir test-results \
--wait 60
- name: Upload results
if: always()
uses: actions/upload-artifact@v4
with:
name: apex-test-results
path: test-results
- name: Delete scratch org
if: always()
run: sf org delete scratch --target-org preview-org --no-promptThree details matter here:
if: always()on the upload and delete steps makes them run even when a test step fails. Without it, a failed run leaves an org behind and loses the results you need.- The workflow has a
timeout-minuteslimit, so a hung command does not hold the runner for hours. - The workflow does not run on every push. It runs on a schedule and on demand, which protects your scratch org allocation.
Keeping the API version under control
The sourceApiVersion field in sfdx-project.json sets the API version that source deployments use.
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"sourceApiVersion": "65.0"
}During release testing, leave it at its current value first. That shows whether existing metadata still works on the new platform. After the tests pass, raise the version deliberately in a separate commit. If you raise it at the same time as testing, a failure could come from the new release or from the new API version, and you cannot tell which.
A useful pattern is to run the pipeline twice: once with the current version and once with a branch that raises it. Compare the two reports.
Making the results trustworthy
A regression suite is only useful if a failure means something. Common problems:
- Tests that depend on org data. An Apex test that uses
SeeAllData=trueor assumes a specific record exists can pass in one org and fail in another. Create test data inside the test. - Flaky tests. A test that fails at random teaches the team to ignore failures. During the preview window, rerun each failed test once. If it passes on the rerun, log it as flaky and fix it later. If it fails twice, treat it as a real regression.
- Missing baseline. A failure means little without a known-good result. Run the same suite against a non-preview sandbox on the current release. A test that fails in both is an existing problem. A test that fails only on preview is a release regression.
- Code coverage mixed with regression. Passing tests do not prove behavior is correct. Coverage rules are enforced at production deployment and are separate from whether your tests are meaningful.
Handling failures
When a test fails on preview but passes on the current release, work in this order:
- Read the failure message and stack trace to find the failing class and line.
- Check the release notes and the Release Updates page for a change that matches.
- Reproduce the failure in a clean scratch org if you can. A reproduction removes doubt about org-specific causes.
- Fix the code if the cause is yours, or open a Salesforce Support case with the reproduction if the platform changed in a way that breaks documented behavior.
- Rerun the suite and record the result in the readiness checklist.
Time matters because the preview window closes. If Support needs to be involved, open the case early. A fix may need to be made before the production upgrade date.
Summary of practices
- Test on both a scratch org and a preview sandbox where you can.
- Run on a schedule, not on every commit, to protect allocation.
- Always publish results and delete orgs, even when a step fails.
- Compare against a current-release baseline to separate release regressions from existing failures.
- Change the API version in its own step.
Exercise
Your nightly preview pipeline reports 12 failing Apex tests. You have not run the suite against a current-release sandbox. Why is that a problem, and what would you do next?
Show answer
Without a baseline, you cannot tell which failures come from the new release and which already existed. Run the same suite against a non-preview sandbox on the current release. Failures that appear in both are existing defects. Failures that appear only on preview are release regressions, so check those against the release notes and Release Updates first.
Exercise
A teammate sets --wait 5 on sf apex run test in CI, but the suite takes about 25 minutes. What can go wrong, and how do you fix it?
Show answer
The command stops waiting after 5 minutes, so the job can finish before the tests do and report no useful result. Set --wait above the measured run time (for example 40), write JUnit output with --output-dir, and make sure the job fails on a non-zero exit code.
Exercise
Why should the "Delete scratch org" step use if: always()?
Show answer
Without it, the step is skipped when an earlier step fails. The scratch org would stay active until its duration ends and use up your daily and active scratch org allocation, which can block later runs.