Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Unit testing

Former Member
0 Kudos

hi

can you plz explain me about Unit testing?

thanks in advance

bramara

2 REPLIES 2

Former Member
0 Kudos

ABAP Unit Tests enable you to write test cases for certain Methods, to check their behaviour and displaying a appropriate protocoll.

These test cases are implemented using usual ABAP classes / methods and can be executed via menü in SE24: Class -> Unit Test

0 Kudos

Here is an example of a local class and a local unit test implemented in the same report. Pressing Ctrl+F10 in ABAP editor will execute the Unit Test:


************************************************************************
* CLASS LCL_TO_BE_TESTED DEFINITION                                    *
************************************************************************
* Definition of local Class LCL_TO_BE_TESTED to be tested using ABAP   *
* Unit Tests                                                           *
************************************************************************
CLASS lcl_to_be_tested DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      return_a
        RETURNING
          value(return) TYPE string.
ENDCLASS. "CLASS lcl_to_be_tested DEFINITION


************************************************************************
* CLASS LCL_TO_BE_TESTED IMPLEMENTATION                                *
************************************************************************
* Implementation of local Class LCL_TO_BE_TESTED to be tested using    *
* ABAP Unit Tests                                                      *
************************************************************************
CLASS lcl_to_be_tested IMPLEMENTATION.
  METHOD return_a.
    return = 'A'.
  ENDMETHOD. "METHOD return_a
ENDCLASS. "CLASS lcl_to_be_tested IMPLEMENTATION




************************************************************************
* CLASS LCL_TEST DEFINITION                                            *
************************************************************************
* Definition of local Class LCL_TEST, which implements the ABAP Unit   *
* Test for local Class LCL_TO_BE_TESTED                                *
************************************************************************
CLASS lcl_test DEFINITION FOR TESTING.
  PUBLIC SECTION.
    CLASS-DATA:
      c_ts_start    TYPE i READ-ONLY,
      c_ts_end      TYPE i READ-ONLY,
      c_dur         TYPE i READ-ONLY,   "duration of all test methods
      r_runtime     TYPE REF TO if_abap_runtime READ-ONLY,
      c_no_of_tests TYPE i READ-ONLY.   "number of test methods executed

    DATA:
      m_ts_start    TYPE i READ-ONLY,
      m_ts_end      TYPE i READ-ONLY,
      m_dur         TYPE i READ-ONLY.   "duration of test method


  PROTECTED SECTION.


  PRIVATE SECTION.
    CLASS-METHODS:
      class_setup,
      class_teardown.

    METHODS:
      test__return_a FOR TESTING,
      test__return_b FOR TESTING,
      setup FOR TESTING,
      teardown FOR TESTING.
ENDCLASS. "CLASS lcl_test DEFINITION FOR TESTING


************************************************************************
* CLASS LCL_TEST IMPLEMENTATION                                        *
************************************************************************
* Implementation of local Class LCL_TEST, which implements the ABAP    *
* Unit Test for local Class LCL_TO_BE_TESTED                           *
************************************************************************
CLASS lcl_test IMPLEMENTATION.

* **********************************************************************
* * METHOD TEST__RETURN_A                                              *
* **********************************************************************
* * 1st Test-Method to check, whether static Method RETURN_A of local  *
* * Class LCL_TO_BE_TESTED returns the Value 'a'                       *
* **********************************************************************
  METHOD test__return_a.
    DATA:
      lv_return TYPE string.

    lv_return = lcl_to_be_tested=>return_a( ).

    cl_aunit_assert=>assert_equals( act = lv_return
                                    exp = 'a' ).
  ENDMETHOD. "METHOD test__return_a


* **********************************************************************
* * METHOD TEST__RETURN_B                                              *
* **********************************************************************
* * 2nd Test-Method to check, whether static Method RETURN_A of local  *
* * Class LCL_TO_BE_TESTED returns the Value 'A'                       *
* **********************************************************************
  METHOD test__return_b.
    DATA:
      lv_return TYPE string.

    lv_return = lcl_to_be_tested=>return_a( ).

    cl_aunit_assert=>assert_equals( act = lv_return
                                    exp = 'A' ).
  ENDMETHOD. "METHOD test__return_b


* **********************************************************************
* * METHOD CLASS_SETUP                                                 *
* **********************************************************************
* * Method used by ABAP Unit Test to start a Unit Test. Executed only  *
* * once upon Start of an ABAP Unit Test.                              *
* **********************************************************************
  METHOD class_setup.
*   initialize Runtime Timer -> LCL_TEST=>R_RUNTIME
    lcl_test=>r_runtime = cl_abap_runtime=>create_hr_timer( ).

*   get 1st runtime timestamp (Class) -> LCL_TEST=>C_TS_START
    lcl_test=>c_ts_start = lcl_test=>r_runtime->get_runtime( ).
  ENDMETHOD. "METHOD class_setup


* **********************************************************************
* * METHOD CLASS_TEARDOWN                                              *
* **********************************************************************
* * Method used by ABAP Unit Test to end a Unit Test. Executed only    *
* * once upon End of an ABAP Unit Test.                                *
* **********************************************************************
  METHOD class_teardown.
*   get 2st runtime timestamp (Class) -> LCL_TEST=>C_TS_END
    lcl_test=>c_ts_end = lcl_test=>r_runtime->get_runtime( ).

*   calculate overall runtime of ABAP Unit Test
*   -> LCL_TEST=>C_DUR
    lcl_test=>c_dur = c_ts_end - c_ts_start.
  ENDMETHOD. "METHOD class_teardown


* **********************************************************************
* * METHOD SETUP                                                       *
* **********************************************************************
* * Method used by ABAP Unit Test to initialize the Execution of a     *
* * Test Run (^= Execution of a Test-Method).                          *
* **********************************************************************
  METHOD setup.
*   count number of test methods executed during ABAP Unit Test
*   -> LCL_TEST=>C_NO_OF_TESTS
    ADD 1 TO lcl_test=>c_no_of_tests.

*   get 1st timestamp (Test-Method) -> ME->M_TS_START
    me->m_ts_start = lcl_test=>r_runtime->get_runtime( ).
  ENDMETHOD. "METHOD setup


* **********************************************************************
* * METHOD TEARDOWN                                                    *
* **********************************************************************
* * Method used by ABAP Unit Test to conclude the Execution of a Test- *
* * Run (^= Execution of a Test-Method).                               *
* **********************************************************************
  METHOD teardown.
*   get 2nd timestamp (Test-Method) -> ME->M_TS_END
    me->m_ts_end = lcl_test=>r_runtime->get_runtime( ).

*   calculate duration of Test-Method -> ME->M_DUR
    me->m_dur = m_ts_end - m_ts_start.
  ENDMETHOD. "METHOD teardown
ENDCLASS. "CLASS lcl_test IMPLEMENTATION