Newer Version Available

This content describes an older version of this product. View Latest

Run Apex Tests

When you’re ready to test changes to your source code, you can run Apex tests in an org using Salesforce CLI on the command line, Salesforce Extensions for VS Code, or from within third-party continuous integration tools, such as Jenkins or CircleCI.
See the Salesforce CLI Command Reference for the full list of command options.

Minimum User Permissions and Settings Required

The user running Apex tests must have these user permissions in the org:

  • View Setup and Configuration
  • API Enabled

Also ensure that the Enable Streaming API setting is enabled in the org’s user interface. The setting is enabled by default.

See User Permissions and Configure User Interface Settings for details.

Run All Apex Tests

This command runs all Apex tests in the scratch org asynchronously. Rather than display the actual test results, the command outputs the force:apex:test:report command that you then run to view the full results.

Run Specific Apex Tests

Use --testlevel to run a subset of tests. See Understanding Deploy in the Apex Developer Guide for the list of possible test level values.

Use the --synchronous parameter to run tests synchronously. The command waits to display the test results until all tests have completed.

Use parameters to list the test classes or suites to run, specify the output format, view code coverage results, and more. For example, the following command runs the TestA and TestB test classes, provides results in Test Anything Protocol (TAP) format, and requests code coverage results.

Use the --tests parameter to run specific test methods using the standard notation Class.method. If you’re testing a managed package, use namespace.Class.method. If you specify a test class without a method, the command runs all methods in the class. This example shows how to run two methods in the TestA class and all methods in the TestB class.

Here’s the same example but with a namespace.

You can specify either --tests or --classnames with force:apex:test:run but not both.

View Test Results

Run the force:apex:test:report command to view the results. The results include the outcome of individual tests, how long each test ran, and the overall pass and fail rate.