ABAP单元测试(UT)的两个简单例子

背景

最近写的接口分支太复杂,每次修改以后手工测试较为耗时,且无法考虑所有场景,为保证代码的质量和稳定,决定学习ABAP unit test相关知识,以下是自己实现的几个例子,Step by Step的参考链接如下
Understanding ABAP Unit Testing

源代码

  • 针对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.
  • 针对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执行步骤

  • 点击下图所示按钮
    ABAP单元测试(UT)的两个简单例子_第1张图片
  • 分析结果
    若case对应的标识为绿色,则证明assertion成立, 测试通过
    同时也应关注coverage Metrics标签,研究业务逻辑分支(函数及子例程)的覆盖状况,理想状态是所有分支都100%被执行,代码标记为绿色
    ABAP单元测试(UT)的两个简单例子_第2张图片

总结

  • 对于新开发程序,建议使用面向对象(OO)的方式,传统的子例程对UT不太友好

你可能感兴趣的:(OO,函数技巧,UT,abap,单元测试,面向对象编程)