用CL_AUNIT_ASSERT 类进行ABAP单元测试 ABAP Unit TEST

ゝ一纸荒年。 2022-06-17 23:38 237阅读 0赞

转自:http://www.saptechnical.com/Tutorials/OOPS/ABAPUnit/Index.htm

#

Understanding “ABAP Unit”

By Nithya Murugesan, YASH Technologies

Introduction:

It is a best practice to modularize our programs as much as we can for better programming. If we want to check one particular module like subroutines, function modules or classes for bugs then we can do it using ABAP Unit. ABAP Unit is a tool for unit testing of ABAP programs.

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.

  1. PRIVATE SECTION.
  2. 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:

  1. **ACT** - Actual result
  2. **EXP** - Expected Result
  3. **MSG** - Message to be displayed in the result
  4. **LEVEL** - Error level (Tolerable/Critical/fatal)
  5. **QUIT** - If the test fails, flow level is controlled using this
  6. (NO/METHOD/CLASS/PROGRAM)
  7. **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.

Example Program:

Let us consider an example for ABAP unit test using the method ASSERT_EQUALS to check the equality of two data objects. In this program, we have two methods divide and factorial in a local class MATH. We want to test the factorial method. So we have created one class and one method MYTEST for testing. In the test method implementation we have called the factorial method and so the data object RESULT is populated. Now we are going to compare the actual data object (RESULT) with the expected result. For that we are calling the ASSERT_EQUALS from the global class passing the expected result.

REPORT ZWTEST1 .

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.

class math implementation.
method divide.
result = opr1 / opr2.
ENDMETHOD.
method factorial.
fact = 1.
if n = 0.
return.
else.
do n times.
fact = fact * sy-index.
enddo.
endif.
endmethod.
ENDCLASS.
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 = 32 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.
*———————————————————————————————————*
* CLASS mytest DEFINITION
*———————————————————————————————————*
*
*———————————————————————————————————*
CLASS mytest DEFINITION “#AU Risk_Level Harmless
FOR TESTING. “#AU Duration Short
PRIVATE SECTION.
METHODS mytest FOR TESTING.
ENDCLASS. “mytest DEFINITION*———————————————————————————————————*
* CLASS mytest IMPLEMENTATION
*———————————————————————————————————*
*
*———————————————————————————————————*
CLASS mytest IMPLEMENTATION.
METHOD mytest.
CREATE OBJECT w_obj.
result = w_obj->factorial( 4 ).
cl_aunit_assert=>assert_equals( act = result
exp = ‘240’
msg = ‘Factorial Not calculated Correctly’
level = ‘0’
quit = ‘2’
tol = ‘0.999’
).
ENDMETHOD. “mytest
ENDCLASS.

注释: 要激活程序 在左上角菜单 program——> test——>unit test

  1. 使用code inspectorprogram ---> check--->code inspector
  2. ABAP Unit出来 需要自己创建一个默认的entry code inspector

发表评论

表情:
评论列表 (有 0 条评论,237人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ABAP指针

    关于abap指针的一些资料。 1.什么是abap指针: 在abap里面,fieldsymbol就相当于c语言的指针。如果你定义并且分配了相应的结构或者变量给它,其实它