Sub topic 2
RunLocalTests and Apex Deployment Test Levels
Assume you know basic Apex, @IsTest, Salesforce metadata, and the sf command. A deployment test level controls which Apex tests Salesforce runs before accepting a deployment.
RunLocalTests: your org's own tests
RunLocalTests runs every test class in the target org except tests that belong to installed managed packages. It is the usual production choice because it checks your code without spending time testing package code.
sf project deploy start \
--source-dir force-app \
--target-org my-prod \
--test-level RunLocalTestsSalesforce does not deploy the changes if a test fails or if the deployment does not satisfy coverage rules.
RunAllTestsInOrg: including managed package tests
RunAllTestsInOrg runs every test in the org, including tests from installed managed packages. It provides the broadest test run, but it can take longer and may include code your team cannot change.
| Test level | Tests run |
|---|---|
| RunLocalTests | All tests except managed package tests |
| RunAllTestsInOrg | All tests, including managed package tests |
flowchart TD
A[Choose a test level] --> B[RunLocalTests]
A --> C[RunAllTestsInOrg]
B --> D[Your org tests]
C --> D
C --> E[Managed package tests]Which level applies when you set none
The default depends on the target org:
- For a sandbox, Salesforce normally uses
NoTestRun. - For a production deployment that contains Apex, Salesforce normally uses
RunLocalTests.
Do not rely on defaults in an automated pipeline. Set --test-level explicitly so the command documents its intent and behaves consistently.
Coverage requirements for each level
Apex code coverage measures how much executable Apex code tests run. Production deployments generally require at least 75% overall coverage.
RunLocalTestsandRunAllTestsInOrgmust meet the org-wide 75% requirement. Managed package code is excluded from this calculation.RunSpecifiedTestsmust provide at least 75% coverage for every deployed class and trigger from the named tests.NoTestRundoes not run tests or calculate deployment coverage, and it is not allowed for production deployments containing Apex.
A passing test run can still produce a failed deployment when coverage is too low.
Exercise
Which level runs your org's tests while excluding installed managed package tests?
Show answer
RunLocalTests.