3

To run all tests or specific test classes we can use both the APIs SOAP and Rest API. Below are the differences that I noticed when done research on both.

  • All test
    • SOAP - Possible, Without mentioning a list of test classes
    • REST - Not possible, You have to specify all classes that you want to run
  • Response
    • SOAP - Provide classes to handle test run results which makes it easy to get list of failed and passed test classes
    • REST - Need to handle the JSON response by our own.

I want to run test classes from Apex code and based on that my requirements are,

  1. To get a list of the failed, passed and aborted test classes list.

  2. I should be able to run all tests with/without mentioning all classes.

1 Answer 1

5

REST - Not possible, You have to specify all classes that you want to run

Wrong. Simply call the Tooling REST API as such:

/services/data/v43.0/tooling/runTestsAsynchronous/

{ "testLevel": "RunLocalTests" }

This will run all tests without specifying names.


REST - Need to handle the JSON response by our own.

You can JSON2Apex from a sample response, so the heavy lifting is just a matter of parsing a class. No "manual" deserializing is necessary at a lower level.

However, since it is running asynchronously, all you get back is a job Id. To get the results later, query for them:

/services/data/v43.0/tooling/query?q=select+apexclass.name,Message,methodname,outcome,runtime,stacktrace+from+apextestresult+where+AsyncApexJobId=%277071T00006ErFY3%27

You'll want to monitor the AsyncApexJob to make sure that the status is either "success" or "failed" before querying for those results.

And, yes, you can do all this through the SOAP API, too. They are roughly analogous in functionality.

2
  • How you have written test classes for your tool. Becuase we can not enqueue test class from a test class?
    – allgood
    Commented Oct 30, 2018 at 19:26
  • @allgood It's been a few years, and I don't have that code any longer, but if I remember correctly, it was mostly stubbed logic via the stub api.
    – sfdcfox
    Commented Oct 30, 2018 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .