ABAP Unit Test

参考http://www.saptechnical.com/Tutorials/OOPS/ABAPUnit/Index.htm写的代码,做了些扩展,测试了subroutine的的Unit Test. 其实写Test Case加大程序员的工作量是肯定的,但好处也是很明显的,为了写好Unit Test, 程序员必然会更加注重程序的结构化和模块化,就不大会出现一个Subroutine几百行上千行的情况了。

REPORT  zgary_t004.

*----------------------------------------------------------------------*
*       CLASS math DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS math DEFINITION.
  PUBLIC SECTION.
    METHODS divide
            IMPORTING opr1    TYPE i
                      opr2    TYPE i
            EXPORTING result  TYPE f
            RAISING   cx_sy_arithmetic_error.
    METHODS factorial
            IMPORTING n TYPE i
            RETURNING value(fact) TYPE i.
ENDCLASS.                              "math DEFINITION

*----------------------------------------------------------------------*
*       CLASS math IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS math IMPLEMENTATION.

  METHOD divide.
    result = opr2 / opr1.
  ENDMETHOD.                           "divide

  METHOD factorial.
    fact = 1.
    IF n = 0.
      RETURN.
    ELSE.
      DO n TIMES.
        fact = fact * sy-index.
      ENDDO.
    ENDIF.
  ENDMETHOD.                           "factorial
ENDCLASS.                              "math IMPLEMENTATION

START-OF-SELECTION.

  DATA w_obj  TYPE REF TO math.
  DATA exc    TYPE REF TO cx_sy_arithmetic_error.
  DATA res    TYPE f.
  DATA result TYPE i.
  DATA text   TYPE string.

  CREATE OBJECT w_obj.
  TRY.
      w_obj->divide( EXPORTING opr1   = 0
                               opr2   = 4
                     IMPORTING result = res ).
      WRITE : res.
      text = res.
    CATCH cx_sy_arithmetic_error INTO exc.
      text = exc->get_text( ).
      MESSAGE text TYPE 'I'.
  ENDTRY.

  CREATE OBJECT w_obj.

  COMPUTE result = w_obj->factorial( 4 ).

  WRITE :/ 'The result for factorial is:',result.


  PERFORM frm_test USING 1 2
                   CHANGING result.


*&---------------------------------------------------------------------*
*&      Form  FRM_TEST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_1      text
*      -->P_2      text
*      <--P_LV_RESULT  text
*----------------------------------------------------------------------*
FORM frm_test  USING    pv_1 TYPE i
                        pv_2 TYPE i
               CHANGING pc_result TYPE i.

  pc_result = pv_1 + pv_2.

ENDFORM.                    " FRM_TEST


*Unit test code========================================================
*----------------------------------------------------------------------*
*       CLASS mytest DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS mytest DEFINITION   "#AU Risk_Level Harmless
             FOR TESTING. "#AU Duration Short
  PRIVATE SECTION.
    METHODS mytest_factorial   FOR TESTING.
    METHODS mytest_divide      FOR TESTING.
    METHODS mytest_divide_0    FOR TESTING.
    METHODS mytest_frm_test    FOR TESTING.
ENDCLASS.                    "mytest DEFINITION

