Running Unit Test Methods

You can run unit tests for:

To run a test, use any of the following:

Running Tests Through the Salesforce User Interface

You can run unit tests on the Apex Test Execution page. Tests started on this page run asynchronously, that is, you don't have to wait for a test class execution to finish. The Apex Test Execution page refreshes the status of a test and displays the results after the test completes.
Apex Test Execution Page
To use the Apex Test Execution page:
  1. From Setup, click Develop | Apex Test Execution.
  2. Click Select Tests....
    If you have Apex classes that are installed from a managed package, you must compile these classes first by clicking Compile all classes on the Apex Classes page so that they appear in the list. See Managing Apex Classes.
  3. Select the tests to run. The list of tests includes only classes that contain test methods.
    • To select tests from an installed managed package, select its corresponding namespace from the drop-down list. Only the classes of the managed package with the selected namespace appear in the list.
    • To select tests that exist locally in your organization, select [My Namespace] from the drop-down list. Only local classes that aren't from managed packages appear in the list.
    • To select any test, select [All Namespaces] from the drop-down list. All the classes in the organization appear, whether or not they are from a managed package.
    Classes with tests currently running don't appear in the list.
  4. Click Run.

After you run tests using the Apex Test Execution page, you can view code coverage details in the Developer Console.

From Setup, click Develop | Apex Test Execution | View Test History to view all test results for your organization, not just tests that you have run. Test results are retained for 30 days after they finish running, unless cleared.

Running Tests Using the IDE

In addition, you can execute tests with the IDE (see

Running Tests Using the Developer Console

The Developer Console enables you to create test runs to execute tests in specific test classes, or to run all tests. The Developer Console runs tests asynchronously in the background allowing you to work in other areas of the Developer Console while tests are running. Once the tests finish execution, you can inspect the test results in the Developer Console. Also, you can inspect the overall code coverage for classes covered by the tests.

You can open the Developer Console in the Salesforce application from Your Name | Developer Console. For more details, check out the Developer Console documentation in the Salesforce online help.

Running Tests Using the API

You can use the runTests() call from the SOAP API to run tests synchronously:
RunTestsResult[] runTests(RunTestsRequest ri)
This call allows you to run all tests in all classes, all tests in a specific namespace, or all tests in a subset of classes in a specific namespace, as specified in the RunTestsRequest object. It returns the following:
  • Total number of tests that ran
  • Code coverage statistics (described below)
  • Error information for each failed test
  • Information for each test that succeeds
  • Time it took to run the test

For more information on runTests(), see the WSDL located at https://your_salesforce_server/services/wsdl/apex, where your_salesforce_server is equivalent to the server on which your organization is located, such as

Though administrators in a Salesforce production organization cannot make changes to Apex code using the Salesforce user interface, it is still important to use runTests() to verify that the existing unit tests run to completion after a change is made, such as adding a unique constraint to an existing field. Salesforce production organizations must use the compileAndTestSOAP API call to make changes to Apex code. For more information, see Deploying Apex.

For more information on runTests(), see SOAP API and SOAP Headers for Apex.

Running Tests Using ApexTestQueueItem

The API for asynchronous test runs is a Beta release.

You can run tests asynchronously using ApexTestQueueItem and ApexTestResult. Using these objects and Apex code to insert and query the objects, you can add tests to the Apex job queue for execution and check the results of completed test runs. This enables you to not only start tests asynchronously but also schedule your tests to execute at specific times by using the Apex scheduler. See Apex Scheduler for more information.

To start an asynchronous execution of unit tests and check their results, use these objects:

Insert an ApexTestQueueItem object to place its corresponding Apex class in the Apex job queue for execution. The Apex job executes the test methods in the class. After the job executes, ApexTestResult contains the result for each single test method executed as part of the test.

To abort a class that is in the Apex job queue, perform an update operation on the ApexTestQueueItem object and set its Status field to Aborted.

If you insert multiple Apex test queue items in a single bulk operation, the queue items will share the same parent job. This means that a test run can consist of the execution of the tests of several classes if all the test queue items are inserted in the same bulk operation.

The maximum number of test queue items, and hence classes, that you can insert in the Apex job queue is the greater of 500 or 10 multiplied by the number of test classes in the organization.

This example shows how to use DML operations to insert and query the ApexTestQueueItem and ApexTestResult objects. The enqueueTests method inserts queue items for all classes that end with Test. It then returns the parent job ID of one queue item, which is the same for all queue items because they were inserted in bulk. The checkClassStatus method retrieves all the queue items that correspond to the specified job ID. It then queries and outputs the name, job status, and pass rate for each class. The checkMethodStatus method gets information of each test method that was executed as part of the job.
public class TestUtil {

    // Enqueue all classes ending in "Test". 
    public static ID enqueueTests() { ApexClass[] testClasses = [SELECT Id FROM ApexClass WHERE Name LIKE '%Test']; if (testClasses.size() > 0) { ApexTestQueueItem[] queueItems = new List<ApexTestQueueItem>(); for (ApexClass cls : testClasses) { queueItems.add(new ApexTestQueueItem(ApexClassId=cls.Id)); } insert queueItems; // Get the job ID of the first queue item returned. ApexTestQueueItem item = [SELECT ParentJobId FROM ApexTestQueueItem WHERE Id=:queueItems[0].Id LIMIT 1]; return item.parentjobid; } return null; } // Get the status and pass rate for each class
    // whose tests were run by the job.
    // that correspond to the specified job ID.
    public static void checkClassStatus(ID jobId) { ApexTestQueueItem[] items = [SELECT ApexClass.Name, Status, ExtendedStatus FROM ApexTestQueueItem WHERE ParentJobId=:jobId]; for (ApexTestQueueItem item : items) { String extStatus = item.extendedstatus == null ? '' : item.extendedStatus; System.debug(item.ApexClass.Name + ': ' + item.Status + extStatus); } } // Get the result for each test method that was executed.
    public static void checkMethodStatus(ID jobId) { ApexTestResult[] results = [SELECT Outcome, ApexClass.Name, MethodName, Message, StackTrace FROM ApexTestResult WHERE AsyncApexJobId=:jobId]; for (ApexTestResult atr : results) { System.debug(atr.ApexClass.Name + '.' + atr.MethodName + ': ' + atr.Outcome); if (atr.message != null) { System.debug(atr.Message + '\n at ' + atr.StackTrace); } } } }
© Copyright 2000–2014, inc. All rights reserved.
Various trademarks held by their respective owners.