I’m frequently asked if Provar can create apex unit tests. The answer to that question is…
No, for now at least.
The second question I get asked is, “Can you run apex unit tests?”, and the answer to that is a definite yes.
Running apex unit tests
My normal answer is to point people to the Salesforce developer docs and explain the different options for doing that:
- Create a Provar test to navigate into Salesforce Setup using the UI and interact with the apex test execution and history options. This is possible but it requires more effort than what is necessary.
- Use Provar’s asynchronous apex option to run the tests. This is relatively low effort, but reading the output to get the result is not for the faint of heart.
- 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 to run a specific method in a test class, but you need to learn how to both 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 complex Provar test that was long running, but I only wanted to run this Provar test case if a certain 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 apex code. However, while trying to get the result I discovered an additional option that was cleaner and much easier to use that prompted me to look into other Salesforce system objects.
The solution I implemented used Provar’s inbuilt Salesforce API support to request the tests to be executed by inserting requests in the ApexTestQueueItem object. This made the tests run asynchronous so I then used a Wait For step to check the unit test — the results were then ready and I simply 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:
A final change I made was 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.
Once you have your result (Outcome = Completed), you can query many other objects to find interesting things about your unit tests. You can refer to the Apex Developers Guide for more info:
- You need to query ApexClass to find the ID of the test you want to execute. You should query 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 🙂
- Useful for 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. This 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! If you want to check the code coverage for an ApexClass, then you need to 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 have a requirement for unit test automation (yes, generation of unit tests), then 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.
Trailhead Apex Testing module:
Apex Developers Guide: Running Unit Test Methods: