ABAP Unit Test (ABAP 单元测试的实现)

一.概述
在项目中我们将常会碰到逻辑十分复杂的程序, 但我们只修改复杂程序的一小部分,对于修改部分的测试会很难进行。首先我们可能不知道程序全部的逻辑,编写测试case会十分的复杂,需要浪费大量的时间。因此ABAP Unit 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:
在这里插入图片描述
如果测试通过,会出现成功的message

如果单元测试中的代码运行不通过,会出现以下界面
在这里插入图片描述
通过方法的名称,可以查看测试方法的哪些部分在测试期间导致了错误:
在这里插入图片描述

参考文档:

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.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂的刘刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值