Sub topic 10
Choosing an Apex test level
This chapter assumes you can build, upload, and validate an inbound change set, and that you know what Apex unit tests and code coverage are. Here you learn what each test level runs and how to pick one.
Why the test level matters
When you validate or deploy an inbound change set, you choose how many Apex tests Salesforce runs. A wider set of tests gives more confidence, but it takes longer. In production, a deployment that contains Apex must also meet the code coverage requirement: at least 75% of Apex lines covered by tests.
The test levels
| Test level | What runs | Typical use |
|---|---|---|
| Default | Depends on the target org and the change set contents | Most deployments |
| Run local tests | All tests in your org, except tests from installed managed packages | Production deployments with Apex |
| Run all tests | Every test in the org, including managed package tests | Rare, full regression checks |
| Run specified tests | Only the test classes you name | Large orgs, small Apex changes |
Default
The Default level behaves differently depending on the target:
- In a sandbox or Developer Edition org, no tests run.
- In production, if the change set contains Apex classes or triggers, all local tests run. If it contains no Apex, no tests run.
A change set that holds only fields and page layouts therefore deploys to production without tests under Default.
Run local tests and Run all tests
Run local tests runs every test your team wrote and skips tests that belong to installed managed packages. Those tests are not yours to fix, and they can fail for reasons you do not control. Run all tests includes them. Use it only when you have a specific reason to test the whole org.
Run specified tests
Here you enter the names of the test classes to run. This is faster in orgs with thousands of tests. The coverage rule is stricter, though. Each Apex class and trigger in the change set must reach 75% coverage on its own, using only the tests you named. Org-wide coverage does not help.
Choosing a level
Follow these guidelines:
- Validate first, using the same level you plan to deploy with. A validation that ran tests can later be deployed with a quick deploy, which skips the tests. A validation that ran no tests cannot be quick deployed.
- Pick Run local tests when you are unsure. It is the safest choice for production changes that include Apex.
- Pick Run specified tests only after checking that each deployed class is covered by the tests you name.
Common errors
- "Average test coverage across all Apex Classes and Triggers is 74%": The org is under the 75% threshold. Add tests or cover more lines before deploying.
- A test passes in the sandbox and fails in production: The test probably depends on data or settings that exist only in the sandbox. Tests should create their own data.
- A managed package test fails under Run all tests: Switch to Run local tests.
- Validation passed but quick deploy is unavailable: The validation ran no tests, or too much time has passed since it ran.
Exercise
A change set for production contains one Apex class, InvoiceService, and its test class, InvoiceServiceTest. The org has 4,000 tests and a full run takes hours. You choose Run specified tests and list only InvoiceServiceTest. What must be true for the validation to pass?
Show answer
InvoiceServiceTest must pass, and InvoiceService must reach at least 75% code coverage from the tests you named. Coverage from other tests in the org does not count at this level.