I’m frequently asked if running apex tests with Provar is possible. The answer to that question is

No, for now, at least.

The second question I get is, “Can you run apex unit tests?” The answer to that is a definite yes.

Running apex unit tests.

My usual answer is to point people to the Salesforce developer docs and explain the different options for doing that:

  1. Create a Provar test to navigate into Salesforce Setup using the UI and interact with the apex test execution and history options. It is possible, but it requires more effort than what is necessary. 
  2. Use Provar’s asynchronous apex option to run the tests. It is a relatively low effort,  but reading the output to get the result is not for the faint of heart.
  3. Use the Salesforce tooling API to make REST web service calls to initiate the unit test request. It’s an easy way to request ‘run all tests’ or run a specific method in a test class, but you need to learn how to construct the request payload and read the response.

It turns out that I recently needed to do this myself with Provar. I wanted to reuse a long-running, complex Provar test, but I only wanted to run this Provar test case if a specific apex unit test was working and if certain data had been loaded. If these unit tests were working, I knew my deployment (to a scratch org) had finished, and my ProvarDX tests were worth spending the resources running on my cloud infrastructure provider.

Using Provar to run apex tests

My initial choice was to make the tooling API call (option 3) so that the person running the test didn’t need to understand the apex code. However, while trying to get the result, I discovered an additional option that was cleaner and much easier to use, which prompted me to look into other Salesforce system objects.

The solution I implemented used Provar’s built-in Salesforce API support to request the tests be executed by inserting requests in the ApexTestQueueItem object. It made the tests run asynchronously, so I then used a Wait For the step to check the unit test — the results were then ready, and I read back the entries. 

The beauty of this solution is that I knew which record ID I had inserted into already. It was fast, and I didn’t have to maintain a JSON payload for the web service request if Salesforce changed it in the future. I also felt it was much easier for my team to maintain.

Here’s a snippet of my Provar test case:

I made a final change to make this Provar test callable, define an input variable for the apex unit test to be executed, and set an output variable for the result. I can now reuse this anywhere I want to check a unit test.

The results

Once you have your result (Outcome = Completed), you can query many other objects to find exciting things about your unit tests. You can refer to the Apex Developers Guide for more info:

    • It would help if you queried ApexClass to find the ID of the test you want to execute. It would help if you queried it by name to make your test independent of the specific environment that you’re running against.
    • To run a test, you only need to insert a record with the ApexClassId of the ApexClass to execute. You then get to sit back and query the results 🙂
  • ApexTestResult 
    • Valid for an outcome, failure message, stack trace, log ID, and execution times
    • For synchronous tests, results for each unit test method
    • In my example above, I only needed to use this object to get my results
    • Contains execution results for each applicable governor limit for each ApexTestResult record
    • Summary information for the test run is useful if executing multiple tests. It includes the number of test methods executed and failed.
    • Cannot be directly inserted using DML calls but can be created via Tooling API and queried
    • Useful if you want to query results of tests scheduled via the other techniques to initiate unit test execution

Note! To check the code coverage for an ApexClass, you must use the Tooling API and the ApexCodeCoverageAggregate object. You cannot query this object using a regular SOQL call. We’d be happy to add this as a feature to Provar. If this is something you want to access regularly, contact us.

Finally, if you require unit test automation (yes, generation of unit tests), please get in touch with me, as I’d be interested to learn more about how this would work within your development lifecycle and which tools you’d be willing to use. You can also find me on Twitter

Further reading

Trailhead Apex Testing module:


Apex Developers Guide: Running Unit Test Methods: