Dieses Beispielprogramm beinhaltet die Aufrufe für den Coverage API, die in den einzelnen Abschnitten des Tutorials erläutert werden.
Das Programm legt eine Messung der Quelltextabdeckung an. Führen Sie in diesem Programm folgende Schritte aus:
Legen Sie eine Messung im Coverage API an.
Führen Sie ein Testprogramm aus und messen Sie seine Quelltextabdeckung.
Berechnen Sie die Quelltextabdeckung des Pakets, zu dem das Testprogramm gehört.
Zeigen Sie die Ergebnisse der Quelltextabdeckung in graphischer Form und als Text an.
Sichern Sie die Ergebnisse und löschen Sie die Rohdaten, aus denen die Ergebnisse berechnet wurden.
Das Programm zeigt an, wie Sie Ihre Testprogramme für schnelle Ad-hoc-Prüfungen ihrer Quelltextabdeckung instrumentieren können.
Als Alternative zur Inline-Instrumentierung können Sie die Aktivitäten von Textbenutzern messen, die Sie über einen bestimmten Zeitraum oder während ihrer Verwendung einer Testinfrastruktur angeben.
*&---------------------------------------------------------------------* *& 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.
Die oben im Coding-Beispiel verwendeten IF_SCV_COVERAGE-Konstanten sind nur in SAP NetWeaver 7.0 EHP2 bis zum Support-Package-Level 5 oder 6 wirksam. In höheren Releases werden die IF_SCV_COVERAGE-Konstanten gelöscht und durch die folgenden Konstanten ersetzt:
CE_SCV_COVERAGE_TYPE=>BRANCH
CE_SCV_COVERAGE_TYPE=>PROCEDURE
CE_SCV_COVERAGE_TYPE=>STATEMENT
Wenn Sie die alten Konstanten verwendet haben, müssen Sie Ihren Quelltext für einen Upgrade ändern oder Ihren Quelltext zu einem solch höheren Release portieren.