runTests()

Run your Apex unit tests.

Syntax

RunTestsResult[] = binding.runTests(RunTestsRequest request);

Usage

To facilitate the development of robust, error-free code, Apex supports the creation and execution of unit tests. Unit tests are class methods that verify whether a particular piece of code is working properly. Unit test methods take no arguments, commit no data to the database, and send no emails. Such methods are flagged with the @isTest annotation in the method definition. Unit test methods must be defined in test classes, that is, classes annotated with @isTest. Use this call to run your Apex unit tests.

This call supports the DebuggingHeader and the SessionHeader.

Sample Code—Java

<!-- otherprops: java set_clipboard -->
public void runTestsSample() {
   String sessionId = "sessionID goes here";
   String url = "url goes here";
   // Set the Apex stub with session ID received from logging in with the partner API
   _SessionHeader sh = new _SessionHeader();
   apexBinding.setHeader(
      new ApexServiceLocator().getServiceName().getNamespaceURI(),
      "SessionHeader", sh);
   // Set the URL received from logging in with the partner API to the Apex stub
   apexBinding._setProperty(ApexBindingStub.ENDPOINT_ADDRESS_PROPERTY, url);

   // Set the debugging header
   _DebuggingHeader dh = new _DebuggingHeader();
   dh.setDebugLevel(LogType.Profiling);
   apexBinding.setHeader(
      new ApexServiceLocator().getServiceName().getNamespaceURI(),
      "DebuggingHeader", dh);

   long start = System.currentTimeMillis();
   RunTestsRequest rtr = new RunTestsRequest();
   rtr.setAllTests(true);
   RunTestsResult res = null;
   try {
      res = apexBinding.runTests(rtr);
   } catch (RemoteException e) {
      System.out.println("An unexpected error occurred: " + e.getMessage());
   }

   System.out.println("Number of tests: " + res.getNumTestsRun());
   System.out.println("Number of failures: " + res.getNumFailures());
   if (res.getNumFailures() > 0) {
      for (RunTestFailure rtf : res.getFailures()) {
         System.out.println("Failure: " + (rtf.getNamespace() ==
         null ? "" : rtf.getNamespace() + ".")
         + rtf.getName() + "." + rtf.getMethodName() + ": "
         + rtf.getMessage() + "\n" + rtf.getStackTrace());
      }
   }
   if (res.getCodeCoverage() != null) {
      for (CodeCoverageResult ccr : res.getCodeCoverage()) {
         System.out.println("Code coverage for " + ccr.getType() +
         (ccr.getNamespace() == null ? "" : ccr.getNamespace() + ".")
         + ccr.getName() + ": "
         + ccr.getNumLocationsNotCovered()
         + " locations not covered out of "
         + ccr.getNumLocations());
   
      if (ccr.getNumLocationsNotCovered() > 0) {
         for (CodeLocation cl : ccr.getLocationsNotCovered())
            System.out.println("\tLine " + cl.getLine());
         }
      }
   }
   System.out.println("Finished in " +
   (System.currentTimeMillis() - start) + "ms");
} 

Arguments

Name Type Description
request RunTestsRequest A request that includes the Apex unit tests and the values for any fields that need to be set for this request.

Response

RunTestsResult