Sub topic 8
RunAllTestsInOrg: including managed package tests
This chapter assumes you know the @IsTest annotation, the difference between a sandbox and production, and basic sf CLI usage. It also assumes you have seen the other three test levels. Here you will learn what RunAllTestsInOrg runs, how it differs from RunLocalTests, and why it is rarely the right choice for a release.
What RunAllTestsInOrg runs
RunAllTestsInOrg runs every Apex test in the target org during a deployment. That includes:
- Tests you wrote in the org.
- Tests that arrived with unlocked or unmanaged code.
- Tests that belong to installed managed packages, for example packages from AppExchange.
Managed package tests are the only difference from RunLocalTests. RunLocalTests skips tests that come from managed packages. RunAllTestsInOrg does not skip them.
The tests run in the org that receives the deployment, against the metadata as it would look after your changes. So the result depends on the org's current state, not only on the files in your project.
How it differs from RunLocalTests
| Aspect | RunLocalTests | RunAllTestsInOrg |
|---|---|---|
| Your own tests | Run | Run |
| Managed package tests | Skipped | Run |
| Runtime | Depends on your test suite | Same or longer, depending on installed packages |
| Who can fix a failure | You | You for your tests, only the vendor for package tests |
| Default for any org type | Default for production deployments that contain Apex | Never a default |
RunAllTestsInOrg is never applied automatically. You must request it with --test-level RunAllTestsInOrg.
Coverage rules at this level
Coverage works the way it does for RunLocalTests. For a production deployment that includes Apex, the org must reach 75% overall Apex code coverage. Every trigger in the deployment must also have some coverage, meaning more than 0%.
Two points about coverage are easy to get wrong:
- Code from managed packages does not count toward your org's coverage percentage. Running package tests adds no coverage to your own classes.
- Because the level runs everything, a class with no test of its own can still gain coverage when another test happens to call it. Do not rely on this. It makes coverage depend on unrelated tests that someone may change later.
The level does not make the 75% rule stricter. It widens the set of tests that must pass.
Running it from the CLI
Set the level with --test-level. No --tests flag is needed, because the level already selects every test.
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level RunAllTestsInOrgYou can also use it for a validation-only deployment. The validation runs the full set of tests and saves nothing to the org.
sf project deploy validate --source-dir force-app --target-org my-sandbox --test-level RunAllTestsInOrgIf you pass --tests together with RunAllTestsInOrg, expect the CLI to reject the combination or ignore the list. The --tests flag belongs to RunSpecifiedTests. Do not rely on it to narrow this level.
What happens when a test fails
A failing test fails the deployment, and Salesforce rolls the deployment back. This is true for every level that runs tests. The new twist with RunAllTestsInOrg is where the failure comes from.
Failures fall into two groups:
- A test in your own code fails. You can read the stack trace, fix the code or the test, and redeploy.
- A managed package test fails. The cause may be one of your changes, such as a validation rule, a required field, or a trigger that now interferes with the package's test data. It may also be a problem in the package that exists regardless of your change.
The second group is the main cost of this level. You cannot edit the package's code, so you cannot change its tests. Your options are to adjust your own change so it no longer interferes, to ask the vendor for a fix, or to deploy with a level that does not run package tests.
Cost and time
Every extra test costs time. An org with several large managed packages can add many minutes or hours to a deployment. Tests also compete for the org's resources, so long runs make parallel test jobs more likely to hit row locks or unrelated failures from tests that share records.
Run times matter most for two stages of a release:
- Validation before a release window, where a long run delays feedback.
- The release itself, where a long run extends the time the deployment is in progress.
A validation followed by a quick deploy avoids paying the production run twice. See the chapter on validation and quick deploy for the details. In short, you validate with tests, then deploy the validated change using its job ID within the allowed window, which is 10 days. The quick deploy skips re-running the tests.
sf project deploy validate --source-dir force-app --target-org my-prod --test-level RunAllTestsInOrg
# The command prints a job ID. Use it to deploy without re-running the tests:
sf project deploy quick --job-id <validated-deploy-job-id> --target-org my-prodCheck your org's current rules for which test levels qualify a validation for quick deploy. Do not assume every level does.
When to use it
RunAllTestsInOrg is a specialized tool. These are the situations where it makes sense:
- Checking compatibility after a major change. Before a platform release or a large refactor in a sandbox, you want to know whether your customizations break anything in installed packages.
- Auditing a sandbox. A scheduled run in a full or partial sandbox can reveal that a package and your metadata no longer work together.
- A policy you do not control. Some organizations require a full run before large releases.
These are the situations where RunLocalTests is the better choice:
- Routine production releases, because you can fix every failure that could stop the release.
- CI validation on each pull request, because the run is faster and the failures are yours to fix.
- Any pipeline stage where a failing vendor test would block the release for reasons unrelated to your change.
A common pattern is to run RunLocalTests in the main pipeline and RunAllTestsInOrg on a schedule in a sandbox. A failure in the scheduled job then becomes a ticket, not a blocked release.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Deployment fails on a test from a namespaced class you do not own | A managed package test is running against your new metadata | Check what your change touched, such as validation rules or required fields, and adjust it. Otherwise contact the package vendor. |
| The same deployment passes with `RunLocalTests` but fails here | The extra failing tests come from managed packages | Compare the failing test names. Those with a namespace prefix are package tests. |
| Deployment takes far longer than expected | Package tests add to the total run time | Use `RunLocalTests` for routine runs and keep this level for scheduled checks. |
| Flaky failures that pass on retry | Tests from different packages compete for shared records or locks | Rerun the job. If it keeps happening, report the test to its owner. |
| Coverage is below 75% although many tests passed | Package code does not count toward your coverage | Add or improve tests for your own classes. |
Practical checklist
- Decide what question you are asking. If it is "is my code safe to release?",
RunLocalTestsanswers it. If it is "does anything in this org break?", use this level. - Run it first in a sandbox that mirrors production's installed packages.
- Record which package tests already fail before your change. This tells you which failures are new.
- Treat a new package-test failure as a signal about your metadata, then confirm it by reading the stack trace.
- Keep the production release path on a level you can fix end to end.
Exercise
A production deployment passes with --test-level RunLocalTests in CI. When you rerun it with RunAllTestsInOrg, it fails on a test class that belongs to an installed managed package, and your own tests all pass. Does the 75% coverage rule explain the failure, and what are two reasonable next steps?
Show answer
No. Your own tests pass, and package code does not count toward your coverage, so the 75% rule is not the cause. The failure comes from the extra tests that RunAllTestsInOrg adds. Reasonable next steps are to inspect whether your change (for example a new validation rule, a required field, or a trigger) interferes with the package's test data and adjust it, or to contact the package vendor if the failure also occurs without your change. If neither is possible, keep RunLocalTests as the release gate and run RunAllTestsInOrg separately as a scheduled check in a sandbox.