Two simple examples of ABAP unit testing (UT)

background

The recently written interface branch is too complicated. Manual testing after each modification is time-consuming, and it is impossible to consider all scenarios. In order to ensure the quality and stability of the code, I decided to learn ABAP unit test related knowledge. The following are a few examples of my own implementation, Step The reference link of by Step is as follows:
Understanding ABAP Unit Testing

Source code

  • For class
*&---------------------------------------------------------------------*
*& Report YPWK_UT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report ypwk_ut.

class lcl_date_utility definition create public.
  public section.
    methods:get_work_year importing i_date type syst-datum
                          exporting e_year type dec10_2
                                    e_msg  type string,
            select_domain exporting e_record_count type i.

endclass.

class lcl_date_utility implementation.
  method get_work_year.
    if i_date gt sy-datlo.
      message e064(00) into e_msg.
    else.
      e_year = ( sy-datlo - i_date ) / 365.
      e_year = round( val = e_year dec = 1 ).
    endif.

  endmethod.

  method select_domain.

    select count( * )
      into e_record_count
      from zlookup
      up to 100 rows.

  endmethod.
endclass.

" testing class
class lcl_demo_unit_test definition for testing risk level harmless  duration short.

  public section.
    methods:ut_01_get_success_workyear for testing,
      ut_02_get_error_workyear for testing,
      ut_03_select_domain for testing.

  private section.
    class-data: mo_ospl_double type ref to if_osql_test_environment.
    class-methods:class_setup,
      class_teardown.

    methods:setup,
      teardown.

    data:mo_cut    type ref to lcl_date_utility,
         lt_domain type table of zlookup with default key.
endclass.

class lcl_demo_unit_test implementation.
  method ut_01_get_success_workyear.
    data:lv_hire_date type d value '20150701'.
    mo_cut->get_work_year(
    exporting
      i_date = lv_hire_date
    importing
      e_year = data(lv_year)
      e_msg = data(lv_msg)
    ).
    
    cl_abap_unit_assert=>assert_equals(
    exporting
      act = lv_year
      exp = round( val = ( sy-datlo - lv_hire_date ) / 365 dec = 1  )
    ).

  endmethod.

  method ut_02_get_error_workyear.
    data:lv_hire_date  type d value '20200701',
         lv_expect_msg type string.
         
    mo_cut->get_work_year(
    exporting
      i_date = lv_hire_date
    importing
    e_year = data(lv_year)
    e_msg = data(lv_msg)
          ).

    message e064(00) into lv_expect_msg.
    cl_abap_unit_assert=>assert_equals(
    exporting
      act = lv_msg
      exp = lv_expect_msg
      ).
  endmethod.

  method ut_03_select_domain.
    mo_cut->select_domain(
    importing
        e_record_count = data(lv_count)
    ).

    cl_abap_unit_assert=>assert_equals(
    exporting
      act = lv_count
      exp = 1
    ).

  endmethod.

  method class_setup.

    mo_ospl_double = cl_osql_test_environment=>create(
    exporting
      i_dependency_list = value #( ( 'ZLOOKUP' ) )
    ).
  endmethod.

  method setup.
    mo_cut = new lcl_date_utility( ).

    append initial line to lt_domain.
    mo_ospl_double->insert_test_data( lt_domain ).

  endmethod.

  method teardown.
    clear mo_cut.

    mo_ospl_double->clear_doubles( ).
  endmethod.

  method class_teardown.
    mo_ospl_double->destroy( ).
  endmethod.

endclass.

  • Put the code in a separate subroutine for FM
*&---------------------------------------------------------------------*
*& 包含               LYPWK_TEST_UT
*&---------------------------------------------------------------------*
class lcl_ywpk_ut definition for testing risk level harmless duration short.
  public section.
    methods: ut_01_success for testing,
      ut_02_fail for testing.
endclass.
class lcl_ywpk_ut implementation.
  method ut_01_success.
    data:lv_hire_date type d value '20150701'.
    data:lv_year type dec10_2,
         lv_msg  type char100.
         
    call function 'YPWK_GET_WORK_YEAR'
      exporting
        i_date = lv_hire_date
      importing
        e_year = lv_year
        e_msg  = lv_msg.

    "Then
    cl_abap_unit_assert=>assert_equals(
    exporting
      act = lv_year
      exp = round( val = ( sy-datlo - lv_hire_date ) / 365 dec = 1  )
      ).
  endmethod.
  method ut_02_fail.
    data:lv_hire_date type d value '20200701'.
    data:lv_year type dec10_2,
         lv_msg  type char100.

    call function 'YPWK_GET_WORK_YEAR'
      exporting
        i_date = lv_hire_date
      importing
        e_year = lv_year
        e_msg  = lv_msg.

    message e064(00) into data(lv_message_exp).
    cl_abap_unit_assert=>assert_equals(
    exporting
      act = lv_msg
      exp = lv_message_exp
      ).

  endmethod.
endclass.

UT execution steps

  • Click the button shown below
    ABAP UT
  • Analysis result
    If the label corresponding to the case is green, it proves that assertion is established.
    At the same time , when the test is passed, you should also pay attention to the coverage Metrics label to study the coverage of business logic branches (functions and subroutines). The ideal state is that all branches are 100% executed , The code is marked in green
    Code is executed

to sum up

  • For newly developed programs, it is recommended to use an object-oriented (OO) approach, traditional subroutines are not friendly to UT

Guess you like

Origin blog.csdn.net/u012232542/article/details/106502530