Standard service - a fixed amount of work that is performed at a fixed price.  Post Service


Saturday, 18 May 2019 16:27

ABAP Units – Test a Little, Implement a Little, Reflect a Little

Written by
Rate this item
(0 votes)


ABAP Units is an important concept from the perspective of testing which allows us as the developer to test each piece of code as an independent entity

with different test scenarios, cases efficiently which makes sure that all grounds for testing is covered and there would be no issues when code is moved in production.

1.    Test Driven Development (TDD)   

Figure 1: Architecture of Test Driven Development

The motto is ‘Test a little, implement a little, reflect a little,” or ‘Red, Green, Refactor.’ In TDD, we write a test method and run it. The test method fails, because the code under test is not implemented. We implement the code under test such that the test is green and repeat the cycle, until the method under test and eventually the entire class are fully developed and also fully testable.

ABAP Units is a part of Test Driven Development Approach. In this approach the functional is meant to provide all valid and invalid test cases or in short has to provide all test-cases before hand and then the actual code for production is to be written.

Features of ABAP Unit

ABAP Unit is part of the test landscape for ABAP programs. ABAP Unit allows us to implement unit tests and to execute them both manually and automatically in development server. ABAP Unit tests are methods of specially designated ABAP classes. ABAP Unit is suitable for test-driven development.

ABAP Unit is directly embedded into the ABAP development environment and into the ABAP runtime environment. In ABAP Unit tests are, test methods in dedicated test classes. A test class may contain several test methods. Usually test classes are local classes within the program under tests.

The most important features of ABAP Units for programming are:

  • The tests are programmed in ABAP. We need not have to learn any additional test script languages.
  • The tests are developed in the ABAP development environment. We need not have to learn any additional interface operation.
  • With the ABAP Unit Wizard, we can generate Test Classes for classes (Class Pools) and Function Groups. For other program objects, we can create test classes manually.
  • ABAP Unit test classes can be implemented in the tested development objects. This ensures that the relationship between unit test and tested code is clear. And since unit tests are transported with the code under test, they are available in all systems of the development and test landscape.
  • Anyone can execute ABAP unit tests. Since ABAP Unit Tests do not have parameters, you do not have to have any expert knowledge to start unit tests, even if you are not familiar with the ones you are running. .
  • The results display makes it easy to evaluate and analyse unit test errors.
  • ABAP Unit integrates ABAP’s capabilities for measuring code coverage. You can validate your ABAP Unit tests by checking their code coverage. You can use the code coverage displays to find out where there are gaps in your ABAP Unit tests – code that the tests do not reach.
  • ABAP Unit tests are not executable in productive clients and can therefore cause neither workload on the system nor problems as a result of side-effects. In fact, ABAP does not generate byte code for ABAP Unit test classes and methods in production systems.

Evaluating Test result using ASSERT Class

Class CL_ABAP_UNIT_ASSERT is used for the evaluation of the results for the object. The class CL_ABAP_UNIT_ASSERT contains the following static methods for the verification of test expectations within ABAP Unit test methods:


Ensure the equality of two data objects. Please note that the arguments are passed by reference. Objects are tested for identity only.


Ensure the equality of two decimal floats with a relative tolerance.


Ensure the value of two data objects not to be equal.


Ensure whether the reference of a reference variable is valid.


Ensure whether the reference of a reference variable is invalid.


Ensure that a data object has its initial value.


Ensure that a data object does not have its initial value.


Requests specific value of SY-SUBRC.


Ensure that an internal table contains a certain line.

All the assert methods have the following mandatory importing parameter:

ACT (Type: Any) – The Actual object to be verified

EXP (Type: Any) – The Expected object to be verified

MSG (Type: CSEQUENCE) – Contains a more detailed description of the error (if applicable)


Calling ABAP Unit Assert Equals method

A simple example for this approach can be validation of material (matnr) and plant(werks) combination. In this case the functional has to before hand provide all valid combinations of material and plant combination for which code should be triggered. All this combinations can be tested before hand itself using this concept and that to independently.

Implementation Example of above case.

Definition of class containing logic which does validation for plant and material combination.

