ABAP Unit Test Fixture Methods

By | May 1, 2013 | ABAP Objects, ABAP Unit Test | 15,795 | 0

In the series of ABAP Unit, lets check out the Test Fixture methods.

Preface

In this article, I would cover various different methods for test fixture. In this series, I’m planning for these articles:

  1. ABAP Unit Test Driven Development Basic Example
  2. ABAP Unit Test Driven Development Basics
  3. ABAP Unit Test Fixture methods
  4. ABAP Unit Test Global class usage
  5. ABAP Unit Test Wizard to generate Test class – This article
  6. ABAP Unit Test Real time example
  7. ABAP Unit Test Advantages

What is Test Fixture

Test fixture is the test configuration like test data or test objects. This data would be used within the test methods. Fixture would be executed before the actual test method gets executed. So when the test is getting performed, the test data or test objects setup in fixture method can be used.

In ABAP, the test fixture is achieved using these predefined methods. These method would be called automatically by ABAP framework if they exist in the test class.

  • SETUP – Instance method SETUP would be called before each test within the test class
  • TEARDOWN – In contrary to SETUP, instance method TEARDOWN would be called after each test within the test class
  • CLASS_SETUP – Similar to SETUP, static method CLASS_SETUP would be used to set up the data once before the first test in the class gets executed
  • CLASS_TEARDOWN – Like TEARDOWN, static method TEARDOWN would be used to clear the data once after the last test in the class gets executed

Method SETUP( )

Special method SETUP( ) is used to setup the common test data for the same test methods of the same test class or of the inherited test class. This method would be called before calling the test method by the ABAP Unit framework. So, basically it would be called as many times as many test methods are there in a single test class.

In this method, you should generally setup your test data which can be leveraged by various different test within the same test class. Simple example, would be to setup default value for a variable, or instantiate the object for Production code.

Method TEARDOWN( )

Special method TEARDOWN( ) should be used to clear down the test data which was used by the actual test. You should use this method to clear the test data and make sure they are ready to use by the next Test method. This method would be called after calling the test method by the ABAP Unit framework. Similar to SETUP, this method would be called as many times as many test methods are there in a single test class.

In this method, you should generally setup your test data which can be leveraged by various different test within the same test class. Simple example, would be to clear the product code objects or all attributes of the test class and make sure it is ready for next execution.

Method CLASS_SETUP( ) & CLASS_TEARDOWN( )

Method CLASS_SETUP( ) is a static method. Set up the test data and save it into some temporary variable. We can use these test data into the SETUP method. The purpose of CLASS_SETUP is to set up the same data like a configuration which would be used by all test methods but NONE of the test method would be modifying it.

Static method CLASS_TEARDOWN would to make sure you clear up all the data and related attributes used in the test class before leaving the class.

Demo Program

Check out this demo program to display the use of test fixture.

 
REPORT ZNP_AUNIT_FIXTURE_DEMO.
*&---------------------------------------------------------------------*
*& Purpose - ABAP Unit fixture methods demo
*& Author  - Naimesh Patel
*& URL     - http://zevolving.com/?p=2228
*&---------------------------------------------------------------------*
*
CLASS lcl_data DEFINITION.
  PUBLIC SECTION.
    TYPES:
      BEGIN OF ty_data,
        glacc TYPE char10,
        dmbtr TYPE bseg-dmbtr,
      END OF ty_data.
    TYPES: tt_data TYPE STANDARD TABLE OF ty_data.
    DATA: t_data TYPE tt_data.
    METHODS: get_sum_for_glacc IMPORTING iv_glacc TYPE char10
             RETURNING value(rv_amt) TYPE bseg-dmbtr.
 
ENDCLASS.                    "lcl_data DEFINITION
 
START-OF-SELECTION.
 
*
CLASS lcl_data IMPLEMENTATION.
  METHOD get_sum_for_glacc.
    DATA: ls_data LIKE LINE OF t_data.
    LOOP AT t_data INTO ls_data WHERE glacc = iv_glacc.
      rv_amt = rv_amt + ls_data-dmbtr.
    ENDLOOP.
  ENDMETHOD.                    "get_sum_for_glacc
ENDCLASS.                    "lcl_data IMPLEMENTATION
*
CLASS lcl_test_collect DEFINITION FOR TESTING
  "#AU Risk_Level Harmless
  "#AU Duration   Short
.
  PRIVATE SECTION.
    DATA: o_cut TYPE REF TO lcl_data.
    METHODS:
      setup,
      teardown,
      test_valid_gl   FOR TESTING,
      test_invalid_gl FOR TESTING.
ENDCLASS.                    "lcl_test_collect DEFINITION
*
CLASS lcl_test_collect IMPLEMENTATION.
  METHOD setup.
    DATA: ls_data LIKE LINE OF o_cut->t_data.
    CREATE OBJECT o_cut.
    ls_data-glacc = '101'. ls_data-dmbtr = '20'.
    APPEND ls_data TO o_cut->t_data.
    ls_data-glacc = '101'. ls_data-dmbtr = '30'.
    APPEND ls_data TO o_cut->t_data.
    ls_data-glacc = '102'. ls_data-dmbtr = '40'.
    APPEND ls_data TO o_cut->t_data.
    ls_data-glacc = '101'. ls_data-dmbtr = '50'.
    APPEND ls_data TO o_cut->t_data.
  ENDMETHOD.                    "setup
  METHOD teardown.
    CLEAR o_cut.
  ENDMETHOD.                    "teardown
  METHOD test_valid_gl.
    DATA: lv_result_amt TYPE bseg-dmbtr.
    lv_result_amt = o_cut->get_sum_for_glacc( '101' ).
    cl_aunit_assert=>assert_equals(
        exp                  = 100
        act                  = lv_result_amt
        msg                  = 'Total is incorrect'
           ).
  ENDMETHOD.                    "test_valid_gl
  METHOD test_invalid_gl.
    DATA: lv_result_amt TYPE bseg-dmbtr.
    lv_result_amt = o_cut->get_sum_for_glacc( '999' ).
    cl_aunit_assert=>assert_equals(
        exp                  = 0
        act                  = lv_result_amt
        msg                  = 'Total is incorrect'
           ).
 
  ENDMETHOD.                    "test_invalid_gl
ENDCLASS.                    "lcl_test_collect IMPLEMENTATION
 

AS rule of TDD you start with empty production method and initial test.

After implementing the logic and re-factoring the test, you would notice your test is getting successful.

RS_AU_SAMPL_FIXTURE_PATTERNS

Since ABAP release 731, you have the report RS_AU_SAMPL_FIXTURE_PATTERNS to show different usage. The program is also well commented πŸ™‚

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.