Sub topic 10
Choosing an Apex test level
This chapter assumes you can build an outbound change set, upload it, and open the inbound change set in the target org. It also assumes you know what an Apex test class is. If you have not yet validated a change set, read the previous chapter first. Here you learn what each test level runs, how it interacts with the code coverage rules, and how to pick one for a given change set.
Why test levels exist
Apex is code that runs inside a shared, multitenant platform. Salesforce wants evidence that new code works before it reaches production. When you deploy Apex classes or triggers, the platform runs Apex tests and measures how much of your code those tests execute. This is code coverage.
Declarative metadata such as fields, page layouts and most flows is not Apex. It does not need Apex tests to deploy. A test level tells the platform which tests to run during a validation or a deployment.
You choose the level in the inbound change set of the target org. The validate and deploy dialogs both offer it. The level you pick for the validation applies to that validation only, so choose it again when you deploy, unless you use quick deploy (described later).
The three levels in a change set
A change set offers three options.
| Test level | What runs | Typical use |
|---|---|---|
| Default | Depends on the target org and the change set contents (see below) | Most deployments |
| Run local tests | Every test in the org, except tests from installed managed packages | Production deployments where you want full local assurance |
| Run all tests | Every test in the org, including managed package tests | Rare. Used when you suspect your change interacts with a package |
How Default behaves
Default is the only level whose behavior depends on context.
- Target is a sandbox: no Apex tests run.
- Target is production and the change set contains Apex classes or triggers: all local tests run. Managed package tests are excluded.
- Target is production and the change set contains no Apex: no tests run.
So Default is not "run nothing". In production it can run your whole local test suite, because Salesforce cannot tell which existing tests depend on the code you are changing. A change set with one small class can trigger every local test in the org.
Run local tests
This level always runs your org's own tests, in a sandbox or in production. In a sandbox it is useful as a rehearsal. If you validate with Run local tests in the sandbox, you find out about failing tests before the production window.
Run all tests
This level adds tests that ship with managed packages. Those tests are written by the package vendor. They can fail for reasons you cannot fix, and they take extra time. Use this level only with a specific reason, such as checking that your change does not disturb a package you rely on.
Code coverage rules
Test level and code coverage are separate ideas, but they meet at deployment time. For production deployments that include Apex:
- The tests that run must pass. One failure fails the whole deployment.
- At least 75% of the Apex code in the org must be covered. This is an org-wide figure.
- Every trigger in the deployment must have some coverage, which means more than 0%.
A sandbox deployment does not enforce these rules, because Default runs no tests there. A change set that deploys cleanly to a sandbox can still fail in production on coverage. This is the most common reason to run a full test level in the sandbox first.
Here is a small class and a test class. The test class both checks behavior and provides the coverage.
public with sharing class InvoiceService {
public static Decimal applyDiscount(Decimal amount, Decimal percent) {
if (amount == null || percent == null) {
return amount;
}
return amount - (amount * percent / 100);
}
}@IsTest
private class InvoiceServiceTest {
@IsTest
static void appliesPercentageDiscount() {
Decimal result = InvoiceService.applyDiscount(200, 10);
System.assertEquals(180, result, 'A 10% discount on 200 should give 180');
}
@IsTest
static void returnsOriginalAmountWhenPercentIsNull() {
Decimal result = InvoiceService.applyDiscount(200, null);
System.assertEquals(200, result, 'A null percent should leave the amount unchanged');
}
}The second test covers the early-return branch. Without it, the if body would not run, and the class would have lower coverage.
Choosing a level: a decision flow
The diagram shows a reasonable order of questions. It is guidance, not a Salesforce rule.
A practical workflow
- Rehearse in a sandbox that is a full-featured match. If you have a partial or full sandbox that mirrors production, validate the change set there with Run local tests. Failures here cost you nothing.
- Validate in production with the level you will deploy with. Validation runs the same tests as deployment but saves nothing.
- Deploy with quick deploy when available. After a successful validation that ran tests, the inbound change set offers a quick deploy. It skips the test run and applies the already-validated change set. Salesforce limits quick deploy to a validation completed within the last 10 days, and the validation must have run tests and met the coverage rules.
- Run the deployment in a quiet window. Tests lock records and consume resources. A long test run during business hours can slow users and can cause failures in tests that depend on org data.
The idea behind this workflow is to move the expensive part, the test run, into a validation that you can do ahead of time. The deployment itself then takes minutes.
Edge cases and common errors
Tests that depend on org data
A test should create its own data. Tests annotated with @IsTest(SeeAllData=true) read real org records. They can pass in a sandbox with copied data and fail in production, or the reverse. Fix the test, not the deployment.
A test that passes alone and fails in a run
Local tests run together. A test that relies on a record count, a shared custom setting, or a particular execution order can fail only in a full run. If a failure message points at a test class that your change set does not include, the cause is an interaction between your new metadata and an existing test. A new required field is the classic case. The old test inserts the record without it, so the insert fails.
Coverage below 75%
The deployment reports coverage as a percentage and lists classes with low numbers. Add tests for the uncovered classes, deploy those tests in the change set (or earlier), and validate again. Remember that test classes must be in the target org to count. If you wrote tests only in the sandbox, include them in the change set.
Managed package test failures under Run all tests
If Run all tests fails in a package's own test class, the problem lies with the package, not your change. Compare against a validation with Run local tests. If that passes, the failing tests are not yours to fix, and Run local tests is the appropriate level.
Long test runs and timeouts
A large org can have thousands of tests. A full run can take hours, and the validation or deployment stays in progress that whole time. Plan for it, and do not start other deployments to the same org while it runs.
When a change set is the wrong tool for the test level
Change sets give you only the three levels above. You cannot choose individual test classes. Other tools can. For example, the Salesforce CLI can deploy with a named list of tests:
sf project deploy start --manifest manifest/package.xml --target-org MyProduction --test-level RunSpecifiedTests --tests InvoiceServiceTestWith specified tests, the coverage requirement applies to each class and trigger in the deployment, not just the org total. That is a stricter check on each component, so confirm your tests cover every deployed class before choosing this route. A later chapter covers when to move to tools like this.
Summary
- Default runs no tests in a sandbox. In production it runs all local tests if the change set has Apex, and none if it does not.
- Run local tests always runs your org's own tests. Run all tests adds managed package tests.
- Production deployments with Apex need passing tests, 75% org-wide coverage, and some coverage for each trigger.
- Rehearse with a full test level in a sandbox, validate in production, then use quick deploy within 10 days.
- When tests fail only in a full run, look for interactions with existing tests, such as a new required field.
Exercise
Your change set contains one Apex trigger and a custom field. It deploys to a sandbox with the Default level without errors. You then validate it in production with Default, and the validation fails on a test class that is not in your change set. What are the two most likely explanations, and how does the test level explain why the sandbox deployment did not show the problem?
Show answer
In production, Default runs all local tests because the change set contains Apex, so existing tests that were never touched are executed. The sandbox deployment with Default ran no tests, so nothing was checked. The likely causes are an existing test that breaks because of the new metadata (for example, it inserts records without a field that is now required, or the trigger changes behavior it depends on), or an existing test that depends on org data that differs between the sandbox and production. To catch this earlier, validate in the sandbox with Run local tests.
Exercise
A validation in production with Run local tests passes. Eight days later you want to deploy. What option avoids running the tests again, and what condition could make it unavailable?
Show answer
Quick deploy. It is unavailable if the validation is older than 10 days, if the validation did not run tests, or if it did not meet the coverage requirements. Here the validation is eight days old and ran tests, so quick deploy should be offered.