Show TOC

Step 3: Calculating Code Coverage ResultsLocate this document in the navigation structure

Procedure

Once you have collected raw data on code coverage, you can calculate coverage results.

Calculating a Result: Sample Code

Here is code for calculating the code coverage of a program and the package to which it belongs. In each case, the code returns the statement coverage.

DATA: measurement TYPE REF TO if_scv_measurement,
      result    TYPE REF TO if_scv_result,
      coverage  TYPE REF TO if_scv_coverage,
      text      TYPE string.

* Get the aggregate statement coverage for package SMOI
result    = measurement->build_package_result( 'SMOI' ).
coverage  = result->get_coverage( if_scv_coverage=>con_statement ).

text = |Package SMOI has a statement coverage of { coverage->get_percentage( ) }%|.
WRITE:/ text.

* Get the statement coverage of a specified program
result    = measurement->build_program_result( 'RSDSSMPL_STATUS' ).
coverage  = result->get_coverage( if_scv_coverage=>con_statement ).

text = |Program RSDSSMPL_STATUS has a statement coverage of { coverage->get_percentage( ) }%|.
WRITE:/ text.

* Extra credit: This method gets all available coverage statistics
DATA: coverage_tab TYPE if_scv_coverage=>tab.
coverage_tab = result->get_coverages( ).

            

As the code shows, you first build a result for a specific entity - an application component, package, program, or selection of such entities. The result is calculated according to the prevailing configuration. The ' BUILD' methods of IF_SCV_MEASUREMENT return objects of type IF_SCV_RESULT to you.

Then, you request code coverage statistics from the result. The IF_SCV_RESULT methods return objects of IF_SCV_COVERAGE to you. You then use methods of IF_SCV_COVERAGE to get the actual statistics.

You can calculate as many different results from a single measurement as you wish - as long as the measurement has not been finalized. The FINALIZE method discards the raw data. The Coverage API also discards raw data through a reorganization mechanism or when the code under measurement is changed.

You must stop a measurement- end the collection of raw data - before you can calculate results.

Note

Note that the BUILD_PROGRAM_RESULT method needs a technical program name for classes and function groups:

  • For classes, this is the name of the class pool, not CL_CCMS_AL_DATA_ENVIRONMENT but CL_CCMS_AL_DATA_ENVIRONMENT===CP.

  • For function groups, this is the name of the function pool, not SALI but SAPLSPALI.

To convert from the commonly visible names of these objects and their members to the technical names, you can use the function module RS_TADIR_TO_PROGNAME. The function module takes as input the TADIR name of a repository object, as shown in the object entry or in a transport request.

Continue on to traversing the hierarchy of results, if the object for which you have calculated coverage has subcomponents.

Here is the complete sample program.

Calculating a Result: Additional Methods

In addition to IF_SCV_MEASUREMENT->BUILD_PACKAGE_RESULT and ->BUILD_PROGRAM_RESULT (shown above), there are two other methods for calculating code coverage results:

  • BUILD_COMPONENT_RESULT- This method calculates the coverage for an application component such as BC-ABA or BC-CCM-MON.

    By default, the method calculates coverages for all of the subcomponents, packages, programs, and processing blocks (methods) contained in the application component.

  • BUILD_SELECTION_RESULT- With this method, you supply a selection object (parameter I_SELECTION) to specify the components, packages, and/or programs for which coverage is to be calculated.

    DATA: measurement TYPE REF TO if_scv_measurement,
          result TYPE REF TO if_scv_result,
          selection TYPE REF TO if_scv_selection.
    
    " Define entities for which results are to be 
    " calculated with if_scv_selection methods
    
    result = measurement->build_selection_result( 
     i_selection = selection ). 
    
                      

By default the method calculates results for any and all subcomponents of a selected entity. For example, BUILD_PACKAGE_RESULT returns by default not only the aggregated coverage of a selected package, but also the coverage results for all subpackages, programs, and processing blocks in the package.

You can configure the results calculation - including the recursive calculation of results - for all of these methods with a configuration object (parameter I_CONFIGURATION).

DATA: result TYPE REF TO if_scv_result,
      measurement TYPE REF TO if_scv_measurement,
      configuration TYPE REF TO if_scv_result_configuration.

" Specify how the result is to be calculated with the 
" methods of if_scv_result_configuration

result = measurement->build_component_result( 
  i_component_name = 'BC-ABA'
  i_configuration = configuration ).

            

Code Coverage Metrics

You can calculate three code coverage measures. As the code example shows, you specify which statistics you wish to see with constants of IF_SCV_COVERAGE. These constants are as follows:

  • IF_SCV_COVERAGE=>CON_BRANCH: The coverage of branches in control structures.

  • IF_SCV_COVERAGE=>CON_PROCEDURE: The coverage of procedures or processing blocks in ABAP code.

  • IF_SCV_COVERAGE=>CON_STATEMENT: The coverage of ABAP statements.

    Note

    The IF_SCV_COVERAGE constants shown above are effective only in SAP NetWeaver 7.0 EHP2 up until Support Package level 5 or 6. In higher release levels, 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.

You can ask for both raw numeric values and calculated code coverage percentages. Here are the available methods:

  • IF_SCV_COVERAGE->GET_EXECUTED: Returns the number of executed branches, statements, or procedures in the specified entity or set of entities in a selection object.

  • IF_SCV_COVERAGE->GET_NOT_EXECUTED: Returns the number of branches, statements, or procedures that were not executed during a measurement.

  • IF_SCV_COVERAGE->GET_PERCENTAGE: Returns the percentage of code coverage for branches, statements, or procedures in the specified entity or selection object.

  • IF_SCV_COVERAGE->GET_TOTAL: Returns the total number of branches, statements, or procedures in the specified entity or set of entities.