---
schema_version: "1"
id: 8e600b8b0705f0d3979d3f85c657df37322682aaa7ebe0f1680ce317322bb4f7
title: apex run test
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/salesforcedx/sfclireference/guides/apex/cli_reference_apex_run_test.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-cli-reference/guide/cli_reference_apex_run_test.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: d55dd0e846b675352a9a7dcfc37933cbbcb3042ed135fcf49a4eac4926bf63d5
products:
  - Salesforce CLI Command Reference
  - salesforcedx
---
<!-- prettier-ignore-start -->

# apex run test

Invoke Apex tests in an org.

## Description for apex run test

Specify which tests to run by using the --class-names, --suite-names, or --tests flags. Alternatively, use the --test-level flag to run all the tests in your org, local tests, or specified tests.

To see code coverage results, use the --code-coverage flag with --result-format. The output displays a high-level summary of the test run and the code coverage values for classes in your org. If you specify human-readable result format, use the --detailed-coverage flag to see detailed coverage results for each test method run.

By default, Apex tests run asynchronously and immediately return a test run ID. You can use the --wait flag to specify the number of minutes to wait; if the tests finish in that timeframe, the command displays the results. If the tests haven't finished by the end of the wait time, the command displays a test run ID. Use the "sf apex get test --test-run-id" command to get the results.

To run both Apex and Flow tests together, run the "sf logic run test" CLI command, which has similar flags as this command, but expands the --tests flag to also include Flow tests.

You must have the "View All Data" system permission to use this command. The permission is disabled by default and can be enabled only by a system administrator.

NOTE: The testRunCoverage value (JSON and JUnit result formats) is a percentage of the covered lines and total lines from all the Apex classes evaluated by the tests in this run.

## Aliases for apex run test

`force:apex:test:run`

## Flags

| Flag Name (Long) | Flag Name (Short) | Description |
|------------------|-------------------|-------------|
| <code>&#8209;&#8209;api&#8209;version</code> | N/A | **Type:** Value<br><br>Override the api version used for api requests made by this command |
| <code>&#8209;&#8209;class&#8209;names</code> | <code>&#8209;n</code> | **Type:** Value<br><br>Apex test class names to run; default is all classes.<br><br>If you select --class-names, you can't specify --suite-names or --tests.<br><br>For multiple classes, repeat the flag for each.<br><br>--class-names Class1 --class-names Class2 |
| <code>&#8209;&#8209;code&#8209;coverage</code> | <code>&#8209;c</code> | **Type:** Boolean<br><br>Retrieve code coverage results. |
| <code>&#8209;&#8209;concise</code> | N/A | **Type:** Boolean<br><br>Display only failed test results; works with human-readable output only. |
| <code>&#8209;&#8209;detailed&#8209;coverage</code> | <code>&#8209;v</code> | **Type:** Boolean<br><br>Display detailed code coverage per test. |
| <code>&#8209;&#8209;flags&#8209;dir</code> | N/A | **Type:** Value<br><br>Import flag values from a directory. |
| <code>&#8209;&#8209;json</code> | N/A | **Type:** Boolean<br><br>Format output as json. |
| <code>&#8209;&#8209;output&#8209;dir</code> | <code>&#8209;d</code> | **Type:** Value<br><br>Directory in which to store test run files. |
| <code>&#8209;&#8209;poll&#8209;interval</code> | <code>&#8209;i</code> | **Type:** Value<br><br>Number of seconds to wait between retries. |
| <code>&#8209;&#8209;result&#8209;format</code> | <code>&#8209;r</code> | **Type:** Value<br>**Valid Values:** `human`, `tap`, `junit`, `json`<br>**Default value:** `human`<br><br>Format of the test results. |
| <code>&#8209;&#8209;suite&#8209;names</code> | <code>&#8209;s</code> | **Type:** Value<br><br>Apex test suite names to run.<br><br>If you select --suite-names, you can't specify --class-names or --tests.<br><br>For multiple suites, repeat the flag for each.<br><br>--suite-names Suite1 --suite-names Suite2 |
| <code>&#8209;&#8209;synchronous</code> | <code>&#8209;y</code> | **Type:** Boolean<br><br>Runs test methods from a single Apex class synchronously; if not specified, tests are run asynchronously. |
| <code>&#8209;&#8209;target&#8209;org</code> | <code>&#8209;o</code> | **Type:** Value<br>**Required**<br><br>Username or alias of the target org. Not required if the `target-org` configuration variable is already set. |
| <code>&#8209;&#8209;test&#8209;level</code> | <code>&#8209;l</code> | **Type:** Value<br>**Valid Values:** `RunLocalTests`, `RunAllTestsInOrg`, `RunSpecifiedTests`<br><br>Level of tests to run; default is RunLocalTests.<br><br>Here's what the levels mean:<br><br><ul><li>RunSpecifiedTests — Only the tests that you specify in the runTests option are run. Code coverage requirements differ from the default coverage requirements when using this test level. The executed tests must cover each class and trigger in the deployment package for a minimum of 75% code coverage. This coverage is computed for each class and triggers individually, and is different than the overall coverage percentage.</li><li>RunLocalTests — All local tests in your org, including tests that originate from no-namespaced unlocked packages, are run. The tests that originate from installed managed packages and namespaced unlocked packages aren't run. This test level is the default for production deployments that include Apex classes or triggers.</li><li>RunAllTestsInOrg — All tests are run. The tests include all tests in your org.</li></ul> |
| <code>&#8209;&#8209;tests</code> | <code>&#8209;t</code> | **Type:** Value<br><br>Apex test class names or IDs and, if applicable, test methods to run; default is all tests.<br><br>If you specify --tests, you can't specify --class-names or --suite-names<br><br>For multiple tests, repeat the flag for each.<br><br>--tests Test1 --tests Test2 |
| <code>&#8209;&#8209;wait</code> | <code>&#8209;w</code> | **Type:** Value<br><br>Sets the streaming client socket timeout in minutes; specify a longer wait time if timeouts occur frequently. |

