And don’t tell me I’m a bore. ABAP unit test worth it!

Here how it was. Once I told you about Test Driven Development methodology, but today I decided to try it in practice how it works in SAP. I created basic OData Service and decided to test it automatically.

Now I want to test it automatically. Earlier I told about SECATT. Today let’s talk about Unit testing. It such a thing which is developed in ABAP and allows to test logic inside of a program. It does not simulate user input but verifies internal procedures, methods, functions for a supposed result. For example, it should always save correct data but never allows to save incorrect data. To implement we create two methods – positive result test and negative result test.

There is the class Data Provider Class (DPC), which provides data over CRUD for OData model. It means we send HTTP GET request, for example, and it sends us back some data after authentification. We need a test which verifies the data is sent and data is equal to etalon.

For this, we need to develop a local class which tests DPC methods. Sources are below.


ABAP Unit Test Example

Look. Except that program can run these tests, gather statistics, run everything in the background, thereby automating quality assurance, could live together with Code Inspector; besides all of these, it can think! Well, think in its own way – it finds out what code is covered and what is not by our test cases.

ABAP Unit Test

ABAP Unit Test

The cool thing is the system shows what business-cases you have are not covered in the unit-test (test scenarios). Such hint will remind what business case is not developed in the test scenario.

Imagine tomorrow you’ll get a new support request. Somebody fix something and everything goes down. Here not to move all these broken stuff to production system we run unit-tests. Unit test will check everything in the development system, ensure this or that piece of code works not how it was supposed in test-case, And then you need to fix code in case of business logic change or fix error in your code. Don’t forget, it runs not only one particular unit-test, but dozens and hundreds of them being developed by other developers during system maintenance. Very convenient!

class zcl_Ut_Z_Hrbot definition for testing
  duration short
  risk level harmless

  private section.

    data: mo_request_context_object type ref to /iwbep/cl_mgw_request_unittst.
    data: ms_request_context_struct type /iwbep/cl_mgw_request_unittst=>ty_s_mgw_request_context_unit.
    data: mo_data_provider type ref to zcl_Z_Hrbot_01_Dpc_Ext.

    methods: setup.
    methods: teardown.
    methods: lt_get_Entityset for testing.
endclass.       "zcl_Ut_Z_Hrbot

class zcl_Ut_Z_Hrbot implementation.

  method setup.

    create object mo_data_provider .


  method teardown.


  method lt_get_Entityset.

    DATA: lr_data                 TYPE REF TO data.
    DATA: ls_response_context     TYPE /iwbep/if_mgw_appl_types=>ty_s_mgw_response_context.
    DATA: ls_data                 TYPE zmes_quota.

    ms_request_context_struct-technical_request-source_entity_type = 'Quota'.
    ms_request_context_struct-technical_request-target_entity_type = 'Quota'.
    ms_request_context_struct-technical_request-source_entity_set  = 'QuotaSet'.
    ms_request_context_struct-technical_request-target_entity_set  = 'QuotaSet'.

    mo_request_context_object = mo_data_provider->/iwbep/if_mgw_conv_srv_runtime~init_dp_for_unit_test(
      is_request_context = ms_request_context_struct ).

    try .
            io_tech_request_context   = mo_request_context_object
            er_entityset              = lr_data
            es_response_context       = ls_response_context

      catch /iwbep/cx_mgw_busi_exception.

      catch /iwbep/cx_mgw_tech_exception.


    assign lr_data->* to .
    loop at  into ls_data.

      act   = ls_data-pernr
      exp   = 1
      msg   = 'Testing one employee'
*     level =