Newer Version Available

This content describes an older version of this product. View Latest

runTests()

Run your Apex unit tests.

Syntax

1RunTestsResult[] = 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

1public void runTestsSample() {
2   String sessionId = "sessionID goes here";
3   String url = "url goes here";
4   // Set the Apex stub with session ID received from logging in with the partner API
5   _SessionHeader sh = new _SessionHeader();
6   apexBinding.setHeader(
7      new ApexServiceLocator().getServiceName().getNamespaceURI(),
8      "SessionHeader", sh);
9   // Set the URL received from logging in with the partner API to the Apex stub
10   apexBinding._setProperty(ApexBindingStub.ENDPOINT_ADDRESS_PROPERTY, url);
11
12   // Set the debugging header
13   _DebuggingHeader dh = new _DebuggingHeader();
14   dh.setDebugLevel(LogType.Profiling);
15   apexBinding.setHeader(
16      new ApexServiceLocator().getServiceName().getNamespaceURI(),
17      "DebuggingHeader", dh);
18
19   long start = System.currentTimeMillis();
20   RunTestsRequest rtr = new RunTestsRequest();
21   rtr.setAllTests(true);
22   RunTestsResult res = null;
23   try {
24      res = apexBinding.runTests(rtr);
25   } catch (RemoteException e) {
26      System.out.println("An unexpected error occurred: " + e.getMessage());
27   }
28
29   System.out.println("Number of tests: " + res.getNumTestsRun());
30   System.out.println("Number of failures: " + res.getNumFailures());
31   if (res.getNumFailures() > 0) {
32      for (RunTestFailure rtf : res.getFailures()) {
33         System.out.println("Failure: " + (rtf.getNamespace() ==
34         null ? "" : rtf.getNamespace() + ".")
35         + rtf.getName() + "." + rtf.getMethodName() + ": "
36         + rtf.getMessage() + "\n" + rtf.getStackTrace());
37      }
38   }
39   if (res.getCodeCoverage() != null) {
40      for (CodeCoverageResult ccr : res.getCodeCoverage()) {
41         System.out.println("Code coverage for " + ccr.getType() +
42         (ccr.getNamespace() == null ? "" : ccr.getNamespace() + ".")
43         + ccr.getName() + ": "
44         + ccr.getNumLocationsNotCovered()
45         + " locations not covered out of "
46         + ccr.getNumLocations());
47   
48      if (ccr.getNumLocationsNotCovered() > 0) {
49         for (CodeLocation cl : ccr.getLocationsNotCovered())
50            System.out.println("\tLine " + cl.getLine());
51         }
52      }
53   }
54   System.out.println("Finished in " +
55   (System.currentTimeMillis() - start) + "ms");
56}

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