8

Is there a way to force a unit test to run immediately? You used to be able to go to App Setup > Developer > Apex Classes > [ClassName] > Run Test button. This used to begin processing a unit test right away. With the new changes to how unit tests are run, this now just queues up the test in the Apex Test Execution page. So, outside of that, you can just use the Apex Test Execution page and select the tests you want to run as well. However, like the previous issue, that simply queues up the test and Salesforce determines when it should start up. Finally, from the web interface, you can use the Developer Console. However, the Developer Console functions exactly the same as the Apex Test Execution page and simply queues it up.

I am trying to follow TDD as well as possible in the Salesforce environment, but waiting for these queued up tests is taking too long. It can take anywhere from a second to a few minutes for these tests to finally run. I understand I can use Eclipse, but I have had several issues in the past where it simply takes much longer in Eclipse than it would through the UI. I am just trying to see if there is a way to force a test to run immediately rather than having to wait for Salesforce to decide it wants to run the test.

  • There is a similar question - Faster unit tests?, but it doesn't take into account the recent changes to how test cases are queued. It does cover a number of external tools for using the Apex SOAP runTests() method to execute the tests immediately. – Daniel Ballinger Mar 3 '13 at 20:47
  • Running a single salesforce unit test containing the assertion 1==1 will take anywhere from 5 to 30 minutes. So quick turnaround on tests is impossible. – Eric Leschinski Mar 6 '15 at 18:25
6

Nope. :(

Ever once in a while these things stay queued forever, but normally it's very fast. IMHO, this is going to be a good thing since my biggest pet peeve is waiting ~45minutes for a deployment to validate (the whole time seeing 1 error teasing me in the window) while all tests run serially. Running all tests in the same org via the test execution framework is about 4-5x faster. Hopefully the next step in the roadmap is to use this for deployments and get those back to being reasonable!!

As a final note, I love Test Driven Development (TDD), but as of today, it's not all that viable a strategy for Salesforce.

  • 1
    I agree. I love TDD as well. From a Java background, where running thousands of tests can process in a matter of minutes, being able to constantly run a test in about the span of a second, the framework is 100% viable. In Salesforce where you must setup the entire data model on every single test, not so much. – Jesse Altman Mar 2 '13 at 12:59
6

They can be run synchronously using Apex SOAP runTests but I acknowledge this is not via the Web UI. I did find it a nice workaround though during times the browser runs seemed sluggish.

One could sub a Session Id and test classes into the following tests.xml:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <env:Header>
    <SessionHeader xmlns="http://soap.sforce.com/2006/08/apex">
      <sessionId>00De00000001234...OXR</sessionId>
    </SessionHeader>
    <CallOptions xmlns="http://soap.sforce.com/2006/08/apex">
      <defaultNamespace xsi:nil="true" />
      <clientLog xsi:nil="true" />
      <debugExceptions>true</debugExceptions>
      <platform xsi:nil="true" /><remoteApplication xsi:nil="true" />
    </CallOptions>
  </env:Header>
  <env:Body>
    <sf:runTests xmlns:sf="http://soap.sforce.com/2006/08/apex">
      <sf:RunTestsRequest>
        <sf:allTests>false</sf:allTests>
        <sf:classes>YourTestClass</sf:classes>
      </sf:RunTestsRequest>
    </sf:runTests>
  </env:Body>
</env:Envelope>

and in the absence of a client, curl it up the wire to test if it's any faster:

curl -H "Content-type: text/xml; charset=utf-8" \
  -H "SOAPAction: \"\"" \
  -d @tests.xml \
  -X POST https://cs15-api.salesforce.com/services/Soap/s/26.0/00De00000001234

and maybe use xpath to extract the test result:

./aboveBashScript.sh | xpath /*/soapenv:Body/runTestsResponse/result/failures/message
  • In theory, you could convert that to Visualforce page with an Apex callout. But I haven't tried (yet). Also the ContainerAsyncRequest object in the Tooling API might work. salesforce.com/us/developer/docs/api_toolingpre/api_tooling.pdf – joshbirk Mar 2 '13 at 4:43
  • Isn't the SOAP call going to essentially do the same thing as the the other methods by just queuing up the tests? My main problem is that I have to queue them up and wait until Salesforce wants to run them. I want to take that out of Salesforce's hands so I can run them whenever I want. Thanks for the advice/code snippet though, I will definitely look into it more. – Jesse Altman Mar 2 '13 at 13:01
  • hey @JesseAltman, the SOAP call is synchronous; the response body contains the test results. – bigassforce Mar 2 '13 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.