Show TOC

Step 4: Traversing the Hierarchy of ResultsLocate this document in the navigation structure

Procedure

If you ask for the code coverage result of a high-level entity, then the Coverage API also automatically calculates the code coverages of all of the subentities of the high-level entity.

Example: If you ask for the code coverage of the application component BC-ABA, then the Coverage API returns not only the aggregate code coverage of the component, but also the code coverages of all of the subcomponents, packages, and programs in the component.

In this section we show you how to traverse this coverage result tree.

Note

If you wish to have the code coverage calculated only for the top-level object, you can prevent the recursive calculation. You can also configure the depth to which the code coverage should be recursively calculated. See Configuring Results.

Sample Code

Here is how to traverse the hierarchy of entities in such a coverage result tree.

DATA: root_node TYPE REF TO if_scv_result_node,
      subnodes TYPE if_scv_result_node=>tab,
      subnode  TYPE REF TO if_scv_result_node,
      coverage TYPE REF TO if_scv_coverage,
      text TYPE string.

root_node = result->get_root_node( ).
subnodes = root_node->get_children( ).

LOOP AT subnodes INTO subnode.
  " Report the statement coverage of child objects
  coverage = subnode->get_coverage( 
    if_scv_coverage=>con_statement ).
  text = |Object { subnode->name } statement coverage is { coverage->get_percentage( ) }%|.
  WRITE: / text.
ENDLOOP.

            

This example returns the statement coverage of all of the children of the entity for which RESULT was calculated.

Example: If RESULT is calculated for a package, then this code snippet would report on the code coverage of the subpackages and programs that belong to the package.

This code snippet produces ABAP list output suitable, for example, for downloading in a simple text file format. But the Coverage API also lets you display and analyze code-coverage results in a more comfortable form online. Go on to Step 5: Displaying Results.

Here is the complete sample program.

Note

The IF_SCV_COVERAGE constants shown above in the sample code are effective only in SAP NetWeaver 7.0 EHP2 up until Support Package 5 or 6. In higher releases, the IF_SCV_COVERAGE constants are deleted and are replaced with these constants:

  • CE_SCV_COVERAGE_TYPE=>BRANCH

  • CE_SCV_COVERAGE_TYPE=>PROCEDURE

  • CE_SCV_COVERAGE_TYPE=>STATEMENT

If you have used the old constants, then you must change your code if you upgrade or port your code to such a higher release.