Monday, February 7, 2011

Understanding "ABAP Unit"

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.

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.

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.

*----------------------------------------------------------------------*
* 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 = 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 = '24'
msg = 'Factorial Not calculated Correctly'
level = '0'
quit = '2'
tol = '0.999'
).
ENDMETHOD. "mytest
ENDCLASS. "mytest IMPLEMENTATION

Executing Unit Tests:

For program,

Program -> Test -> Unit Test.

For class,

Class -> Unit Test.

For Function Module,

Function Module -> Test -> Unit Test.

Result of Unit Test:

If both the actual and the expected result is same, then Unit test does not find any errors. In that case one message will be displayed on status bar like,

If it finds errors then a result will be displayed as follows:

The task is displayed in a tree structure with a Program name, Class name and method name. Both the expected and the actual results can be seen in the Unit test results. Also in the stack it will be displaying the line number where the error occurred. By double clicking the line number we can enter into the source code.

ABAP Unit results in Code Inspector:

We can see the ABAP unit results in code inspector. While creating the variant, check for the ABAP unit in Dynamic check.

In the Code inspector results we can check for the ABAP unit errors, warnings and informations.




No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites