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.
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:
- ABAP Unit Test Driven Development Basic Example
- ABAP Unit Test Driven Development Basics
- ABAP Unit Test Fixture methods
- ABAP Unit Test Global class usage
- ABAP Unit Test Wizard to generate Test class – This article
- ABAP Unit Test Real time example
- 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).
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.
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.
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?