ABAP Guide: Write ABAP Unit Tests

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Checked on 2026-11-13 against open-abap 2.14.30 (the abaplint transpiler and runtime, open-abap-core a9acc41), abaplint 2.120.103 and Node.js 24.21.

What you build

You write a small class named zut_claim_limit that decides whether an expense claim is within a spending limit of 1000, and a local test class that checks it with four tests. The first run of the tests fails on purpose: the class compares with < where the rule needs <=, so a claim of exactly 1000 is rejected. You read the failure, find the line, fix it and run the tests again until all of them pass. The finished state is the pair of files that the ABAP lab keeps, so the lab’s own npm test passes.

The playground is open-abap, a community open-source project, not SAP software. It runs ABAP Unit tests with its own copy of the class cl_abap_unit_assert, and it prints a failure differently from an SAP system: where ABAP Development Tools show a list of tests with a red marker, the playground prints a JavaScript error and ends with exit code 1. The rules for writing tests are the same.

Terminal with npm test listing the greeter test and the four claim tests, followed by echo $? printing 0

Before you start

  • No account is needed: Node.js, the abaplint tools and open-abap install from the public npm registry without signing in. The licences you accept by installing (MIT for the abaplint packages) are listed in the README of the ABAP lab.

  • Set up the lab first, with the guide Set Up a Local ABAP Playground: the folder abap-lab with npm install done and an editor such as Visual Studio Code open on it. The guide ABAP Guide: Your First ABAP Program explains the two-file layout of an object (.abap and .xml) and how a transpile error looks; it is worth reading first.

  • The lab already holds the finished files of this guide in modules/ROOT/examples/abap-lab/src/. Type them yourself rather than copying them, so that you meet the failure on your own screen.

  • Read ABAP Unit and Test Doubles for the ideas behind this guide and Classes and Objects for classes and methods. This guide needs only the basics: a unit test is a small program that calls your code with input you choose and checks the result.

  • The commands npm test and echo $? run in a Unix-style shell such as bash or zsh. The figures are drawings of the editor and the terminal, not screenshots. A highlighted box with a number marks the file, line or output of the step with the same number.

Steps

A test of a global class lives in the same development object as the class. In the abapGit layout of the lab that is a third file next to <name>.clas.abap and <name>.clas.xml: <name>.clas.testclasses.abap. On an SAP system the same file is the tab Test Classes of the class in ABAP Development Tools. The source of a test class is not part of the production code, production code cannot address it, and it is not generated in production systems.

Write the class under test

  1. In the src folder, create src/zut_claim_limit.clas.abap. The class has a constant c_limit with the value 1000 and two methods: is_within_limit answers abap_true or abap_false, and status_for turns that answer into the text Approved or Rejected. The listing shows the finished file. For now write line 16 with a slip that looks harmless: result = xsdbool( amount < c_limit )., with < where the listing has <=. The tests will find it.

    CLASS zut_claim_limit DEFINITION PUBLIC FINAL CREATE PUBLIC.
      PUBLIC SECTION.
        CONSTANTS c_limit TYPE i VALUE 1000.
    
        METHODS is_within_limit
          IMPORTING amount        TYPE i
          RETURNING VALUE(result) TYPE abap_bool.
    
        METHODS status_for
          IMPORTING amount        TYPE i
          RETURNING VALUE(result) TYPE string.
    ENDCLASS.
    
    CLASS zut_claim_limit IMPLEMENTATION.
      METHOD is_within_limit.
        result = xsdbool( amount <= c_limit ).
      ENDMETHOD.
    
      METHOD status_for.
        IF is_within_limit( amount ) = abap_true.
          result = `Approved`.
        ELSE.
          result = `Rejected`.
        ENDIF.
      ENDMETHOD.
    ENDCLASS.
  2. Create the attribute file src/zut_claim_limit.clas.xml. It has the same layout as the attribute file of the greeter class, with the class name and the description changed, and it includes the line CLSCCINCL that the greeter class also has because it also has a test file.

    <?xml version="1.0" encoding="utf-8"?>
    <abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
     <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
       <VSEOCLASS>
        <CLSNAME>ZUT_CLAIM_LIMIT</CLSNAME>
        <LANGU>E</LANGU>
        <DESCRIPT>Claim limit</DESCRIPT>
        <STATE>1</STATE>
        <CLSCCINCL>X</CLSCCINCL>
        <FIXPT>X</FIXPT>
        <UNICODE>X</UNICODE>
       </VSEOCLASS>
      </asx:values>
     </asx:abap>
    </abapGit>
    Visual Studio Code with the file src/zut_claim_limit.clas.abap open: the class zut_claim_limit with the constant c_limit of 1000 and the methods is_within_limit and status_for, where is_within_limit compares with a less-than sign; callout 1 marks the class file and callout 2 marks the companion XML file in the explorer

