Chilkat Online Tools

PowerBuilder / Salesforce Platform APIs / Tooling Run Tests Sync

Back to Collection Items

integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_Jarr
oleobject loo_JsonObj_1
oleobject loo_SbRequestBody
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_Id
string ls_Name
string ls_Namespace
integer li_NumLocations
integer li_NumLocationsNotCovered
string ls_V_type
integer j
integer li_Count_j
string ls_Message
string ls_MethodName
integer li_SeeAllData
integer li_Time
string ls_ApexLogId
integer li_NumFailures
integer li_NumTestsRun
integer li_TotalTime
integer i
integer li_Count_i

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

loo_Http = create oleobject
// Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

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

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

// [
//   {
//     "className": "YourClassName",
//     "testMethods": [
//       "testMethod1",
//       "testMethod2",
//       "testMethod3"
//     ]
//   },
//   {
//     "maxFailedTests": "2"
//   }
// ]

loo_Jarr = create oleobject
// Use "Chilkat_9_5_0.JsonArray" for versions of Chilkat < 10.0.0
li_rc = loo_Jarr.ConnectToNewObject("Chilkat.JsonArray")

loo_Jarr.AddObjectAt(-1)
loo_JsonObj_1 = loo_Jarr.ObjectAt(loo_Jarr.Size - 1)
loo_JsonObj_1.UpdateString("className","YourClassName")
loo_JsonObj_1.UpdateString("testMethods[0]","testMethod1")
loo_JsonObj_1.UpdateString("testMethods[1]","testMethod2")
loo_JsonObj_1.UpdateString("testMethods[2]","testMethod3")
destroy loo_JsonObj_1

loo_Jarr.AddObjectAt(-1)
loo_JsonObj_1 = loo_Jarr.ObjectAt(loo_Jarr.Size - 1)
loo_JsonObj_1.UpdateString("maxFailedTests","2")
destroy loo_JsonObj_1

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

loo_SbRequestBody = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbRequestBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Jarr.EmitSb(loo_SbRequestBody)

loo_Resp = loo_Http.PTextSb("POST","https://domain.com/services/data/v{{version}}/tooling/runTestsSynchronous",loo_SbRequestBody,"utf-8","application/json",0,0)
if loo_Http.LastMethodSuccess = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Jarr
    destroy loo_SbRequestBody
    return
end if

loo_SbResponseBody = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)

loo_JResp = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Resp
    destroy loo_Http
    destroy loo_Jarr
    destroy loo_SbRequestBody
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

destroy loo_Resp

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "apexLogId": "07L4H00000SpGroUAF",
//   "codeCoverage": [
//     {
//       "id": "01q4H000000WjD8QAK",
//       "locationsNotCovered": [
//         {},
//         {}
//       ],
//       "name": "StateTrigger",
//       "namespace": null,
//       "numLocations": 2,
//       "numLocationsNotCovered": 2,
//       "type": "Trigger"
//     },
//     {
//       "id": "01q58000000RLTyAAO",
//       "locationsNotCovered": [
//         {},
//         {},
//         {}
//       ],
//       "name": "RestrictContactByName",
//       "namespace": null,
//       "numLocations": 3,
//       "numLocationsNotCovered": 3,
//       "type": "Trigger"
//     },
//     {
//       "id": "01q58000000RLBhAAO",
//       "locationsNotCovered": [
//         {},
//         {},
//         {},
//         {},
//         {},
//         {}
//       ],
//       "name": "ClosedOpportunityTrigger",
//       "namespace": null,
//       "numLocations": 6,
//       "numLocationsNotCovered": 6,
//       "type": "Trigger"
//     },
//     {
//       "id": "01p4H000009PEFIQA4",
//       "locationsNotCovered": [
//         {},
//         {}
//       ],
//       "name": "PrepareSandbox",
//       "namespace": null,
//       "numLocations": 22,
//       "numLocationsNotCovered": 2,
//       "type": "Class"
//     }
//   ],
//   "codeCoverageWarnings": [
//     {
//       "id": "01q4H000000WjD8QAK",
//       "message": "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required",
//       "name": "StateTrigger",
//       "namespace": null
//     },
//     {
//       "id": "01q58000000RLTyAAO",
//       "message": "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required",
//       "name": "RestrictContactByName",
//       "namespace": null
//     },
//     {
//       "id": "01q58000000RLBhAAO",
//       "message": "Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required",
//       "name": "ClosedOpportunityTrigger",
//       "namespace": null
//     },
//     {
//       "id": "01q4H000000WjD8QAK",
//       "message": "Average test coverage across all Apex Classes and Triggers is 60%, at least 75% test coverage is required.",
//       "name": null,
//       "namespace": ""
//     }
//   ],
//   "failures": [
//   ],
//   "flowCoverage": [
//   ],
//   "flowCoverageWarnings": [
//   ],
//   "numFailures": 0,
//   "numTestsRun": 1,
//   "successes": [
//     {
//       "id": "01p4H00000A0zYxQAJ",
//       "methodName": "runApexClass_should_work",
//       "name": "PrepareSandboxTest",
//       "namespace": null,
//       "seeAllData": false,
//       "time": 1088
//     }
//   ],
//   "totalTime": 1261
// }

// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

ls_ApexLogId = loo_JResp.StringOf("apexLogId")
li_NumFailures = loo_JResp.IntOf("numFailures")
li_NumTestsRun = loo_JResp.IntOf("numTestsRun")
li_TotalTime = loo_JResp.IntOf("totalTime")
i = 0
li_Count_i = loo_JResp.SizeOfArray("codeCoverage")
do while i < li_Count_i
    loo_JResp.I = i
    ls_Id = loo_JResp.StringOf("codeCoverage[i].id")
    ls_Name = loo_JResp.StringOf("codeCoverage[i].name")
    ls_Namespace = loo_JResp.StringOf("codeCoverage[i].namespace")
    li_NumLocations = loo_JResp.IntOf("codeCoverage[i].numLocations")
    li_NumLocationsNotCovered = loo_JResp.IntOf("codeCoverage[i].numLocationsNotCovered")
    ls_V_type = loo_JResp.StringOf("codeCoverage[i].type")
    j = 0
    li_Count_j = loo_JResp.SizeOfArray("codeCoverage[i].locationsNotCovered")
    do while j < li_Count_j
        loo_JResp.J = j
        j = j + 1
    loop
    i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("codeCoverageWarnings")
do while i < li_Count_i
    loo_JResp.I = i
    ls_Id = loo_JResp.StringOf("codeCoverageWarnings[i].id")
    ls_Message = loo_JResp.StringOf("codeCoverageWarnings[i].message")
    ls_Name = loo_JResp.StringOf("codeCoverageWarnings[i].name")
    ls_Namespace = loo_JResp.StringOf("codeCoverageWarnings[i].namespace")
    i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("failures")
do while i < li_Count_i
    loo_JResp.I = i
    i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("flowCoverage")
do while i < li_Count_i
    loo_JResp.I = i
    i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("flowCoverageWarnings")
do while i < li_Count_i
    loo_JResp.I = i
    i = i + 1
loop
i = 0
li_Count_i = loo_JResp.SizeOfArray("successes")
do while i < li_Count_i
    loo_JResp.I = i
    ls_Id = loo_JResp.StringOf("successes[i].id")
    ls_MethodName = loo_JResp.StringOf("successes[i].methodName")
    ls_Name = loo_JResp.StringOf("successes[i].name")
    ls_Namespace = loo_JResp.StringOf("successes[i].namespace")
    li_SeeAllData = loo_JResp.BoolOf("successes[i].seeAllData")
    li_Time = loo_JResp.IntOf("successes[i].time")
    i = i + 1
loop


destroy loo_Http
destroy loo_Jarr
destroy loo_SbRequestBody
destroy loo_SbResponseBody
destroy loo_JResp

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '[
    {
        "className": "YourClassName",
        "testMethods": [
            "testMethod1",
            "testMethod2",
            "testMethod3"
        ]
    },
    {
        "maxFailedTests": "2"
    }
]

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/runTestsSynchronous

