Sub topic 11
Run Apex Code and Tests
This chapter assumes you have installed the CLI, authorized an org, and set a default target org. It also assumes you know basic Apex: classes, methods, and test classes. Both commands below run against the default target org unless you pass --target-org.
Two ways to run Apex
The CLI gives you two Apex entry points:
sf apex runexecutes anonymous Apex. Anonymous Apex is code that runs once and is not saved in the org. It suits quick experiments and one-off scripts.sf apex run testruns saved test classes in the org and reports the results.
Run anonymous Apex from a file
Create a file named hello.apex with one line:
System.debug('Hello from the Salesforce CLI');Run it, then run the org's local tests:
sf apex run --file hello.apex
sf apex run test --test-level RunLocalTests --result-format human --wait 10The first command compiles and executes the file in the org. It prints the compile status, the execution status, and the debug log. Look for a USER_DEBUG line that contains your message.
If you omit --file, the command prompts you to type code and finish with Ctrl+D. That is handy for a single line, but a file is easier to repeat and keep in version control.
Run tests
sf apex run test starts a test run in the org. These flags matter most:
| Flag | Purpose |
|---|---|
| `--test-level` | Which tests to run: `RunLocalTests`, `RunAllTestsInOrg`, or `RunSpecifiedTests` |
| `--class-names` | One or more test classes to run |
| `--tests` | Specific test methods, written as `ClassName.methodName` |
| `--result-format` | Output style, such as `human`, `tap`, `junit`, or `json` |
| `--wait` | Minutes to wait for results before the command returns |
| `--code-coverage` | Include code coverage in the results |
RunLocalTests runs every test in your org except tests that come from installed managed packages. This is usually what you want.
To run one class and see coverage:
sf apex run test --class-names AccountServiceTest --code-coverage --result-format human --wait 10What happens without `--wait`
Without --wait, the command starts the run and returns right away with a test run ID. The tests keep running in the org. Fetch the results later with the ID:
sf apex get test --test-run-id 707xx0000000001The ID above is an example. Use the one the first command printed.
Common errors
- Nothing in the output but a compile error. Anonymous Apex that fails to compile reports a line and column. Fix the code and rerun. Nothing was executed.
- No default org. If you see an error about a missing target org, set one with
sf config set target-org myorg, or add--target-org myorgto the command. - The command returns before tests finish. You did not pass
--wait, or the wait time was too short. Pass a larger number or usesf apex get test. - Tests pass in a scratch org and fail in a sandbox, or the reverse. Tests that depend on existing data behave differently between orgs. Create the records the test needs inside the test.
- A class name is not found.
--class-namesneeds the name of a class already deployed to the org. Deploy it first.
Check your understanding
Exercise
You run sf apex run test --test-level RunLocalTests with no --wait flag. The command returns immediately. How do you get the results?
Show answer
The command prints a test run ID. Run sf apex get test --test-run-id <id> with that ID to fetch the results. You can also rerun the original command with --wait set to a number of minutes.
Exercise
Which command runs only the method testInsert in the class AccountServiceTest, and which flag shows coverage?
Show answer
sf apex run test --tests AccountServiceTest.testInsert --code-coverage --wait 10. The --tests flag selects the method, and --code-coverage adds coverage to the results.