Wednesday 29 August 2012

Running Tests and Code Coverage - Apex

Now that you’ve added the tests, you need to run them and inspect their results. In addition to ensuring that the tests pass, you’ll be able to find out how much of the code was covered by your tests.
There are many ways to run tests in Apex. You can either run all tests in a single class or all tests in all classes through the Apex Classes pages. You can also execute the tests asynchronously in the Apex Test Execution page, which allows you to check the results at a later time and access saved test results. Last but not least, you can also start the asynchronous execution of tests by writing Apex code that inserts and queries API objects.
For the purposes of this tutorial, you’ll use the Apex Classes page in the user interface and then inspect the code coverage results.
  1. Click Your Name | Setup | Develop | Apex Classes.
  2. Locate the TestInvoiceStatementDeletion link in the list of classes and click it.
  3. From the detail page of the TestInvoiceStatementDeletion class, click Run Test.
  4. After all test methods in the class execute, the Apex Test Result page displays, containing a summary of the test results.

    Summary of test results 
    The Summary section of the page shows that three tests were run with zero failures, which means that all our tests passed. The code coverage is 100%, which means that all code lines in the trigger were exercised by the tests. This section also contains a link to the debug log that contains debug log output of the executed code.
  5. Scroll down to the Code Coverage section. This section contains the code coverage of all triggers and classes in the organization. You can find the code coverage of the trigger listed there.

    Code coverage section in the Apex Test Result page 
  6. Click the coverage percentage number to see the statements covered in the trigger. In this case, you’ll see two blue lines corresponding to the statements that have code coverage. If there are statements that aren’t covered by a test, they appear as red lines. In our case, there are no red lines because we have 100% coverage.
  7. To update code coverage results for the trigger on the Apex Triggers page, click Apex Classes, then click Calculate your organization’s code coverage.
  8. Click Apex Triggers. You can view the code coverage of the trigger listed at 100% in the Code Coverage column.
    Alternatively, you can click Run All Tests, which runs all tests in your organization and updates the code coverage results on the page at once.

1 comment: