Chilkat Online Tools

Swift / Salesforce Platform APIs / Tooling Run Tests Async

Back to Collection Items

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()
    var success: Bool

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "classNames": "comma-separated list of class names",
    //   "classids": "comma-separated list of class IDs",
    //   "suiteNames": "comma-separated list of test suite names",
    //   "suiteids": "comma-separated list of test suite IDs",
    //   "maxFailedTests": "integer value",
    //   "testLevel": "TestLevel enum value",
    //   "skipCodeCoverage": "boolean value"
    // }

    let json = CkoJsonObject()
    json.UpdateString("classNames", value: "comma-separated list of class names")
    json.UpdateString("classids", value: "comma-separated list of class IDs")
    json.UpdateString("suiteNames", value: "comma-separated list of test suite names")
    json.UpdateString("suiteids", value: "comma-separated list of test suite IDs")
    json.UpdateString("maxFailedTests", value: "integer value")
    json.UpdateString("testLevel", value: "TestLevel enum value")
    json.UpdateString("skipCodeCoverage", value: "boolean value")

    // Adds the "Authorization: Bearer <access_token>" header.
    http.AuthToken = "<access_token>"
    http.SetRequestHeader("Content-Type", value: "application/json")

    var resp: CkoHttpResponse? = http.PostJson3("https://domain.com/services/data/v{{version}}/tooling/runTestsAsynchronous", contentType: "application/json", json: json)
    if http.LastMethodSuccess == false {
        print("\(http.LastErrorText)")
        return
    }

    let sbResponseBody = CkoStringBuilder()
    resp!.GetBodySb(sbResponseBody)

    let jResp = CkoJsonObject()
    jResp.LoadSb(sbResponseBody)
    jResp.EmitCompact = false

    print("Response Body:")
    print("\(jResp.Emit())")

    var respStatusCode: Int = resp!.StatusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp!.Header)")
        print("Failed.")
        resp = nil
        return
    }

    resp = nil

}

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
    "classNames": "comma-separated list of class names",
    "classids": "comma-separated list of class IDs",
    "suiteNames": "comma-separated list of test suite names",
    "suiteids": "comma-separated list of test suite IDs",
    "maxFailedTests": "integer value",
    "testLevel": "TestLevel enum value",
    "skipCodeCoverage": "boolean value"
}

or 

{
    "tests": [
        {
            "className": "YourClassName",
            "testMethods": [
                "testMethod1",
                "testMethod2",
                "testMethod3"
            ]
        }
    ],
    "maxFailedTests": "integer value",
    "testLevel": "TestLevel enum value",
    "skipCodeCoverage": "boolean value"
}'
https://domain.com/services/data/v{{version}}/tooling/runTestsAsynchronous

Postman Collection Item JSON

{
  "name": "Tooling Run Tests Async",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"classNames\": \"comma-separated list of class names\",\n    \"classids\": \"comma-separated list of class IDs\",\n    \"suiteNames\": \"comma-separated list of test suite names\",\n    \"suiteids\": \"comma-separated list of test suite IDs\",\n    \"maxFailedTests\": \"integer value\",\n    \"testLevel\": \"TestLevel enum value\",\n    \"skipCodeCoverage\": \"boolean value\"\n}\n\nor \n\n{\n    \"tests\": [\n        {\n            \"className\": \"YourClassName\",\n            \"testMethods\": [\n                \"testMethod1\",\n                \"testMethod2\",\n                \"testMethod3\"\n            ]\n        }\n    ],\n    \"maxFailedTests\": \"integer value\",\n    \"testLevel\": \"TestLevel enum value\",\n    \"skipCodeCoverage\": \"boolean value\"\n}\n",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/tooling/runTestsAsynchronous",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "tooling",
        "runTestsAsynchronous"
      ]
    },
    "description": "Runs one or more methods within one or more Apex classes, using the asynchronous test execution mechanism. In the request body, you can specify test class names and IDs, suite names and IDs, the maximum number of failed tests to allow, and the test level, as comma-separated lists or as an array. You can also indicate whether to opt out of collecting code coverage information during the test run (available in API version 43.0 and later)."
  },
  "response": [
    {
      "name": "Tooling Run Tests Async",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"tests\": [\n        {\n            \"className\": \"PrepareSandboxTest\",\n            \"testMethods\": [\n                \"runApexClass_should_work\"\n            ]\n        }\n    ],\n    \"maxFailedTests\": \"1\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{_endpoint}}/services/data/v{{version}}/tooling/runTestsAsynchronous",
          "host": [
            "{{_endpoint}}"
          ],
          "path": [
            "services",
            "data",
            "v{{version}}",
            "tooling",
            "runTestsAsynchronous"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 11 Dec 2023 10:04:34 GMT"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=63072000; includeSubDomains"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-Robots-Tag",
          "value": "none"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache,must-revalidate,max-age=0,no-store,private"
        },
        {
          "key": "Sforce-Limit-Info",
          "value": "api-usage=250/15000"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=UTF-8"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        }
      ],
      "cookie": [
      ],
      "body": "\"7074H0000DCt2IZ\""
    }
  ]
}