What is the best way to run test classes from Apex code? SOAP Apis or Rest API?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












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.










share|improve this question







New contributor




allgood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    2
    down vote

    favorite












    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.










    share|improve this question







    New contributor




    allgood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      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.










      share|improve this question







      New contributor




      allgood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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.







      unit-test rest-api soap-api






      share|improve this question







      New contributor




      allgood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      allgood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      allgood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      allgood

      132




      132




      New contributor




      allgood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      allgood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      allgood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted











          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.






          share|improve this answer




















          • Thanks @sfdcfox! I was expecting reply from you. Do you think doing this thing synchronously makes a sense? Just to remind again, I want to do this thing in Apex only. You commented on one of the questions that for regression testing you were running the tests once in 24 hours and emailed that result. Did you used asynchronous or synchronous APIs in that?
            – allgood
            2 hours ago











          • @allgood Synchronous limits will prevent most reasonable sized orgs from running tests synchronously. You would need to check back every few minutes. If you're using Visualforce, an actionPoller would be appropriate, otherwise you'll want to set a callback after a few minutes (e.g. a queueable checking for results). P.S. if you're running tests in the current org, you can insert ApexTestQueueItem directly in Apex without a callout...
            – sfdcfox
            2 hours ago










          • @allgood I implemented this in pure Apex code for "the current org" by inserting ApexTestQueueItem records, then setting up a schedulable that checks back every five minutes until it's done.
            – sfdcfox
            2 hours ago










          • yes, my requirement is to run tests in current org but I think it will be an extra headache to prepare a list of all test classes and insert that list in ApexTestQueueItem when we want to run all tests. To get the list of all test classes again we need to use SOSL to find @isTest in classes, correct? :) suggested by you in one of the post.
            – allgood
            2 hours ago










          • @allgood Yes, it's really not that big of a deal. A few lines of code. I think the entire tool I wrote only ended up being 20-30 lines of code. It's not incredibly complicated.
            – sfdcfox
            2 hours ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "459"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          allgood is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f237478%2fwhat-is-the-best-way-to-run-test-classes-from-apex-code-soap-apis-or-rest-api%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted











          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.






          share|improve this answer




















          • Thanks @sfdcfox! I was expecting reply from you. Do you think doing this thing synchronously makes a sense? Just to remind again, I want to do this thing in Apex only. You commented on one of the questions that for regression testing you were running the tests once in 24 hours and emailed that result. Did you used asynchronous or synchronous APIs in that?
            – allgood
            2 hours ago











          • @allgood Synchronous limits will prevent most reasonable sized orgs from running tests synchronously. You would need to check back every few minutes. If you're using Visualforce, an actionPoller would be appropriate, otherwise you'll want to set a callback after a few minutes (e.g. a queueable checking for results). P.S. if you're running tests in the current org, you can insert ApexTestQueueItem directly in Apex without a callout...
            – sfdcfox
            2 hours ago










          • @allgood I implemented this in pure Apex code for "the current org" by inserting ApexTestQueueItem records, then setting up a schedulable that checks back every five minutes until it's done.
            – sfdcfox
            2 hours ago










          • yes, my requirement is to run tests in current org but I think it will be an extra headache to prepare a list of all test classes and insert that list in ApexTestQueueItem when we want to run all tests. To get the list of all test classes again we need to use SOSL to find @isTest in classes, correct? :) suggested by you in one of the post.
            – allgood
            2 hours ago










          • @allgood Yes, it's really not that big of a deal. A few lines of code. I think the entire tool I wrote only ended up being 20-30 lines of code. It's not incredibly complicated.
            – sfdcfox
            2 hours ago














          up vote
          2
          down vote



          accepted











          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.






          share|improve this answer




















          • Thanks @sfdcfox! I was expecting reply from you. Do you think doing this thing synchronously makes a sense? Just to remind again, I want to do this thing in Apex only. You commented on one of the questions that for regression testing you were running the tests once in 24 hours and emailed that result. Did you used asynchronous or synchronous APIs in that?
            – allgood
            2 hours ago











          • @allgood Synchronous limits will prevent most reasonable sized orgs from running tests synchronously. You would need to check back every few minutes. If you're using Visualforce, an actionPoller would be appropriate, otherwise you'll want to set a callback after a few minutes (e.g. a queueable checking for results). P.S. if you're running tests in the current org, you can insert ApexTestQueueItem directly in Apex without a callout...
            – sfdcfox
            2 hours ago










          • @allgood I implemented this in pure Apex code for "the current org" by inserting ApexTestQueueItem records, then setting up a schedulable that checks back every five minutes until it's done.
            – sfdcfox
            2 hours ago










          • yes, my requirement is to run tests in current org but I think it will be an extra headache to prepare a list of all test classes and insert that list in ApexTestQueueItem when we want to run all tests. To get the list of all test classes again we need to use SOSL to find @isTest in classes, correct? :) suggested by you in one of the post.
            – allgood
            2 hours ago










          • @allgood Yes, it's really not that big of a deal. A few lines of code. I think the entire tool I wrote only ended up being 20-30 lines of code. It's not incredibly complicated.
            – sfdcfox
            2 hours ago












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          sfdcfox

          235k10180396




          235k10180396











          • Thanks @sfdcfox! I was expecting reply from you. Do you think doing this thing synchronously makes a sense? Just to remind again, I want to do this thing in Apex only. You commented on one of the questions that for regression testing you were running the tests once in 24 hours and emailed that result. Did you used asynchronous or synchronous APIs in that?
            – allgood
            2 hours ago











          • @allgood Synchronous limits will prevent most reasonable sized orgs from running tests synchronously. You would need to check back every few minutes. If you're using Visualforce, an actionPoller would be appropriate, otherwise you'll want to set a callback after a few minutes (e.g. a queueable checking for results). P.S. if you're running tests in the current org, you can insert ApexTestQueueItem directly in Apex without a callout...
            – sfdcfox
            2 hours ago










          • @allgood I implemented this in pure Apex code for "the current org" by inserting ApexTestQueueItem records, then setting up a schedulable that checks back every five minutes until it's done.
            – sfdcfox
            2 hours ago










          • yes, my requirement is to run tests in current org but I think it will be an extra headache to prepare a list of all test classes and insert that list in ApexTestQueueItem when we want to run all tests. To get the list of all test classes again we need to use SOSL to find @isTest in classes, correct? :) suggested by you in one of the post.
            – allgood
            2 hours ago










          • @allgood Yes, it's really not that big of a deal. A few lines of code. I think the entire tool I wrote only ended up being 20-30 lines of code. It's not incredibly complicated.
            – sfdcfox
            2 hours ago
















          • Thanks @sfdcfox! I was expecting reply from you. Do you think doing this thing synchronously makes a sense? Just to remind again, I want to do this thing in Apex only. You commented on one of the questions that for regression testing you were running the tests once in 24 hours and emailed that result. Did you used asynchronous or synchronous APIs in that?
            – allgood
            2 hours ago











          • @allgood Synchronous limits will prevent most reasonable sized orgs from running tests synchronously. You would need to check back every few minutes. If you're using Visualforce, an actionPoller would be appropriate, otherwise you'll want to set a callback after a few minutes (e.g. a queueable checking for results). P.S. if you're running tests in the current org, you can insert ApexTestQueueItem directly in Apex without a callout...
            – sfdcfox
            2 hours ago










          • @allgood I implemented this in pure Apex code for "the current org" by inserting ApexTestQueueItem records, then setting up a schedulable that checks back every five minutes until it's done.
            – sfdcfox
            2 hours ago










          • yes, my requirement is to run tests in current org but I think it will be an extra headache to prepare a list of all test classes and insert that list in ApexTestQueueItem when we want to run all tests. To get the list of all test classes again we need to use SOSL to find @isTest in classes, correct? :) suggested by you in one of the post.
            – allgood
            2 hours ago










          • @allgood Yes, it's really not that big of a deal. A few lines of code. I think the entire tool I wrote only ended up being 20-30 lines of code. It's not incredibly complicated.
            – sfdcfox
            2 hours ago















          Thanks @sfdcfox! I was expecting reply from you. Do you think doing this thing synchronously makes a sense? Just to remind again, I want to do this thing in Apex only. You commented on one of the questions that for regression testing you were running the tests once in 24 hours and emailed that result. Did you used asynchronous or synchronous APIs in that?
          – allgood
          2 hours ago





          Thanks @sfdcfox! I was expecting reply from you. Do you think doing this thing synchronously makes a sense? Just to remind again, I want to do this thing in Apex only. You commented on one of the questions that for regression testing you were running the tests once in 24 hours and emailed that result. Did you used asynchronous or synchronous APIs in that?
          – allgood
          2 hours ago













          @allgood Synchronous limits will prevent most reasonable sized orgs from running tests synchronously. You would need to check back every few minutes. If you're using Visualforce, an actionPoller would be appropriate, otherwise you'll want to set a callback after a few minutes (e.g. a queueable checking for results). P.S. if you're running tests in the current org, you can insert ApexTestQueueItem directly in Apex without a callout...
          – sfdcfox
          2 hours ago




          @allgood Synchronous limits will prevent most reasonable sized orgs from running tests synchronously. You would need to check back every few minutes. If you're using Visualforce, an actionPoller would be appropriate, otherwise you'll want to set a callback after a few minutes (e.g. a queueable checking for results). P.S. if you're running tests in the current org, you can insert ApexTestQueueItem directly in Apex without a callout...
          – sfdcfox
          2 hours ago












          @allgood I implemented this in pure Apex code for "the current org" by inserting ApexTestQueueItem records, then setting up a schedulable that checks back every five minutes until it's done.
          – sfdcfox
          2 hours ago




          @allgood I implemented this in pure Apex code for "the current org" by inserting ApexTestQueueItem records, then setting up a schedulable that checks back every five minutes until it's done.
          – sfdcfox
          2 hours ago












          yes, my requirement is to run tests in current org but I think it will be an extra headache to prepare a list of all test classes and insert that list in ApexTestQueueItem when we want to run all tests. To get the list of all test classes again we need to use SOSL to find @isTest in classes, correct? :) suggested by you in one of the post.
          – allgood
          2 hours ago




          yes, my requirement is to run tests in current org but I think it will be an extra headache to prepare a list of all test classes and insert that list in ApexTestQueueItem when we want to run all tests. To get the list of all test classes again we need to use SOSL to find @isTest in classes, correct? :) suggested by you in one of the post.
          – allgood
          2 hours ago












          @allgood Yes, it's really not that big of a deal. A few lines of code. I think the entire tool I wrote only ended up being 20-30 lines of code. It's not incredibly complicated.
          – sfdcfox
          2 hours ago




          @allgood Yes, it's really not that big of a deal. A few lines of code. I think the entire tool I wrote only ended up being 20-30 lines of code. It's not incredibly complicated.
          – sfdcfox
          2 hours ago










          allgood is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          allgood is a new contributor. Be nice, and check out our Code of Conduct.












          allgood is a new contributor. Be nice, and check out our Code of Conduct.











          allgood is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f237478%2fwhat-is-the-best-way-to-run-test-classes-from-apex-code-soap-apis-or-rest-api%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery