Sub topic 10
Running Apex tests and reading results
This chapter assumes you have a Salesforce DX project, an authorized default org, and the Apex extensions working. If you have not deployed GreetingService yet, do that first, because tests run against code that exists in the org.
The test class
The tutorial's GreetingService has a matching test class. Save it as force-app/main/default/classes/GreetingServiceTest.cls.
@IsTest
private class GreetingServiceTest {
@IsTest
static void greetsByName() {
System.assertEquals('Hello, Ada!', GreetingService.greet('Ada'));
}
@IsTest
static void greetsWorldWhenBlank() {
System.assertEquals('Hello, world!', GreetingService.greet(''));
}
}A test class needs the @IsTest annotation. Each test method is static, takes no arguments, and is also marked @IsTest. A test passes if it finishes without an unhandled exception. A failed System.assertEquals throws one, so the test fails.
Deploy the class and its metadata file before you run it:
sf project deploy start --source-dir force-appRunning tests from the editor
The Apex extension adds Run Test and Debug Test links above each test class and each test method. Click Run Test above greetsByName to run one method. Click it above the class declaration to run every method in the class.
You can also use the Testing view in the activity bar. It lists the Apex test classes and methods the language server found, and it lets you run any node in the tree. The view fills in only after the language server finishes starting, so an empty tree usually means Java or the server is not ready.
From the Command Palette, these commands run broader sets:
- SFDX: Run Apex Tests lets you pick a class, a suite, or all tests.
- SFDX: Run Apex Test Suite runs a named group of classes.
Running tests from the CLI
The CLI is the same operation without the editor. It is also what a CI job would run.
sf apex run test --test-level RunLocalTests --result-format human --wait 10The options do the following:
--test-level RunLocalTestsruns all tests in your org except those from installed managed packages.--result-format humanprints a readable report. Other formats includejsonandjunit.--wait 10waits up to 10 minutes and prints results when the run finishes. Without it, the command returns immediately with a test run ID.
To run only one class, use --class-names GreetingServiceTest together with --test-level RunSpecifiedTests, or omit the level and let the CLI infer it.
Reading the results
Results appear in the Output panel under the Apex entry, and the CLI prints the same data to the terminal. A human-readable report has three parts.
- A summary with the outcome, the number of tests run, the pass rate, and the run time.
- A table of test results, one row per method, showing
PassorFail. - A failure message and stack trace for each failing method.
A failure looks like this:
GreetingServiceTest.greetsByName Fail
System.AssertException: Assertion Failed: Expected: Hello, Ada!, Actual: Hello, AdaThe message names the class and method, gives the expected and actual values, and points to a line number in the stack trace. Open that line, fix the code or the assertion, redeploy, and run again.
Common mistakes
- Missing deploy. Deploy first, then run. A test class that is not in the org will not be found.
- Empty Testing view. Check that the Apex language server is running and that Java is detected. The Troubleshooting chapter covers this.
- Wrong org. Tests run against the default org. Run
sf org listto confirm which org has the check mark. - Test timeouts in the CLI. If
--waitis too short, the command exits before the run ends. Increase it or fetch results later withsf apex get test --test-run-id <id>.
Code coverage is reported only if you ask for it. Add --code-coverage to the CLI command to include per-class percentages in the report.
Exercise
You changed the greet method locally, then clicked Run Test in the editor, and the test still shows the old behavior. What is the most likely cause, and what do you do?
Show answer
The change was not deployed, so the org still runs the old class. Deploy with sf project deploy start --source-dir force-app, then run the test again.
Exercise
Which CLI command runs all local tests, prints a readable report, and waits up to 10 minutes?
Show answer
sf apex run test --test-level RunLocalTests --result-format human --wait 10