anthyiaconsulting

ABAP Tutorial

3 Testing methods

a Quick Example

You write your test in a class that typically starts with zltc. As all ABAP OO Programming, you separate your testing methods into a defintion block and an implementation block.

1     CLASS zcl_calculator ...
2     *implements a method named add
3     ENDCLASS.
4
5     * Unit Test Class
6     CLASS zltc_simple_calculator_test definition FOR TESTING
7            DURATION SHORT
8            RISK LEVEL HARMLESS.
9            PRIVATE SECTION.
10     DATA mo_calculator TYPE REF TO zcl_calculator.
11
12     METHODS setup.
13     METHODS add_2plus2_sum4 FOR TESTING.
14     METHODS teardown.
15     ENDCLASS.

Listing III.6: Simple Test Class Definition

Note: ABAP testing methods are parameter less.

The RISK␣LEVELattribute describes the effects that a test could have on the system.

Risk Level

Potential Side Effect

CRITICAL

Could alter system settings, customizing etc.

DANGEROUS

Could change records in the database.

HARMLESS

No side effects.

Table III.1: RISK LEVEL attributes

Below is an ABAP test implementation block.

CLASS ZLTC_SIMPLE_CALCULATOR_TEST IMPLEMENTATION.
METHOD setup.
           CREATE OBJECT mo_calculator.
ENDMETHOD.


METHOD add_2plus2_sum4.
           DATA lv_expected TYPE int2 VALUE 4.
           DATA lv_sum TYPE int2.
           lv_sum = mo_calculator->add(
                   EXPORTING
                   iv_first_addend = 2
                   iv_second_addend = 2 ).
           cl_abap_unit_assert=>assert_equals(
                   EXPORTING
                   act = lv_sum
                   exp = lv_expected
                   msg = 'Calculator Addition Test Failed' ).
ENDMETHOD.


METHOD teardown.
           CLEAR mo_calculator.
ENDMETHOD.


ENDCLASS.
Listing III.7: Simple Test Class and Methods

mo_calculator is declared as an object of TYPE␣REF␣TO␣zcl_calculator.

To initiate a test run for the ZLTC_SIMPLE_CALCULATOR_TEST test class defined in the listing , follow the context menu path of the ZLTC_SIMPLE_CALCULATOR␣␣âĂć␣EXECUTE␣âĂć␣UNIT␣TESTS.

Nicolai Nielsen, anthyiaconsulting