Newer Version Available

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

Testing

When you’re ready to test changes to your Salesforce app source code, you can run Apex tests from the Salesforce DX CLI. Apex tests are run in your scratch org.

You can also execute the CLI command for running Apex tests (force:apex:test:run) from within third-party continuous integration tools, such as Jenkins.

To run Apex tests from the command line:

1sfdx force:apex:test:run

This command runs all Apex tests in the scratch org asynchronously and then outputs a job ID. Pass the ID to 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.

1sfdx force:apex:test:report --testrunid 7074C00000988ax

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

1sfdx force:apex:test:run --synchronous --classnames TestA

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.

1sfdx force:apex:test:run --classnames TestA,TestB --resultformat tap --codecoverage

Use the --tests parameter to run specific test methods using the standard notation Class.method. If you are 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.

1sfdx force:apex:test:run --tests TestA.excitingMethod,TestA.boringMethod,TestB

Here’s the same example but with a namespace.

1sfdx force:apex:test:run --tests ns.TestA.excitingMethod,ns.TestA.boringMethod,ns.TestB

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