Implementing class fetching data from marc table

Class definition which would be used for implementing ABAP UNITS

Key points to be noted here: a) Key word Testing b) Risk level & Duration

Implementing class for ABAP UNITS

In the above class / snapshot we have provide thepossible combination which should be present and code should work.


Case 1) Invalid Test Case

As can be seen in above snapshot, using the method asserts_equal of  class cl_aunit_assert it gives detail explanation of where in table value is not matching including the index of the table.

Case 2 : Valid Test Case

Displays test executed successfully, all required values are present.

Key Points:

a) Key word Testing

Whenever FOR TESTING follows after the class definition, it signifies that it is a class for abap units and it won’t get executed in production environment.



b) Risk level  & Duration

Risk levelis maintained in client settings and it would define the level of risk which ispresent in executing the test.

There are 3 levels of risk: 1) Harmless 2 ) Dangerous 3) Critical

Duration is the required time for execution of test case.

Types: 1) Short 2) Medium 3) Long

Also Read: QuickViews for Functions in S/4HANA

Use of Transaction SECATT in Abap Units.

SECATT is a t-code used for creating scripts which can be used for automating repetitive process. In this t-code the user can create a script which can contain all the material and plant combinations which is used in example above which is then used as test data for our scenario.

Use of ABAP Units using Global Classes:

Abap Units can also be used with global classes for carrying out testing of the production code.

The processremains similar which is as follows:

  • We first create aglobal class with methods which would contain the required production codes.
  • Once the globalclass is created we can create a testing method in the class which wouldcontain a local class to carry out the execution for testing the methods ofclass which is similar to concept of local classes.

Different Methods used in ABAP units:

Methods in ABAP units are referred to as Fixture Methods.

  • Setup Method: It is similar to initialization event of SAP. It’s an instance method which is called before execution of each test fixture method in Abap Units.
  • Tear Down: It’s a method which is called after execution of the test fixture methods, which can be used to clear or refresh variables.
  • Class Setup: The concept of this method is equivalent to class constructor concept in SAP. This method is executed when the abap units classes are loaded for the first time in memory
  • Class Teardown: The concept of this method can be roughly compared to end-of-selection event in SAP ABAP. This method is executed when the last test case is executed in the class.

Test Data Container (TDC):

It’s a container which is created using tcode- SECATT which contains set of test data stored in variants which are used for testing the production code.

Example of Global Class using SECATT transaction:

Maintain Data for testing (Done by Functional)

Also Check – Free Video Course on HANA Models/Views

Create a parameter and parameter reference would be the Table Type containing the fields for validation; in our case matnr and werks.

Create a variant and then double click on the value tab where functional would create the test data.

Add the required matnr and werks combination and savethe required data.

Create a global class containing the fetch_data method which contains the required logic which fetches matnr and werks combinations.

Also Take – End to End Debugging Course for SAP Functionals

In ABAP workbench open the class and by right clicking, one can create new local test class by following the required instruction and selecting the required test method along-with the method for which test method needs to be created

In the local class using the class cl_apl_ecatt_tdc_api we can get the instance of are Test Data Container (TDC).

Using the instance we can get the variants saved forthe required test of method which provides the test data.

We then call the fetch_data method of global class which contains the data fetched from production code which is checked against variant expected data.

If there is difference in data, we get the following output.

ABAPers, we understand ABAP Unit Test is relatively newconcept used very sparingly. But if you start using it more frequently it isjust matter of time when you would start loving ABAP Units and whole ABAPcommunity would shift from traditional testing to this robust new ABAP Units.

If you need any help in implementing ABAP Unit Tests for your development, do let us know. We would surely suggest. You may put your questions in the comment section below or email us at This email address is being protected from spambots. You need JavaScript enabled to view it. or This email address is being protected from spambots. You need JavaScript enabled to view it..

If you want to have real time discussions and resolutions, do join our SAP Technical Telegram Group where we have more than 4885+ active SAP consultants from 6 Continents.

Please Note: You need to install Telegram App on your mobile first and then you can join the group using the above link.

Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.

Free SAP ABAP for HANA Training Tutorials Exercises.

Read 51 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.