Postman Collection Item JSON

{
  "name": "Tooling Run Tests Sync",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "[\n    {\n        \"className\": \"YourClassName\",\n        \"testMethods\": [\n            \"testMethod1\",\n            \"testMethod2\",\n            \"testMethod3\"\n        ]\n    },\n    {\n        \"maxFailedTests\": \"2\"\n    }\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/runTestsSynchronous",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "tooling",
        "runTestsSynchronous"
      ]
    },
    "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 Sync",
      "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/runTestsSynchronous",
          "host": [
            "{{_endpoint}}"
          ],
          "path": [
            "services",
            "data",
            "v{{version}}",
            "tooling",
            "runTestsSynchronous"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 11 Dec 2023 09:59:37 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=246/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": "{\n    \"apexLogId\": \"07L4H00000SpGroUAF\",\n    \"codeCoverage\": [\n        {\n            \"id\": \"01q4H000000WjD8QAK\",\n            \"locationsNotCovered\": [\n                {},\n                {}\n            ],\n            \"name\": \"StateTrigger\",\n            \"namespace\": null,\n            \"numLocations\": 2,\n            \"numLocationsNotCovered\": 2,\n            \"type\": \"Trigger\"\n        },\n        {\n            \"id\": \"01q58000000RLTyAAO\",\n            \"locationsNotCovered\": [\n                {},\n                {},\n                {}\n            ],\n            \"name\": \"RestrictContactByName\",\n            \"namespace\": null,\n            \"numLocations\": 3,\n            \"numLocationsNotCovered\": 3,\n            \"type\": \"Trigger\"\n        },\n        {\n            \"id\": \"01q58000000RLBhAAO\",\n            \"locationsNotCovered\": [\n                {},\n                {},\n                {},\n                {},\n                {},\n                {}\n            ],\n            \"name\": \"ClosedOpportunityTrigger\",\n            \"namespace\": null,\n            \"numLocations\": 6,\n            \"numLocationsNotCovered\": 6,\n            \"type\": \"Trigger\"\n        },\n        {\n            \"id\": \"01p4H000009PEFIQA4\",\n            \"locationsNotCovered\": [\n                {},\n                {}\n            ],\n            \"name\": \"PrepareSandbox\",\n            \"namespace\": null,\n            \"numLocations\": 22,\n            \"numLocationsNotCovered\": 2,\n            \"type\": \"Class\"\n        }\n    ],\n    \"codeCoverageWarnings\": [\n        {\n            \"id\": \"01q4H000000WjD8QAK\",\n            \"message\": \"Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required\",\n            \"name\": \"StateTrigger\",\n            \"namespace\": null\n        },\n        {\n            \"id\": \"01q58000000RLTyAAO\",\n            \"message\": \"Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required\",\n            \"name\": \"RestrictContactByName\",\n            \"namespace\": null\n        },\n        {\n            \"id\": \"01q58000000RLBhAAO\",\n            \"message\": \"Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required\",\n            \"name\": \"ClosedOpportunityTrigger\",\n            \"namespace\": null\n        },\n        {\n            \"id\": \"01q4H000000WjD8QAK\",\n            \"message\": \"Average test coverage across all Apex Classes and Triggers is 60%, at least 75% test coverage is required.\",\n            \"name\": null,\n            \"namespace\": \"\"\n        }\n    ],\n    \"failures\": [],\n    \"flowCoverage\": [],\n    \"flowCoverageWarnings\": [],\n    \"numFailures\": 0,\n    \"numTestsRun\": 1,\n    \"successes\": [\n        {\n            \"id\": \"01p4H00000A0zYxQAJ\",\n            \"methodName\": \"runApexClass_should_work\",\n            \"name\": \"PrepareSandboxTest\",\n            \"namespace\": null,\n            \"seeAllData\": false,\n            \"time\": 1088\n        }\n    ],\n    \"totalTime\": 1261\n}"
    }
  ]
}