Fundamentals of ABAP Unit Testing

A Beginners Guide to Implementing and Configuring ABAP Unit Tests in SAP Systems

Published April 23, 2023


Testing is a vital aspect of software development, ensuring the stability, reliability, and performance of your applications. In the SAP ecosystem, ABAP Unit testing is an underutilized but important approach for validating custom code.

In this blog post, we will explore the basics of testing in SAP, how to set up and configure ABAP Unit tests, and provide some examples to illustrate the different risk levels within the context of a "Hello World" program.

ABAP Unit Testing in SAP

To run ABAP Unit tests in SAP, you must work within a client that has the appropriate role assigned via the transaction code SCC4. Only clients with roles such as Customizing, Demo, Training/Education, and Test are suitable for running tests. Production and SAP Reference clients do not allow test execution to preserve system stability and data integrity.

Configuring ABAP Unit Tests

To configure your ABAP Unit tests, use the transaction code SAUNIT_CLIENT_SETUP. This transaction code offers several options for tailoring your tests:

  1. Prohibit the execution of tests: Prevent tests from running in certain clients.
  2. Limit the risk level: Define the acceptable risk levels for test execution - Harmless, Dangerous, or Critical.
  3. Define the duration of tests: You can set a time limit for test execution in seconds, ensuring that tests do not run indefinitely and consume valuable system resources.

Risk Levels Explained

  • CRITICAL: Tests that involve changes to system settings or Customizing
  • DANGEROUS: Tests that involve changes to persistent data
  • HARMLESS: Tests that have no effects on persistent data or system settings

A Harmless test could validate the output of a straightforward message display function. For instance, if a program is designed to display Hello World!, your test could confirm that the generated output string matches the anticipated value.

A Dangerous test could involve checking the creation of a temporary record in a database table, while a Critical test might assess the impact of a configuration change on the program's behavior.

Duration of Tests Explained

The duration of tests in ABAP Unit is an attribute that helps to categorize test cases based on the expected time they take to execute. When defining a test class for ABAP Unit tests, you can specify the expected duration using one of the following options:

  • SHORT: The test is expected to complete quickly, usually within seconds. These tests are often simple, with minimal overhead, and are designed to be executed frequently, such as during development or as part of a continuous integration process.
  • MEDIUM: The test is expected to take a moderate amount of time, possibly a few minutes. These tests may involve more complex logic, access to external resources, or a larger amount of data. They may be executed less frequently than short tests, such as during integration testing or before deploying changes to production.
  • LONG: The test is expected to take a significant amount of time to complete, possibly hours. These tests are typically used for complex scenarios, stress testing, or end-to-end system testing. Due to their duration, long tests are generally executed less frequently, such as during specific testing phases or at certain milestones in the project lifecycle.

By specifying the test duration, you help set expectations for test execution time, which can be useful when organizing and prioritizing tests. Additionally, it allows developers and testers to ensure that the testing process is efficient and that time-sensitive tests are executed promptly.

Example Implementation


* Local Class definition

               RETURNING VALUE(rv_result) TYPE STRING.

* Local Class implementation

    rv_result = 'Hello World!'.


* Local Test class definition

    DATA: mo_hello_world TYPE REF TO LCL_HELLO_WORLD.

    METHODS: setup,
             test_display_message FOR TESTING.

* Local Test class implementation

  METHOD setup.
    CREATE OBJECT mo_hello_world.

  METHOD test_display_message.
    DATA: lv_output TYPE STRING.

    lv_output = mo_hello_world->DISPLAY_MESSAGE( ).

      act = lv_output
      exp = 'Hello World!'
      msg = 'Unexpected output' ).


* Report execution

  CREATE OBJECT go_hello_world.
  WRITE: / go_hello_world->DISPLAY_MESSAGE( ).

In this ABAP code example, we have a simple program designed to display the message "Hello World!". The program consists of a local class LCL_HELLO_WORLD, which has a single method called DISPLAY_MESSAGE. This method returns the string "Hello World!" when called.

To ensure the program works correctly, we're using ABAP Unit testing. We create a local test class LTCL_HELLO_WORLD_TEST with a FOR TESTING statement, specifying the risk level as Harmless and the test duration as Short. This test class contains two methods: setup and test_display_message.

The setup method is used to initialize the test environment by creating an instance of the LCL_HELLO_WORLD class. The test_display_message method contains the actual test logic. It calls the DISPLAY_MESSAGE method of the LCL_HELLO_WORLD class and stores the output in a variable lv_output. Then, it uses cl_abap_unit_assert=>assert_equals to compare the actual output (lv_output) with the expected value ('Hello World!').

If the output matches the expected value, the test passes, indicating that the DISPLAY_MESSAGE method works as intended. If not, the test fails, signaling that there might be an issue with the program's implementation.

Step-by-Step Example

Let's walk through a possible real-world example. I encourage you to try this in a system if you have access to one.

Imagine a new ABAP dev, Ali, decides to edit this code and changes the class implementation method.

    rv_result = 'Hello World'.

Luckily, Ali knows the value of tests so he executes the ABAP Unit test in SAP GUI. Windows: Ctrl + Shift + F10 Mac OS: fn + Control + Shift + F10 The results show Ali a very helpful message. Expected "Hello World!" Actual "Hello World" This is because Ali removed the exclamation mark in the updated method. Failed Test

Ali realizes this is important and reverts his change. Naturally, he executes the ABAP Unit tests again. This time, the test passes. Passing Test

Even though this is a simple example, ABAP Unit testing is crucial for validating the functionality of your custom code in the SAP ecosystem. By writing and executing unit tests, you can ensure your code behaves as expected, allowing you to catch and fix any issues before they make their way into the production environment. This is particularly helpful if you are writing programs that need to work across multiple SAP versions. The ABAP Unit tests allow you to catch errors early and identify the exact part of the code that needs to be updated.

Want to code like the top 1% of elite SAP developers?

Elevate your skills with exclusive insights, expert tips, and cutting-edge best practices. Don't miss out – become part of the elite SAP community today!

Related Posts


TechEd 2022

A solid overview of parallel development in 25 minutes.


Handling SAP Cross-Version Compatibility

Manage only one codebase across SAP versions.


Why Isolated Development Environments?

How isolated development environments enabled fast, high quality development with good economics.