Sub topic 3
Setting Test Levels with `sf project deploy`
Assume you know basic Apex, @IsTest, Salesforce metadata, and the difference between sandboxes and production. The sf project deploy command controls which Apex tests run during a deployment.
Set the test level
Use --test-level to choose one of four levels. Use --tests with RunSpecifiedTests to name test classes.
# Sandbox deployment without Apex tests
sf project deploy start --source-dir force-app \
--target-org my-sandbox --test-level NoTestRun
# Production deployment with selected tests
sf project deploy start --source-dir force-app \
--target-org my-prod --test-level RunSpecifiedTests \
--tests AccountServiceTest
# Production deployment with all tests outside managed packages
sf project deploy start --source-dir force-app \
--target-org my-prod --test-level RunLocalTests| Test level | Tests run | Typical use |
|---|---|---|
| `NoTestRun` | None | Sandbox development |
| `RunSpecifiedTests` | Named tests | Targeted CI validation |
| `RunLocalTests` | All tests in your org | Normal production deployment |
| `RunAllTestsInOrg` | Local and managed-package tests | Maximum test coverage |
Production deployments that contain Apex cannot use NoTestRun. With test execution, the deployed Apex must meet Salesforce coverage rules. The usual requirement is at least 75% coverage. RunSpecifiedTests also requires every deployed class and trigger to receive sufficient coverage from the selected tests, so a high org-wide percentage does not hide an uncovered changed class.
Test failures and quick deploys
A failed test fails the deployment. Salesforce does not apply the deployment's metadata, so fix the test or its data setup and run the deployment again.
A validation deployment runs the tests without saving the metadata. If it passes, use the returned job ID for a quick deploy. The quick deploy reuses the successful validation instead of running the tests again within Salesforce's allowed time window.
sf project deploy validate --source-dir force-app \
--target-org my-prod --test-level RunLocalTests
sf project deploy quick --job-id <validated-deploy-job-id> \
--target-org my-prodPipeline choices and troubleshooting
Use NoTestRun for fast sandbox feedback, RunSpecifiedTests for focused CI checks, and RunLocalTests for production releases. Use RunAllTestsInOrg only when managed-package tests are part of the release policy.
For failures, inspect the test result for the first assertion or exception. For coverage errors, verify that the selected tests execute the changed branches. For “test class not found,” check the class name, source format, and target org. For a quick-deploy error, validate again and use the new job ID.
Exercise
Which command runs only AccountServiceTest during deployment?
Show answer
sf project deploy start --test-level RunSpecifiedTests --tests AccountServiceTest