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

Important

Important

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.

Format: JSON

Request Query Parameters

category

  • Type: Enum of type String
  • Description: 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

  • Type: Enum of type String
  • Description: 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

  • Type: Boolean
  • Description: 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

  • Type: String
  • Description: 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

  • Type: String
  • Description: 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

  • Type: Integer
  • Description: 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

apexTestClasses

  • Type: Object[]
  • Description: An array of Apex test class objects. If there aren’t any tests in the result set, the value is [].

size

  • Type: Integer
  • Description: The total number of test classes in the result set across all pages. If the result set is empty, the value is 0.

nextRecordsUrl

  • Type: String
  • Description: 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

  • Type: String
  • Description: 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

  • Type: String
  • Description: 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.

id

  • Type: String
  • Description: 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

  • Type: String
  • Description: The name of the test class.

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

namespacePrefix

  • Type: String
  • Description: 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

  • Type: Object[]
  • Description: 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

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

Example Response Body

{
  "apexTestClasses":[
    {
      "id":"01pxx0000004UVl",
      "name":"BAAdditionTest",
      "namespacePrefix":"my_namespace",
      "testMethods":[
        {"name":"testNegativeAddition"},
        {"name":"testSimpleAddition"}
      ]
    },
    {
      "id":"01pxx0000004UXN",
      "name":"BABitwiseTest",
      "namespacePrefix":"my_namespace",
      "testMethods":[
        {"name":"testBitwiseAND"},
        {"name":"testBitwiseOR"}
      ]
    }
  ],
  "message":null,
  "nextRecordsUrl":"/services/data/v68.0/tooling/tests?namespacePrefix=my_namespace&pageSize=2&nextRecord=my_namespace.BAComparisonTest",
  "size":10,
  "testSetSignature":"91a678b54197669171f11eb824d6765a"
}