Sub topic 3
Choosing an Apex Test Level
Assume you know basic Salesforce CLI commands, metadata, Git, and Apex classes. The sf CLI uses a test level to decide which Apex tests run during deployment.
Choose the test level
Use the smallest level that meets the target org's deployment rules:
NoTestRunskips tests. Use it only where the org permits it, such as some sandbox deployments.RunSpecifiedTestsruns only tests named with--tests.RunLocalTestsruns all tests that are not from managed packages.RunAllTestsInOrgruns every test, including managed-package tests. It is usually slow.
sf project deploy start --manifest manifest/package.xml \
--target-org my-sandbox --test-level RunLocalTestsProduction deployments generally require tests and sufficient code coverage. A deployment can fail because a test fails, coverage is too low, or metadata dependencies are missing.
Validate, then quick-deploy
Validation checks a deployment without applying its changes. A successful validation creates a job that can be quick-deployed without running the tests again.
sf project deploy validate --manifest manifest/package.xml \
--target-org production --test-level RunLocalTests
sf project deploy quick --job-id 0AfXXXXXXXXXXXXXXX \
--target-org productionReplace the job ID with the ID printed by the validation command.
Conflicts, deletion, and status
Source tracking records changes between a Salesforce DX project and a scratch org or supported sandbox. Preview changes before deployment. If both local files and the org changed, retrieve or merge the org change deliberately instead of overwriting it.
Use destructive changes to remove metadata. Review the XML file carefully because deletion cannot be undone by deployment.
sf project deploy start --manifest manifest/package.xml \
--post-destructive-changes manifest/destructiveChangesPost.xml \
--target-org my-sandboxCheck a deployment that is still running, resume a stopped wait, or cancel it when appropriate:
sf project deploy report --use-most-recent
sf project deploy resume --use-most-recent
sf project deploy cancel --use-most-recentTroubleshooting and automation
For retrieve errors, verify the metadata type, member name, API version, permissions, and .forceignore. For deploy errors, read the component name, line number, test failure, and dependency message. Retrieve or preview again after resolving conflicts.
In CI, authenticate with a protected secret, set the target org explicitly, use a manifest, and fail the job when sf returns a nonzero status. Keep validation and quick deployment as separate controlled steps.
Exercise
Which test level runs local Apex tests but excludes managed-package tests?
Show answer
RunLocalTests.