Chilkat Online Tools

Android / Salesforce Platform APIs / Tooling Run Tests Async

Back to Collection Items

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    CkHttp http = new CkHttp();
    boolean success;

    // 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"
    // }

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

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

    CkHttpResponse resp = http.PostJson3("https://domain.com/services/data/v{{version}}/tooling/runTestsAsynchronous","application/json",json);
    if (http.get_LastMethodSuccess() == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    resp.GetBodySb(sbResponseBody);

    CkJsonObject jResp = new CkJsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    Log.i(TAG, "Response Body:");
    Log.i(TAG, jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
    if (respStatusCode >= 400) {
        Log.i(TAG, "Response Header:");
        Log.i(TAG, resp.header());
        Log.i(TAG, "Failed.");

        return;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

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\""
    }
  ]
}