apex run test
Invoke Apex tests in an org.
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.
force:apex:test:run
| Flag Name (Long) | Flag Name (Short) | Description |
|---|---|---|
‑‑api‑version | N/A | Type: Value Override the api version used for api requests made by this command |
‑‑class‑names | ‑n | Type: Value Apex test class names to run; default is all classes. If you select –class-names, you can’t specify –suite-names or –tests. For multiple classes, repeat the flag for each. –class-names Class1 –class-names Class2 |
‑‑code‑coverage | ‑c | Type: Boolean Retrieve code coverage results. |
‑‑concise | N/A | Type: Boolean Display only failed test results; works with human-readable output only. |
‑‑detailed‑coverage | ‑v | Type: Boolean Display detailed code coverage per test. |
‑‑flags‑dir | N/A | Type: Value Import flag values from a directory. |
‑‑json | N/A | Type: Boolean Format output as json. |
‑‑output‑dir | ‑d | Type: Value Directory in which to store test run files. |
‑‑poll‑interval | ‑i | Type: Value Number of seconds to wait between retries. |
‑‑result‑format | ‑r | Type: Value Valid Values: human, tap, junit, jsonDefault value: humanFormat of the test results. |
‑‑suite‑names | ‑s | Type: Value Apex test suite names to run. If you select –suite-names, you can’t specify –class-names or –tests. For multiple suites, repeat the flag for each. –suite-names Suite1 –suite-names Suite2 |
‑‑synchronous | ‑y | Type: Boolean Runs test methods from a single Apex class synchronously; if not specified, tests are run asynchronously. |
‑‑target‑org | ‑o | Type: Value Required Username or alias of the target org. Not required if the target-org configuration variable is already set. |
‑‑test‑level | ‑l | Type: Value Valid Values: RunLocalTests, RunAllTestsInOrg, RunSpecifiedTestsLevel of tests to run; default is RunLocalTests. Here’s what the levels mean:
|
‑‑tests | ‑t | Type: Value Apex test class names or IDs and, if applicable, test methods to run; default is all tests. If you specify –tests, you can’t specify –class-names or –suite-names For multiple tests, repeat the flag for each. –tests Test1 –tests Test2 |
‑‑wait | ‑w | Type: Value Sets the streaming client socket timeout in minutes; specify a longer wait time if timeouts occur frequently. |
Run all Apex tests and suites in your default org:
Run the specified Apex test classes in your default org and display results in human-readable form:
Run the specified Apex test suites in your default org and include code coverage results and additional details:
Run the specified Apex tests in your default org and display results in human-readable output:
Run all tests in the org with the specified username with the specified test level; save the output to the specified directory:
Run all tests in the org asynchronously:
Run all tests synchronously; the command waits to display the test results until all tests finish:
Run specific tests using the –test-level flag:
Run Apex tests on all the methods in the specified class; output results in Test Anything Protocol (TAP) format and request code coverage results:
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:
Run Apex tests on methods specified using the standard Class.method notation with a namespace: