ABAP Unit Test with Setup & Teardown Methods

lightABAP Unit Test with Setup & Teardown Methods

 

 


The post provides little idea on the unit test framework special methods – setup & teardown. 

Below we have a global class in tx- SE24 with few methods for which unit test cases to be designed.

The class with attributes.

1

The class with 3 methods. The FLIGHT_CALC method makes use of GET_SCARR & GET_SPFLI method .

2

Each method- signature and its body.

3


4


The FLIGHT_CALC method makes use of both the methods GET_SCARR & GET_SPFLI and final gives back records with countryfr as DE.

5


So our main class and methods are ready to be unit tested. Select the Local Test Classes button to create unit test class.

6


CLASS ltd_flight_calc DEFINITION FOR TESTING
                                           INHERITING FROM zcl_flight_calculation.
PUBLIC SECTION.
CLASS-METHODS: create_double
                                       RETURNING VALUE(rr_insatnce)
                                       TYPE REF TO ltd_flight_calc.
ENDCLASS.

CLASS ltd_flight_calc IMPLEMENTATION.

             METHOD create_double.
                        CREATE OBJECT rr_insatnce.
           ENDMETHOD.

ENDCLASS.

CLASS ltc_flight_calc DEFINITION
                                            FOR TESTING
                                           DURATION SHORT
                                           RISK LEVEL HARMLESS.
PRIVATE SECTION.
       DATA: f_cut TYPE REF TO ltd_flight_calc.
       METHODS: setup.
        METHODS: teardown.
         METHODS: flight_calc_test FOR TESTING.
ENDCLASS.

CLASS ltc_flight_calc IMPLEMENTATION.
 METHOD setup.
       f_cut = ltd_flight_calc=>create_double( ).
 ENDMETHOD.
METHOD flight_calc_test.
       DATA: lt_scarr TYPE scarr_tab,
       lt_spfli_calc TYPE spfli_tab.

         f_cut->flight_calc(
                                    EXPORTING
                                         it_scarr = lt_scarr
                                   IMPORTING
                                         et_spfli_calc = lt_spfli_calc ).

           cl_abap_unit_assert=>assert_equals(
                                   EXPORTING
                                        act = lines( lt_spfli_calc )
                                       exp = 0
                                       msg = ‘Must be zero record’ ).

   ENDMETHOD.
METHOD teardown.
FREE f_cut.
ENDMETHOD.
ENDCLASS.


 

7


From menu, unit test can be executed.

8

The unit test results error.

10


We call the method FLIGHT_CALC in the test class method to be tested. We don’t pass any value to the method and we expect it shouldn’t bring any record after execution that is the goal of the method FLIGHT_CALC.  As out test case result gives error then surely there is some problem in the method code.

Put a break point and run the unit test.

9

We call the method FLIGHT_CALC without any values ( lt_scarr is blank) but it gives us 8 records where as our expectation is 0 records/lines.

11

we applied for all entries in the select query and if requested condition table is blank then it reads all the value. So the methods can be corrected as below by putting a check in GET_SCARR & GET_SPFLI methods.

1213

Now run the unit test.

14

This time we don’t pass any value to the method FLIGHT_CALC and so it returns no value. So the method is working as expected.

15

The unit test is successful and we have the message on the status bar.

16


 

In our test class we have mentioned the SETUP & TEARDOWN method. These are specila methods available in the unit test framework. 

The SETUP method is used to prepare the required data needed for the unit test like object instantiation and others. This method automatically executed before the first test method is called.

The TEARDOWN method is called after the last test method finished its execution and used to free/release all the resources.

To test put break points in the setup method, in the test method and in the teardown method and execute unit test.


CLASS ltd_flight_calc DEFINITION FOR TESTING
                                           INHERITING FROM zcl_flight_calculation.
PUBLIC SECTION.
CLASS-METHODS: create_double
                                      RETURNING VALUE(rr_insatnce)
                                    TYPE REF TO ltd_flight_calc.
ENDCLASS.

CLASS ltd_flight_calc IMPLEMENTATION.

METHOD create_double.
        CREATE OBJECT rr_insatnce.
ENDMETHOD.

ENDCLASS.

CLASS ltc_flight_calc DEFINITION
                                           FOR TESTING
                                           DURATION SHORT
                                          RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA: f_cut TYPE REF TO ltd_flight_calc.
METHODS: setup.
METHODS: teardown.
METHODS: flight_calc_test FOR TESTING.
ENDCLASS.

CLASS ltc_flight_calc IMPLEMENTATION.
     METHOD setup.
            f_cut = ltd_flight_calc=>create_double( ).
      ENDMETHOD.
METHOD flight_calc_test.
DATA: lt_scarr TYPE scarr_tab,
lt_spfli_calc TYPE spfli_tab.
APPEND INITIAL LINE TO lt_scarr ASSIGNING FIELD-SYMBOL(<fs_scarr>).
<fs_scarr>-carrid = ’12’.
f_cut->flight_calc(
EXPORTING
it_scarr = lt_scarr
IMPORTING
et_spfli_calc = lt_spfli_calc ).

cl_abap_unit_assert=>assert_equals(
EXPORTING
act = lines( lt_spfli_calc )
exp = 0
msg = ‘Must be zero record’ ).

ENDMETHOD.
METHOD teardown.
FREE f_cut.
ENDMETHOD.
ENDCLASS.


Here we call with carrid = 12 and it should return us 0 record.

17

Very first the SETUP method is called by the unit test framework.

18

Now its calling the test method.

19

Finally it calls the teardown method.

20

Why the unit test log displayed in YELLOW color. We provided the TIME DURATION is SHORT. Means it should be executed in 60 secs. As in debugging we take more than 60 secs it appears as yellow. So unit test is nit about that the code returns the correct value but also it should be executed with definite time else there may be some performance issue in the code which can be optimized.

21


So far we have seen how to make use of the test class and the test methods.

But in real unit test we can do much more things.

Check the SPFLI method. Our test method FLIGHT_CALC returns all the records with countryfr as DE based on our carrid request.

Check the table.

22

For carrid = LH there are 4 matching records with countryfr as DE and for carrid = UA there are 2 records with countryfr as DE

23


CLASS ltd_flight_calc DEFINITION FOR TESTING
                                            INHERITING FROM zcl_flight_calculation.
PUBLIC SECTION.

CLASS-METHODS: create_double
                                      RETURNING VALUE(rr_insatnce)
                                     TYPE REF TO ltd_flight_calc.

METHODS: set_carrid IMPORTING iv_carrid TYPE scarr-carrid.
PROTECTED SECTION.
METHODS: get_scarr REDEFINITION.

PRIVATE SECTION.
DATA: gv_carrid TYPE scarr-carrid.
ENDCLASS.

CLASS ltd_flight_calc IMPLEMENTATION.

METHOD create_double.
CREATE OBJECT rr_insatnce.
ENDMETHOD.
METHOD set_carrid.
gv_carrid = iv_carrid.
ENDMETHOD.
METHOD get_scarr.
CHECK gv_carrid IS NOT INITIAL.
APPEND INITIAL LINE TO gt_scarr ASSIGNING FIELD-SYMBOL(<fs_scarr>).
<fs_scarr>-carrid = gv_carrid.
ENDMETHOD.
ENDCLASS.

CLASS ltc_flight_calc DEFINITION
                                            FOR TESTING
                                            DURATION SHORT
                                            RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA: f_cut TYPE REF TO ltd_flight_calc.
METHODS: setup.
METHODS: teardown.
METHODS: flight_calc_test_lh FOR TESTING.
METHODS: flight_calc_test_ua FOR TESTING.
ENDCLASS.

CLASS ltc_flight_calc IMPLEMENTATION.
METHOD setup.
f_cut = ltd_flight_calc=>create_double( ).
ENDMETHOD.
METHOD flight_calc_test_lh.
DATA: lt_scarr TYPE scarr_tab,
lt_spfli_calc TYPE spfli_tab.
f_cut->set_carrid( iv_carrid = ‘LH’ ).
f_cut->flight_calc(
                                         EXPORTING
                                               it_scarr = lt_scarr
                                         IMPORTING
                                                et_spfli_calc = lt_spfli_calc ).

cl_abap_unit_assert=>assert_equals(
                                                                            EXPORTING
                                                                              act = lines( lt_spfli_calc )
                                                                              exp = 4
                                                                              msg = ‘Must be 4 records’ ).

ENDMETHOD.

METHOD flight_calc_test_ua.
DATA: lt_scarr TYPE scarr_tab,
lt_spfli_calc TYPE spfli_tab.
f_cut->set_carrid( iv_carrid = ‘UA’ ).
f_cut->flight_calc(
                                       EXPORTING
                                          it_scarr = lt_scarr
                                       IMPORTING
                                         et_spfli_calc = lt_spfli_calc ).

cl_abap_unit_assert=>assert_equals(
                                                                         EXPORTING
                                                                        act = lines( lt_spfli_calc )
                                                                         exp = 2
                                                                         msg = ‘Must be 2 records’ ).

ENDMETHOD.
METHOD teardown.
FREE f_cut.
ENDMETHOD.
ENDCLASS.


Here we have two test classes one is called test double class and second one the test class with test methods. 

Put debug points and execute the unit test.

24


Here we are in the test class ltc_flight_calc, calling,  execute method FLIGHT_CALC step by step.

25

Here we are trying to call the method get_scarr, execute (F5).

26

It doesn’t call the get_scarr method of the class ZCL_FLIGHT_CALCULATION but instead it call the redefinition method get_scarr in the test double class ltd_flight_calc.

27

28

29

30

Both the test methods are executed successfully.


 

One comment

Leave a Reply