Sub topic 14
Running Tests and Reading Coverage in the Org and the Salesforce CLI
This chapter assumes you can write a basic test class with @isTest and assertions, and that you have a sandbox, scratch org, or Developer Edition org. It also assumes the Salesforce CLI (sf) is installed and authorized to that org. Here you will run tests and read the coverage numbers they produce.
Running tests in the org
You have three common places to run tests.
- Developer Console: Open Test > New Run, pick classes and methods, and select Run. The Tests tab shows pass and fail results. The Overall Code Coverage panel lists coverage per class.
- Apex Test Execution page: In Setup, search for Apex Test Execution. Select Select Tests, choose classes, and run them. Results appear under View Test History.
- Apex Classes page: In Setup, open Apex Classes. The Percent Coverage column shows the stored result for each class. Use Estimate your organization's code coverage to compute the org-wide figure.
In the Developer Console, double-click a class in the coverage panel after a run. Covered lines are highlighted blue and uncovered lines red. The red lines are where your next test should go.
Running tests with the Salesforce CLI
The CLI suits scripts and CI jobs. This command runs every local test, meaning every test except those from installed managed packages, and collects coverage:
sf apex run test \
--target-org my-sandbox \
--test-level RunLocalTests \
--code-coverage \
--result-format human \
--wait 10The --wait 10 flag makes the command wait up to 10 minutes. Without it, the command returns a test run ID right away. You can fetch the results later with that ID:
sf apex get test --test-run-id 707xx0000000001 --code-coverage --target-org my-sandboxTo run a narrower set while developing, name the classes or methods:
sf apex run test --class-names DiscountCalculatorTest --code-coverage --synchronous
sf apex run test --tests DiscountCalculatorTest.appliesPercentageDiscount --synchronousAdd --detailed-coverage alongside --code-coverage to see which lines were covered. Use --output-dir test-results --result-format json when a pipeline needs to parse the output.
Choosing a test level
A test level controls which tests run. You choose it when you deploy.
| Test level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | No tests | Sandbox or development deploys only |
| `RunSpecifiedTests` | Tests you name | Targeted checks |
| `RunLocalTests` | All tests except managed-package tests | Default for production deploys that include Apex |
| `RunAllTestsInOrg` | Every test, including managed packages | Full regression runs |
A validation-only deploy lets you check these requirements before the real release:
sf project deploy start --source-dir force-app --test-level RunLocalTests --dry-run --target-org my-prodReading the results
A result has two parts you must check separately.
- Pass or fail per method. Any failure blocks a production deploy, even if coverage is above the threshold.
- Coverage per class and trigger. Look at the lowest percentages first, then open the uncovered lines.
flowchart TDReplace that idea with a simple habit: run, read failures, read the lowest coverage, add a test, run again.
Exercise
A CI job runs sf apex run test --test-level RunLocalTests --wait 10 and reports 82% coverage, but the deploy still fails. Name two things in the test results you should check.
Show answer
Check whether any test methods failed, because one failure blocks a production deploy regardless of the percentage. Also check the per-class coverage, because each trigger and class that you deploy needs at least some coverage, and the deploy can fail on a trigger with 0%.