Show TOC

Measurement Tutorial: Sample ProgramLocate this document in the navigation structure

Definition

This sample program includes the calls to the Coverage API that are explained in the individual tutorial sections.

The program creates a measurement of code coverage. By running the program, you do the following:

  • Create a measurement in the Coverage API

  • Run a test program and measure its code coverage

  • Calculate the code coverage of the package to which the test program belongs

  • Display the code coverage results in graphical and in text form

  • Save the results and discard the raw data from which the result was calculated.

The program shows how you could instrument your test programs for quick, ad hoc checks of their code coverage.

As an alternative to inline instrumentation, you can measure activities by test users you specify over a period of time or during their use of a testing infrastructure.

*&---------------------------------------------------------------------*
*& Report  Z_COVERAGE_API_EXAMPLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  z_coverage_api_example.

* Measure the code coverage of a test program inline in a single
* session.  The program makes no permanent changes to your system
* and so may be run in a customer system.

* The measurement
DATA: factory     TYPE REF TO if_scv_factory,
      measurement TYPE REF TO if_scv_measurement,
      users       TYPE if_scv_measurement=>users,
      testkey     TYPE cva_testk.

* The results
DATA: result    TYPE REF TO if_scv_result,
      coverage  TYPE REF TO if_scv_coverage,
      text      TYPE string.

* The results for entities in package
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.

* The display
DATA: display TYPE REF TO if_scv_result_display.

* Optionally saving the measurement and results
DATA: repository     TYPE REF TO if_scv_repository,
      measurement_id TYPE cva_uuid,
      result_id      TYPE cva_uuid.

* Exceptions
DATA: execution_error TYPE REF TO cx_scv_execution_error,
      call_error      TYPE REF TO cx_scv_call_error.

* Create a measurement and specify user to measure
* Code coverage will be measured only on the local ABAP server
TRY.
    factory = cl_scv_coverage_api=>get_factory( ).

* Default is actually sy-uname if no user is specified
    APPEND 'AUNIT_USER' TO users.

    measurement = factory->create_measurement(
      i_name              = 'myMeasurement'
      i_local_server_only = abap_true
      i_users             = users ).

* Start the measurement and execute the test programs whose
* code coverage is to be measured.
    measurement->start( ).
* Execute anything you want to measure
    SUBMIT rsdssmpl_status AND RETURN. "Harmless standard CCMS report

* Stop the measurement - no further code coverage data may be added
    measurement->stop( ).

* Obtain the test key - Optional - just to show that it can be done
    testkey = measurement->get_testkey( ).

* Calculate the code coverage in package SMOI - processing block,
* branch, and statement coverage are all calculated by default.
* The coverages of all subcomponents of SMOI are also calculated
    result    = measurement->build_package_result( 'SMOI' ).

* Display the results in a graphical pop-up - show_as_fullscreen also available
    display = cl_scv_coverage_ui=>get_result_display( result ).
    display->show_as_popup( ).

* For download to a mail, for example, add the results in list form
* Write the statement coverage of package SMOI
    coverage  = result->get_coverage( if_scv_coverage=>con_statement ).
    text = |Package SMOI has a statement coverage of 
            { coverage->get_percentage( ) }%|.
    WRITE:/ text.

* Add the statement coverage of the program objects in package SMOI
    root_node = result->get_root_node( ).
    subnodes = root_node->get_children( ).

    LOOP AT subnodes INTO subnode.
      coverage = subnode->get_coverage( if_scv_coverage=>con_statement ).
      text = |Object { subnode->name } has a statement coverage of 
              { coverage->get_percentage( ) }%|.
      WRITE: / text.
    ENDLOOP.

* Optionally: Save the results for future re-use
* You need to save the ID to retrieve the results - 
*   see also the measurement series tutorial.
* The measurement is implicitly saved as well.
    repository = cl_scv_coverage_api=>get_repository( ).
    result_id = repository->save_result( result ).

* Finalize the measurement to discard the raw data
* The measurement and results are not deleted
    measurement->finalize( ).

* Exception handling
  CATCH cx_scv_execution_error INTO execution_error.
    text = |Execution error occurred. Message: { execution_error->get_text( ) }|.
    WRITE: / text.
  CATCH cx_scv_call_error INTO call_error.
    text = |Call error occurred. Message: { execution_error->get_text( ) }|.
    WRITE: / text.
ENDTRY.
         
Note

The IF_SCV_COVERAGE constants shown above in the sample code are effective only in SAP NetWeaver 7.0 EHP2 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.