Write the test class

  1. Create src/zut_claim_limit.clas.testclasses.abap. The addition FOR TESTING makes ltcl_limit a test class, and DURATION SHORT RISK LEVEL HARMLESS says that the test is quick and changes no data. Each method declared FOR TESTING is one test. The method setup is part of the fixture: it runs before every test method and gives each test a new object in the variable cut (class under test), so one test cannot disturb another. Inside a test, assert_true, assert_false and assert_equals of cl_abap_unit_assert compare the result with what you expect. The optional parameter msg is the text printed when a check fails, and act and exp hold the actual and the expected value. A test that raises no failure passes.

    CLASS ltcl_limit DEFINITION FINAL FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
      PRIVATE SECTION.
        DATA cut TYPE REF TO zut_claim_limit.
    
        METHODS setup.
        METHODS small_amount_is_accepted FOR TESTING.
        METHODS large_amount_is_rejected FOR TESTING.
        METHODS amount_at_limit_is_accepted FOR TESTING.
        METHODS status_text_follows_limit FOR TESTING.
    ENDCLASS.
    
    CLASS ltcl_limit IMPLEMENTATION.
      METHOD setup.
        cut = NEW #( ).
      ENDMETHOD.
    
      METHOD small_amount_is_accepted.
        cl_abap_unit_assert=>assert_true( cut->is_within_limit( 500 ) ).
      ENDMETHOD.
    
      METHOD large_amount_is_rejected.
        cl_abap_unit_assert=>assert_false( cut->is_within_limit( 1500 ) ).
      ENDMETHOD.
    
      METHOD amount_at_limit_is_accepted.
        cl_abap_unit_assert=>assert_true(
          act = cut->is_within_limit( zut_claim_limit=>c_limit )
          msg = `A claim of exactly the limit must be accepted` ).
      ENDMETHOD.
    
      METHOD status_text_follows_limit.
        cl_abap_unit_assert=>assert_equals(
          act = cut->status_for( 500 )
          exp = `Approved` ).
        cl_abap_unit_assert=>assert_equals(
          act = cut->status_for( 1500 )
          exp = `Rejected` ).
      ENDMETHOD.
    ENDCLASS.
    Visual Studio Code with the file src/zut_claim_limit.clas.testclasses.abap open: the local test class ltcl_limit FOR TESTING with a setup method and four test methods that call assert_true, assert_false and assert_equals of cl_abap_unit_assert; callout 3 marks the test class file in the explorer

Run the tests and read the failure

  1. In the terminal, in the abap-lab folder, run the script that transpiles all sources and then runs every test:

    npm test
  2. Look at the lines that start with a class name, such as ZUT_CLAIM_LIMIT: running ltcl_limit->small_amount_is_accepted. The runner prints one line when a test starts, so the last of these lines is the test that failed: here amount_at_limit_is_accepted. The two tests before it passed, and the test status_text_follows_limit never started, because the run stops at the first failure. The lab’s greeter test runs first.

  3. Read the block that follows. It begins with kernel_cx_assert [Error], the exception the assertion raised, and a stack of JavaScript lines that you can ignore. Further down, msg holds the text that you gave the assertion: A claim of exactly the limit must be accepted. That sentence is why a message is worth writing: it tells you which rule is broken without opening the test. The block is long, and the figure leaves out the lines marked with three dots and the lines that name files on the computer that ran it.

    Terminal after npm test: the transpiler reports the output files it wrote (one example run), three test lines appear for the greeter and two claim tests and a fourth for amount_at_limit_is_accepted, then an error of type kernel_cx_assert whose msg value reads A claim of exactly the limit must be accepted; callout 4 marks the command, callout 5 the last test line that started and callout 6 the message
  4. Ask the shell for the exit code of the run that just ended. A program reports success or failure to the shell as a number: 0 means success and anything else means failure. The failed run ended with 1. A build server uses this number to decide that a change must not be released.

    echo $?
    Terminal right after the failed npm test: the command echo $? prints 1; callout 7 marks the printed exit code

Fix the class and run again

  1. Open src/zut_claim_limit.clas.abap and change line 16 to the form of the listing in step 1: < becomes <=. The rule says a claim up to and including the limit is accepted, and the test amount_at_limit_is_accepted was the one that checked exactly that boundary. Save the file.

    Visual Studio Code with the file src/zut_claim_limit.clas.abap open after the fix: the line in is_within_limit now reads result = xsdbool( amount ⇐ c_limit ); callout 8 marks the changed line
  2. Run npm test again, then echo $?. All five test lines appear (the greeter test and your four), no error follows, and the exit code is 0.

Check the result

npm test prints the Output files line and then five lines that start with ZCL_GREETER: running or ZUT_CLAIM_LIMIT: running, with no kernel_cx_assert after them, and echo $? prints 0. npm run lint prints abaplint: 0 issue(s) found followed by the number of files analyzed, which grows as the lab does.

Variations

  • In the test status_text_follows_limit, change Rejected to Declined and run npm test. The test now fails with actual holding Rejected and expected holding Declined, the two values that assert_equals compared. Change it back.

  • Add a fifth test method for an amount of 0 and decide first what you expect; then run the tests.

  • Give each assertion a msg, as the boundary test does, and compare how a failure reads with and without it.

What can go wrong

Symptom Cause Fix

A block starting with kernel_cx_assert [Error] and exit code 1 after npm test

An assertion failed. The last running line before the block names the test.

Read msg (and actual and expected for assert_equals), open the named test and fix the code or the expectation.

A test you wrote has no running line

An earlier test failed first. The run stops at the first failure.

Fix the failing test; the later tests then start.

Error: check_syntax, Method "is_within_limt" not found, methodCallChain, zut_claim_limit.clas.testclasses.abap:18 and no test runs

A method name in the test class is misspelled, so the transpile stops before anything runs.

Open the file and line at the end of the message and correct the name. npm run lint reports the same finding with the column.

All tests pass although you broke the code

You ran node output/index.mjs yourself after a failed transpile. Node.js ran the last successful build. In the lab this printed the five test lines and exit code 0 while the transpile had failed.

Run npm test: it runs the tests only when the transpile succeeded.

Where to go next