来源资料待手动核验
项目简介
Create snapshots in ABAP
Create snapshots in ABAP
Create snapshots either in the debugger or directly in your unit-tests. Snapshots are stored in ECATT test data containers (TDC).
Snapshots can be created with class ``zdbglsnapshotstdc``. The following listing shows a sample unit-test using snapshots.
CLASS test_snapshot DEFINITION FOR TESTING
DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
METHODS flight_test FOR TESTING
RAISING zcx_dbgl_snapshot.
ENDCLASS.
CLASS test_snapshot IMPLEMENTATION.
METHOD flight_test.
DATA(snapshot_manager) = zdbgl_snapshots_tdc=>create_default(
tdc_name = 'ZDBGL_SNAPSHOT_SAMPLE'
).
DATA(carrier) = VALUE scarr_tab(
( carrid = 'SAP' carrname = 'SAP Flights' )
( carrid = 'IN' carrname = 'Internat. Airways' )
).
DATA(flight) = VALUE sflight( carrid = 'SAP' connid = 700 fldate = '20201231' ).
snapshot_manager->compare_or_record( name = 'CARRIER' actual = carrier ).
snapshot_manager->compare_or_record( name = 'FLIGHT' actual = flight ).
" can be omitted, when autosave = abap_true in factory method or constructor
snapshot_manager->commit_changes( ).
ENDMETHOD.
ENDCLASS.The method ``snapshotmanager->compareorrecord` compares the given value with the recorded value or records the value. We need to give every value a unique name in the parameter `name`. The behavior (recording or comparing) is controlled with the SET/GET-parameter `ZDBGLSNAPRECORD``. A value of 'X' means 'create/update the snapshot'. Parameter ZDBGLSNAPRECORD
The standard implementation uses the method ``clabapunitassert=>assertequals` for comparison. Override the method `zdbglsnapshotstdc=>compare`` to specify another comparison method.
Another option to create snapshots is the debugger script ``zdbglscriptstoreintdc``. Prerequisite is an existing TDC.
Go the transaction ``secatt`` and create a test data container. Add a parameter for every variable you want to record. The parameter should have the same name and type as the variable. Last but not least give the test data container API access as shown in the picture below. Permit api access
Set an appropriate breakpoint and create the snapshot with the debugger-script ``zdbglscriptstoreintdc``. Load script This script asks you for the name, the version and the variant of the test data container as shown in the picture below. Prompt tdc key
With the API in the class ``claplecatttdcapi`` we can access the snapshot.
The example can be found in the program ``zdbgldemoregressiontest``.
The procedure ``toverify`` is the procedure under test.
FORM to_verify.
FIELD-SYMBOLS: <line> TYPE sflight.
APPEND INITIAL LINE TO demo_itab ASSIGNING <line>.
<line>-carrid = 'LH'.
<line>-connid = '3444'.
<line>-price = 400.
ENDFORM.In the first step we create a snapshot before and after this procedure is executed.
In the second step we use the snapshot (test data container "ZDBGLSAMPLE") to write unit-tests:
REPORT ZDBGL_DEMO_REGRESSION_TEST.
DATA: demo_itab TYPE STANDARD TABLE OF sflight.
START-OF-SELECTION.
FIELD-SYMBOLS: <line> TYPE sflight.
APPEND INITIAL LINE TO demo_itab ASSIGNING <line>.
<line>-carrid = 'LH'.
<line>-connid = '3445'.
<line>-price = 500.
BREAK-POINT. "snapshot is created
PERFORM to_verify.
BREAK-POINT. "snapshot is created
" subprogram should be verified.
" It changes the global variable demo_itab.
FORM to_verify.
FIELD-SYMBOLS: <line> TYPE sflight.
APPEND INITIAL LINE TO demo_itab ASSIGNING <line>.
<line>-carrid = 'LH'.
<line>-connid = '3444'.
<line>-price = 400.
ENDFORM.
CLASS regression_test DEFINITION FOR TESTING
DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA: tdc_accessor TYPE REF TO cl_apl_ecatt_tdc_api.
METHODS setup
RAISING cx_static_check.
METHODS verify_changed_itab FOR TESTING
RAISING cx_static_check.
ENDCLASS.
CLASS regression_test IMPLEMENTATION.
METHOD setup.
tdc_accessor = cl_apl_ecatt_tdc_api=>get_instance( EXPORTING
i_testdatacontainer = 'ZDBGL_SAMPLE' i_testdatacontainer_version = 1 ).
ENDMETHOD.
METHOD verify_changed_itab.
DATA: exp_demo_itab LIKE demo_itab.
" given: use the snapshot before procedure under test was executed
tdc_accessor->get_value( EXPORTING i_param_name = 'DEMO_ITAB' i_variant_name = 'BEFORE'
CHANGING e_param_value = demo_itab ).
" when: execute procedure under test
PERFORM to_verify.
" then: use the snapshot after the procedure under test was executed
tdc_accessor->get_value( EXPORTING i_param_name = 'DEMO_ITAB' i_variant_name = 'AFTER'
CHANGING e_param_value = exp_demo_itab ).
cl_abap_unit_assert=>assert_equals( exp = exp_demo_itab
act = demo_itab msg = 'Regression test not passed' ).
ENDMETHOD.
ENDCLASS.These types are currently supported:
These types are not supported:
There is a legacy API to create snapshots in the debugger, which uses the database tables ``zdbglvariables` and `zdbgllocals`` as a temporary storage and which is bit more difficult to use. This API is documented in the docs-folder.
Installation is done with abapGit. ABAP 7.40 or higher is needed. All ABAP-objects have the prefix ``zdbgl``.
Exceptions are logged in the checkpoint-groups "zdbglstoreglobals" and "zdbglstorelocals" (see transaction ``saab``). Logging is only active, if the checkpoint-groups is activated.