ABAP Unit Test Global class usage

By | May 13, 2013 | ABAP Objects, ABAP Unit Test | 14,393 | 2

Since ABAP release 7.0, you can create ABAP Unit Test Global Class. Lets see what to do and what not to when using the ABAP Unit Global Test Class.

Preface

ABAP Unit testing was introduced as part of the ABAP Release 6.40. The global test classes were introduced in the next release i.e. ABAP Release 7.0. Lets talk about the ABAP Unit Global test class. Before we jump in, check out the already posted (and in pipeline) 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

Global Unit Test Class

When you create a class in SE24 or in SE80, you can specify the category of the class. Here you have an option to make any class as Global Unit Test class by selecting Test Class (ABAP Unit).

ABAP Unit Global Class Definition

Once the class is created, you can specify the Runtime Limit and Risk Level.

You might have noticed that I have created class as Abstract Instantiation. Before we talk about that, lets see different type of usage of Global Test class.

Helper Class

These class would contain the methods which would be used or leveraged in building up part of test fixture. Although, these type of class would be tagged with FOR TESTING, you may not create any test method as such. This class may also not contain any full test fixture method, but it would have a smaller helper methods to be part of fixture.

Parent Unit Test

This type of class may contain at least one test method. It may also contain a fixture method. You can inherit your local test class from this global class. You can create a Global Unit Test class with few test methods and reuse that while defining the local test class. Since, you wont be able to instantiate the object as well as you don’t want to have lot of programs using the same class, you should create the class as Abstract class.

Demo

In his Demo, I have the the logic to build a “packet” from a bigger dataset.

Global unit test class ZCL_DEMO_AUT

 
class ZCL_DEMO_AUT definition
  public
  abstract
  create public
  for testing
  "#AU Duration Short
  "#AU Risk_Level Harmless
 .
 
*"* public components of class ZCL_DEMO_AUT
*"* do not include other source files here!!!
public section.
 
*"* protected components of class ZCL_DEMO_AUT
*"* do not include other source files here!!!
protected section.
  data O_CUT type ref to ZCL_DEMO_PACKET .
  methods BUILD_ITAB_ALL .
 

Method BUILD_ITAB_ALL implementation

 
METHOD build_itab_all.
 
  DATA: lv_string TYPE string.
  DO 10 TIMES.
    lv_string = sy-index.
    CONCATENATE 'Line -' lv_string INTO lv_string.
    APPEND lv_string TO o_cut->t_data.
  ENDDO.
 
ENDMETHOD.
 

Class Under test – production class

 
class ZCL_DEMO_PACKET definition
  public
  final
  create public .
 
*"* public components of class ZCL_DEMO_PACKET
*"* do not include other source files here!!!
public section.
 
  data V_PACKET_SIZE type I .
  data V_NUMBER_OF_PACKET type I .
  data T_DATA type TREXT_STRING .
 
  methods MAKE_PACKET
    returning
      value(RT_PACKET) type TREXT_STRING .
 

Local class definition inheriting from ZCL_DEMO_AUT

 
*"* use this source file for any type declarations (class
*"* definitions, interfaces or data types) you need for method
*"* implementation or private method's signature
 
class lcl_test_packet DEFINITION
  INHERITING FROM zcl_demo_Aut
  for TESTING
  "#AU Duration Short
  "#AU Risk_Level Harmless
 .
 
  PRIVATE SECTION.
    METHODS: setup,
             test_packet_2 FOR TESTING.
 
ENDCLASS.
 

Local test class implementation

 
*"* local class implementation for public class
*"* use this source file for the implementation part of
*"* local helper classes
 
CLASS lcl_test_packet IMPLEMENTATION.
  METHOD setup.
    CREATE OBJECT me->o_cut.
    me->build_itab_all( ).
    me->o_cut->v_packet_size = 3.
    me->o_cut->v_number_of_packet = 2.
  ENDMETHOD.                    "setup
  METHOD test_packet_2.
 
    DATA: lt_result_itab TYPE trext_string.
    DATA: lt_exp_itab TYPE trext_string.
 
    APPEND LINES OF o_cut->t_data FROM 4 TO 6 TO lt_exp_itab.
 
    lt_result_itab = o_cut->make_packet( ).
    cl_aunit_assert=>assert_equals(
        exp                  = lt_exp_itab
        act                  = lt_result_itab
        msg                  = 'Unable to create proper packet'
           ).
 
  ENDMETHOD.                    "test_packet_2
ENDCLASS.                    "lcl_test_packet IMPLEMENTATION
 

With empty implementation in MAKE_PACKET( ), it the test would fail – as expected doesn’t match with actual result. So, add the implementation to create a packet.

 
METHOD make_packet.
 
  DATA: lv_from TYPE i.
  DATA: lv_to TYPE i.
 
  lv_to = v_number_of_packet * v_packet_size.
  lv_from = lv_to - v_packet_size + 1.
 
  APPEND LINES OF t_data
    FROM lv_from TO lv_to
    TO rt_packet.
 
ENDMETHOD.
 

As soon as you add the logic and run again, you would see that your test passes !!!

Few Guidelines on using ABAP Global Test class

  • Refrain of using the Global test class as Parent Test class. This would get executed in all the test classes where ever the local class is inheriting the class. It may be unnecessary.
  • Make your helper class as Test class as well by adding the FOR TESTING as you don’t want it to run in production
  • Use test class to build up smaller chunk of the text fixtures which can be reused in your various test cases
  • All instance methods are by default for testing. So, make sure you remove it for testing as you don’t want to create test methods in the Global test class

Do you have any guidelines to add?

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

2 Comments

  • Ravi

    Hi Naimesh,

    Nice blog on Global test classes.

    I have a doubt in ABAP Unit, most of the examples I have seen, are based on some arithmetic operation. My question is in case we expect an internal table as a result of a module, how do we test the same?

    Further, how do we ensure the data returned from a module is valid? for example, manager of an employee, this data might be changed overtime.

    Regards,
    Ravi

  • Hello Ravi,

    I can understand your doubts about the ABAP Unit. In order to better understand the usage of ABAP Unit, we need to understand why the Unit testing is important.

    Since ABAP being a high level programming language, many of the basic logic is already hidden within the commands. Like PARAMETERS would define a 1000 screen with PBO, PAI logic etc. In compare to other languages, you would need to code for it.

    Majority of developers in ABAP are writing the business relevant logic. This logic would than end up in some kind of output – ALV or Form or IDOC. So, where would be an opportunity to introduce ABAP Unit. You can design all your programs in such a way that you have smaller units of the code to work with. Once you have smaller units, you would see the usage of ABAP Unit more clearly.

    Like If you have logic to format the Numbers. If it is negative, it should be -123 instead of 123-. Instead of having this logic repeating for each field, you write a method to give you back the value. Without ABAP unit, you would be putting a breakpoint, debugging the logic through to check if it works as expected. With using TDD and ABAP Unit, you would write a test first – Setting the expected value and write a logic in the method. Run the Unit Test over and over, till the logic is as expected.

    I would try to cover the real time examples where I have used ABAP Unit.

    Thanks,
    Naimesh Patel

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.