Sub topic 15
Running tests in scratch orgs
This chapter assumes you can write a GitHub Actions workflow that installs the Salesforce CLI and authenticates with an SFDX auth URL. It also assumes you know Apex test levels. Here you add a job that tests your code in a fresh, disposable org.
Why use a scratch org
A check-only deployment to a sandbox tests your code against whatever data and metadata that sandbox holds. A scratch org is different. It is a short-lived org built from a definition file, so every run starts from the same known state. Tests that pass only because of leftover sandbox data fail here, which is the point.
Scratch orgs also let several pull requests run tests at the same time. Each run gets its own org, so runs do not block each other.
The pipeline flow
The scratch org is created from the Dev Hub, which is the org that has scratch org creation enabled. Your workflow must authenticate to the Dev Hub first. Store its auth URL as a secret, as you did for other orgs.
The commands
# Create a scratch org, deploy the project, run tests, and clean up
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias ci-scratch \
--duration-days 1 \
--set-default
sf project deploy start --source-dir force-app --target-org ci-scratch
sf apex run test \
--test-level RunLocalTests \
--result-format human \
--code-coverage \
--wait 20 \
--target-org ci-scratch
sf org delete scratch --target-org ci-scratch --no-prompt--duration-days 1 is the shortest lifetime, so an org that is never deleted expires on its own. Deleting the org at the end frees capacity right away, because Dev Hubs limit active scratch orgs and daily creations.
Making cleanup reliable
If the test step fails, the job stops and the delete command never runs. Put the delete in a separate step with if: always().
- name: Delete scratch org
if: always()
run: sf org delete scratch --target-org ci-scratch --no-promptCommon errors
- No default Dev Hub. Creation fails if the runner has no Dev Hub login. Log in with
--alias devhuband add--target-dev-hub devhubto the create command. - Missing features or settings. If your code needs a feature such as Communities, list it in the
featuresorsettingssection ofproject-scratch-def.json. Otherwise the deploy fails. - Limits reached. Many parallel runs can exhaust the daily creation limit. Run scratch tests on pull requests only, not on every push.
- Slow creation. Creating an org takes minutes. Pass
--waitwith a generous value.
Choosing between scratch and sandbox validation
Scratch org tests tell you whether the code works on its own. A check-only deploy to a sandbox tells you whether it deploys cleanly alongside the metadata already in that org. Many teams use both.
Exercise
A scratch org test job fails at the "Run Apex tests" step, and you later find dozens of old scratch orgs in the Dev Hub. What caused this, and how do you fix it?
Show answer
The job stopped at the failed test step, so the delete command never ran. Move the delete into its own step with if: always() so it runs whether or not the tests pass. Keep --duration-days 1 so any org that still leaks expires quickly.