Skip to content

Blog

When does Salesforce run Apex tests on a deployment, and how do you run fewer?

Published November 28, 2026

Apex tests are the slowest part of most Salesforce deployments to production. This post explains when Salesforce runs them, which levels you can choose, the coverage rules that limit your options, and five practical ways to run fewer tests without breaking a deployment.

The test levels

A Metadata API deployment takes a test level. These are the ones the Salesforce CLI offers today:

Level What runs
NoTestRun No tests.
RunSpecifiedTests Only the test classes you name.
RunLocalTests Every test in your org except tests from installed managed packages.
RunAllTestsInOrg Every test, including managed-package tests.
RunRelevantTests Salesforce chooses tests based on the components in the package. A newer option that needs a recent API version (version 66 or later at the time of writing).

What runs if you don't choose

If you don't pick a level, Salesforce uses a default that depends on where you deploy:

  • Sandboxes and Developer Edition orgs: no tests.
  • Production: all local tests if the package contains Apex classes or triggers, and none if it doesn't. Gearset's documentation of this default also counts Flows that are set to activate on deployment, so check your own package for those (their page).

That second rule is the source of most surprise slow deployments. The test run isn't triggered by what you changed; it is triggered by what is in the package.

The coverage rules

Two rules decide whether a production deployment can pass:

  • With RunLocalTests or RunAllTestsInOrg, the org needs at least 75% Apex code coverage overall, and every test that runs must pass.
  • With RunSpecifiedTests, each Apex class and trigger in the package needs at least 75% coverage on its own, from the tests you named.

The second rule is why "just run the tests for my class" sometimes fails in production when the full run would have passed: a class in the package with no test among your picks has zero coverage in that run.

Five ways to run fewer tests

  1. Keep unchanged Apex out of the package. Change sets, "select all" and dependency add-ons often pull in classes that haven't changed. In production, any Apex in the package means a full local test run. Compare your source against the target and deploy only the differences.
  2. Specify the tests, and check coverage per class. RunSpecifiedTests can cut a run from hundreds of classes to a handful, as long as every class in the package is covered by one of them.
  3. Try RunRelevantTests. Where your API version allows it, let Salesforce choose. Read Salesforce's current documentation for its limits before relying on it for production.
  4. Validate ahead, quick deploy later. Run a validation with tests while you wait for the release window. A validation that ran tests and passed can be promoted with a quick deploy, which deploys the validated package without running the tests again. Salesforce allows this within 10 days of the validation.
  5. Ship configuration separately from code. A package with no Apex needs no tests at the default level, so layouts, fields and permission changes don't have to wait behind a code release. Check any Flows in the package first.

What we checked

On 28 November 2026 we ran check-only deployments, which change nothing in the org, against a Developer Edition org (Salesforce treats these as production) with an explicit NoTestRun. A custom label validated, and so did an active Flow. One org and one date is a small sample, and Salesforce changes behaviour between releases, so run the same check against your own org before depending on it.

How Diffbridge automates this

Diffbridge's Smart test level applies the same ideas for you on every deployment, and shows the plan, with reasons, before you start:

  • Apex that is byte-identical to the target is dropped from the package, so it can't force a test run.
  • If nothing in the package adds, changes or deletes Apex, the deployment runs with NoTestRun, even into production.
  • If Apex changed, it runs the related tests: test classes in the package, tests matching naming conventions such as FooTest, tests that reference the changed class, and for triggers, tests that use the trigger's object.
  • In production, if a changed class has no related test, it falls back to all local tests instead of risking a coverage failure.

Smart picks related tests by name and by reference, so a test that exercises code only indirectly can be missed. The production fallback covers the case where a changed class has no related test at all. You can always override the plan and hand-pick tests. The manual page on Smart tests has the details.