header_abap

Mastering Unit Testing in ABAP: a guide to software quality

13.02.2024
Reading time: 3 min

Unit testing is an indispensable tool for any software developer. It enables the verification of the functional correctness of software modules at the smallest unit level, such as procedures, classes or function groups. In the ABAP world, clean code is tested code. Our SAP HR Solution Developer Lev Lourie shows you the best way to proceed in his guest article.

What is a unit test?

A unit test is a mechanism that provides concepts and tools for testing. It is carried out using test classes, which are usually local classes within productive global classes. A test class is defined with the addition “FOR TESTING”. Test methods check the correctness of the tested code using assertions.

CLASS [EXAMPLE_TEST]  DEFINITION
  FINAL
  FOR TESTING
  DURATION SHORT
  RISK LEVEL HARMLESS.

PRIVATE SECTION.
 METHODS:
    [METHOD1_TEST]  FOR TESTING.
 ENDCLASS.

 CLASS [EXAMPLE_TEST] IMPLEMENTATION.
 METHOD METHOD1_TEST  .
     … 
    CL_ABAP_UNIT_ASSERT=>ASSERT_EQUALS(
      EXP = …
      ACT = …
      MSG = 'UNEXPECTED TEST VALUE’).
 ENDMETHOD.
 ENDCLASS.

Additional attributes in the class definition are:

Common functionality in unit tests

If several unit tests require the same functionality, they can have a common superclass that has test methods in the subclasses. If no test methods are to be inherited, the common functionality is provided in a test helper class with the addition “ABSTRACT”.

The “given-if-then” style

The implementation of the test method is constructed as a scenario in the “given-if-then” style. In the “given” part, the instances and data are prepared for the test. This phase is implemented in the Setup method. The operation to be tested is executed in the “if” part. The “then” part ensures that the desired result is achieved. Assertions are used in this step and if the result is not achieved, an error message should be displayed.

METHOD REJECTS_INVALID_COUNTRY.
" WHEN…
" THEN” 
  CL_ABAP_UNIT_ASSERT=>ASSERT_TABLE_CONTAINS(
    TABLE = MESSAGES
    LINE = …
    MSG = `EXPECTED ERROR MESSAGE NOT FOUND` ).
 ENDMETHOD.

TEST-SEAMS und TEST-INJECTION

Another test tool is “TEST-SEAMS”, which marks the code parts that are replaced by other code during a test. A TEST-INJECTION block is called up in the test code for this purpose. The code in the injection block replaces the code in the test seam.

First steps with unit tests in ABAP

Start by selecting the class you want to test. In the menu, navigate to Utilities -> Test classes -> Generate and follow the wizard to create the test class. Once this is done, you can start writing your first test for a method in your class. Call the method in your test and use the ASSERT_EQUALS method to compare the result obtained with the expected result. Finally, execute the test by clicking on the unit test icon in the toolbar.

Tested code is clean code

Unit testing in ABAP is a powerful tool that significantly improves the quality of software development. It allows developers to check the functionality of their code at the smallest unit level and ensure that their code works correctly. With the use of test classes, assertions, the “given-if-then” style, test seeds and test injections, ABAP offers a comprehensive and flexible test environment. By applying these practices, ABAP developers can ensure that their code is clean, maintainable, efficient and error-free, bringing them closer to the goal of clean code.

Unit testing is therefore an important instrument for quality assurance and at the same time considerably shortens the development time.

The author
Lev
Lourie
Lev has many years of experience in SAP programming in the areas of human resources and logistics as well as in adapting practical solutions to the needs of customers.
Contact
mmmake-ansprechpartner-lev-lourie