## Examples for apex run test

Run all Apex tests and suites in your default org:

```shell
sf apex run test
```

Run the specified Apex test classes in your default org and display results in human-readable form:

```shell
sf apex run test --class-names MyClassTest --class-names MyOtherClassTest --result-format human
```

Run the specified Apex test suites in your default org and include code coverage results and additional details:

```shell
sf apex run test --suite-names MySuite --suite-names MyOtherSuite --code-coverage --detailed-coverage
```

Run the specified Apex tests in your default org and display results in human-readable output:

```shell
sf apex run test --tests MyClassTest.testCoolFeature --tests MyClassTest.testAwesomeFeature --tests AnotherClassTest --tests namespace.TheirClassTest.testThis --result-format human
```

Run all tests in the org with the specified username with the specified test level; save the output to the specified directory:

```shell
sf apex run test --test-level RunLocalTests --output-dir <path to outputdir> --target-org me@my.org
```

Run all tests in the org asynchronously:

```shell
sf apex run test --target-org myscratch
```

Run all tests synchronously; the command waits to display the test results until all tests finish:

```shell
sf apex run test --synchronous
```

Run specific tests using the --test-level flag:

```shell
sf apex run test --test-level RunLocalTests
```

Run Apex tests on all the methods in the specified class; output results in Test Anything Protocol (TAP) format and request code coverage results:

```shell
sf apex run test --class-names TestA --class-names TestB --result-format tap --code-coverage
```

Run Apex tests on methods specified using the standard Class.method notation; if you specify a test class without a method, the command runs all methods in the class:

```shell
sf apex run test --tests TestA.excitingMethod --tests TestA.boringMethod --tests TestB
```

Run Apex tests on methods specified using the standard Class.method notation with a namespace:

```shell
sf apex run test --tests ns.TestA.excitingMethod --tests ns.TestA.boringMethod --tests ns.TestB
```
<!-- prettier-ignore-end -->