*----------------------------------------------------------------------*
*       CLASS mytest IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS mytest IMPLEMENTATION.
  METHOD mytest_factorial.
    CREATE OBJECT w_obj.
    result = w_obj->factorial( 4 ).
    cl_aunit_assert=>assert_equals( act    = result
                                    exp    = '24'
                                    msg    = 'Factorial Not calculated Correctly'
                                    level  = '0'
                                    quit   = '2'
                                    tol    = '0.999'
                                     ).
  ENDMETHOD.                    "mytest

  METHOD mytest_divide.
    CREATE OBJECT w_obj.
    DATA: lv_res     TYPE f,
          lv_res_exp TYPE f,
          lv_res_str TYPE string.

    lv_res_exp = 4 / 32.
    lv_res_str = lv_res_exp.

    w_obj->divide( EXPORTING opr1   = 32
                             opr2   = 4
                   IMPORTING result = lv_res ).

    cl_aunit_assert=>assert_equals( act    = lv_res
                                    exp    = lv_res_str
                                    msg    = 'Divide Not calculated Correctly'
                                    level  = '0'
                                    quit   = '2'
                                    tol    = '0.999'
                                     ).
  ENDMETHOD.                    "mytest

  METHOD mytest_divide_0.

    CREATE OBJECT w_obj.
    DATA: lv_res     TYPE f,
          lv_res_exp TYPE f,
          lv_res_str TYPE string.

    TRY.
        w_obj->divide( EXPORTING opr1   = 0
                                 opr2   = 4
                       IMPORTING result = lv_res ).
        lv_res_str = res.
      CATCH cx_sy_arithmetic_error INTO exc.
        lv_res_str = exc->get_text( ).
    ENDTRY.

    cl_aunit_assert=>assert_equals( act    = lv_res_str
                                    exp    = 'Division by zero'
                                    msg    = 'Divide Not calculated Correctly'
                                    level  = '0'
                                    quit   = '2'
                                    tol    = '0.999'
                                     ).
  ENDMETHOD.                    "mytest

  METHOD mytest_frm_test.

    DATA: lv_res     TYPE i.

    PERFORM frm_test USING 1 2
                     CHANGING lv_res.

    cl_aunit_assert=>assert_equals( act    = lv_res
                                    exp    = 4
                                    msg    = 'PERFORM frm_test calculated Correctly'
                                    level  = '0'
                                    quit   = '2'
                                    tol    = '0.999'
                                     ).
  ENDMETHOD.                    "mytest
ENDCLASS.                    "mytest IMPLEMENTATION


 

执行Unit test:

Report:

FM:

 

参考文档:

How to write these tests:

ABAP unit is based on ABAP objects. The global class CL_AUNIT_ASSERT contains methods which can be used for testing .Tests are implemented in local classes. Inside the local class the necessary method from the global class can be called for testing. These test classes can be written inside the program for which the test is to be done. It will not affect our production code in anyways.  

Difference between Ordinary class and Test class:

Both the test class and test method should have FOR TESTING addition.  

Ex:

   CLASS mytest DEFINITION FOR TESTING.

      PRIVATE SECTION.

      METHODS mytest FOR TESTING.

   ENDCLASS.                               

Methods in CL_AUNIT_ASSERT for Testing:  

  • ASSERT_EQUALS

  • ASSERT_DIFFERS

  • ASSERT_BOUND

  • ASSERT_NOT_BOUND

  • ASSERT_INITIAL

  • ASSERT_NOT_INITIAL

  • ASSERT_CHAR_CP

  • ASSERT_CHAR_NP

  • ASSERT_EQUALS_F

  • FAIL

  • ABORT  

ASSERT_EQUALS - Checks the equality of two data objects.

ASSERT_DIFFERS - Checks for the difference of two data objects.

ASSERT_BOUND - checks for the validity of the reference of a reference variable.

ASSERT_INITIAL - checks whether the reference of a reference variable is invalid.

ASSERT_NOT_INITIAL - checks whether the data object is not having its initial value.

ASSERT_SUBRC - checks for the specific value of SY-SUBRC.     

ASSERT_EQUALS:  

ASSERT_EQUALS is one of the methods in the class CL_AUNIT_ASSERT. This method can be used for checking equality of two data objects.  

The parameters of the method:  

     ACT           - Actual result

     EXP            - Expected Result

     MSG          - Message to be displayed in the result

     LEVEL      - Error level (Tolerable/Critical/fatal)

     QUIT         - If the test fails, flow level is controlled using this                        

                          (NO/METHOD/CLASS/PROGRAM)

     TOL           - Tolerance level for F  

Levels:

  • 0  -  Tolerable

  • 1  -  Critical

  • 2  -  Fatal

Quit:

  • No ( 0 ) –  It will continue the current test Method.

  • Method ( 1 )   –  It will interrupt the current test method

  • Class ( 2 )    –  It will interrupt the current test class.

  • Program ( 3 )  –  abandon execution of all test classes for the tested  program.  

Tolerance:

If the tolerance limit specified is exceeded then error is shown.

Ex:

Actual result – 24.

Expected Result – 25.

Tolerance – 0.9999.       
Difference = Expected Result - Actual result.
                  = 1 > tolerance.
Therefore displays an error.  

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值