Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Retrieve Unit Tests

The Test Discovery API returns details about Apex and automated flow tests. This resource is available in Tooling API version 65.0 and later.

Syntax

  • URI: /services/data/vXX.X/tooling/tests/
  • HTTPS Method: GET
  • Authentication: Authorization: Bearer token
  • Response Encoding: X-Chatter-Entity-Encoding: false

The X-Chatter-Entity-Encoding HTTP request header must be set to false so that the client requests raw (unencoded) output in the response. See Response Body Encoding.

Important

  • Format: JSON

Request Query Parameters

Parameter Type Description
category Enum of type String

Specifies the test category from which tests are retrieved. If this parameter isn’t specified, tests in all categories are retrieved. You can’t specify multiple categories in the same call. This parameter is available in API version 66.0 and later.

Existing categories are:

  • apex—Retrieves only Apex test classes.
  • flow—Retrieves only automated flow test classes.
testLevel Enum of type String Specifies which tests to retrieve based on test level. If omitted, defaults to RunAllTestsInOrg. Aligns with the testLevel request body parameter in the Test Runner API. Available in API version 68.0 and later and replaces showAllMethods.

Valid values:

  • RunAllTestsInOrg—all tests in the org, regardless of namespace. Includes tests from installed managed packages.
  • RunLocalTests—tests in the org’s namespace, plus flow tests. Excludes tests from installed managed packages.
showAllMethods Boolean

Deprecated. Available only in API version 67.0 and earlier. In API version 68.0 and later, use testLevel instead.

Specifies whether to retrieve all methods (true) or only visible methods (false) in a test class.

If this parameter value isn’t specified, the default value is false.

Results adhere to standard Apex visibility rules, which are influenced by factors such as namespace, managed package origin, access modifiers, and user permissions. For example, if a test class is private, then it isn’t retrieved unless showAllMethods is set to true.

namespacePrefix String

Specifies the namespace from which tests are retrieved. If this parameter isn’t specified, tests in all namespaces are retrieved.

All automated flow tests are in the FlowTesting namespace. In namespaced packages and orgs, the full namespace of an automated flow test is FlowTesting.namespacePrefix.

  • In API version 66.0 and later, to query for only flow tests in all namespaces, set the category query parameter to flow and don’t specify a namespacePrefix. To query for only flow tests in a specific namespaced package or org, set the category query parameter to flow and set namespacePrefix to the namespace.
  • In API version 65.0, to query for only flow tests, set the namespacePrefix to FlowTesting. To query for flow tests in a specific namespaced package or org, set namespacePrefix to FlowTesting.namespacePrefix.
nextRecord String A cursor that specifies the first test class to retrieve in the next page of results. This value is included in the nextRecordUrl property of the current page.
pageSize Integer Specifies the number of test classes to retrieve per page. If this parameter value isn’t specified, the default value is 1000 classes. The maximum value is 10000 classes.

Request Body

None.

Response Body Properties

Name Type Description
apexTestClasses Object[] An array of Apex test class objects. If there aren’t any tests in the result set, the value is [].
size Integer The total number of test classes in the result set across all pages. If the result set is empty, the value is 0.
nextRecordsUrl String The URL of the next page in the result set. If the current page is the last page in a result set, the nextRecordsUrl value is null.
testSetSignature String

An MD5 hash that represents the test classes and methods in the result set. If there aren’t any tests in the result set, the value is "".

When a result set spans multiple pages, the testSetSignature can change from one page to the next. A different testSetSignature indicates that the tests in the result set have changed since the retrieval of the previous page.

message String An informational message about the request, such as an input validation warning, or null if there is no applicable message.

Each Apex test class object in the apexTestClasses list includes these properties.

Name Type Description
id String

The Salesforce ID of the test class.

All Apex test classes have an ID.

Not all flow test classes have an ID. If the flow test class doesn’t have an ID, then the id value is "".

name String

The name of the test class.

For a flow test, the name is the API name of the flow.

namespacePrefix String

The namespace of the test class.

Apex tests are in the default namespace. If an Apex test is in a namespaced package or org, the namespacePrefix value is "NamepacePrefix". Otherwise, the namespacePrefix value is "".

All flow tests are in the FlowTesting namespace. If a flow test is in a namespaced package or org, the namespacePrefix value is "FlowTesting.NamepacePrefix". Otherwise, the namespacePrefix value is "FlowTesting".

testMethods Object[]

An array of test methods in the test class.

Each test method object includes the name property, which is set to the name of the test method. For example, {"name": "testSimpleAddition"}.

A flow test method name includes the API name of the flow followed by an underscore (_) and the API name of the flow test. For example, {"name":"FlowName_UpdateRecordFlowTest"}

Example

Example Request

1curl "https://MyDomain.my.salesforce.com/services/data/v68.0/tooling/tests?namespacePrefix=my_namespace&pageSize=2" \ 
2-H "Authorization: Bearer token" \ 
3       -H "X-Chatter-Entity-Encoding: false"

Example Response Body

1{
2  "apexTestClasses":[
3    {
4      "id":"01pxx0000004UVl",
5      "name":"BAAdditionTest",
6      "namespacePrefix":"my_namespace",
7      "testMethods":[
8        {"name":"testNegativeAddition"},
9        {"name":"testSimpleAddition"}
10      ]
11    },
12    {
13      "id":"01pxx0000004UXN",
14      "name":"BABitwiseTest",
15      "namespacePrefix":"my_namespace",
16      "testMethods":[
17        {"name":"testBitwiseAND"},
18        {"name":"testBitwiseOR"}
19      ]
20    }
21  ],
22  "message":null,
23  "nextRecordsUrl":"/services/data/v68.0/tooling/tests?namespacePrefix=my_namespace&pageSize=2&nextRecord=my_namespace.BAComparisonTest",
24  "size":10,
25  "testSetSignature":"91a678b54197669171f11eb824d6765a"
26}
27