Skip to content

CL_ABAP_UNIT_ASSERT

The assertion class for ABAP Unit tests — provides static methods to verify expected values and fail with structured error messages.

Purpose

CL_ABAP_UNIT_ASSERT is used exclusively inside test classes (marked with FOR TESTING). It never needs instantiation — all methods are static. A failed assertion immediately fails the current test method and reports the failure in the unit test result panel.

Test classes must live in a global class or program — not a standalone PUBLIC class

You cannot paste a CLASS ... DEFINITION PUBLIC in SE38 and activate it — that syntax is for global classes created in SE24. In SE38 (a report/program), test classes must be defined without PUBLIC, like:

CLASS lcl_my_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
See the complete copy-paste example below.

Key methods

Method Checks
ASSERT_EQUALS( exp act ) exp equals act
ASSERT_NOT_EQUALS( exp act ) exp does not equal act
ASSERT_INITIAL( act ) act is initial (zero, empty, initial)
ASSERT_NOT_INITIAL( act ) act is not initial
ASSERT_BOUND( act ) Object reference is bound (not initial)
ASSERT_NOT_BOUND( act ) Object reference is not bound
ASSERT_TRUE( act ) act = abap_true
ASSERT_FALSE( act ) act = abap_false
FAIL( msg ) Unconditionally fail with message

All methods accept an optional msg parameter for the failure description.

Complete copy-paste example (SE38 report)

Create a new report in SE38 and paste this entire block — it contains a simple local class to test and the test class for it:

REPORT z_unit_test_demo.

" ---- Class under test (local class) ----
CLASS lcl_calculator DEFINITION.
  PUBLIC SECTION.
    METHODS add
      IMPORTING iv_a          TYPE i
                iv_b          TYPE i
      RETURNING VALUE(rv_sum) TYPE i.
    METHODS divide
      IMPORTING iv_a            TYPE i
                iv_b            TYPE i
      RETURNING VALUE(rv_result) TYPE i
      RAISING   cx_sy_zerodivide.
ENDCLASS.

CLASS lcl_calculator IMPLEMENTATION.
  METHOD add.
    rv_sum = iv_a + iv_b.
  ENDMETHOD.
  METHOD divide.
    IF iv_b = 0.
      RAISE EXCEPTION TYPE cx_sy_zerodivide.
    ENDIF.
    rv_result = iv_a / iv_b.
  ENDMETHOD.
ENDCLASS.

" ---- Test class ----
CLASS lcl_calculator_test DEFINITION
  FOR TESTING
  RISK LEVEL HARMLESS
  DURATION SHORT.

  PRIVATE SECTION.
    DATA mo_calc TYPE REF TO lcl_calculator.
    METHODS setup.
    METHODS test_add_positive    FOR TESTING.
    METHODS test_add_zero        FOR TESTING.
    METHODS test_divide_ok       FOR TESTING.
    METHODS test_divide_by_zero  FOR TESTING.
ENDCLASS.

CLASS lcl_calculator_test IMPLEMENTATION.

  METHOD setup.
    " Runs before each test method
    mo_calc = NEW lcl_calculator( ).
  ENDMETHOD.

  METHOD test_add_positive.
    DATA(lv_result) = mo_calc->add( iv_a = 3 iv_b = 4 ).
    cl_abap_unit_assert=>assert_equals(
      exp = 7
      act = lv_result
      msg = '3 + 4 should be 7' ).
  ENDMETHOD.

  METHOD test_add_zero.
    DATA(lv_result) = mo_calc->add( iv_a = 5 iv_b = 0 ).
    cl_abap_unit_assert=>assert_equals(
      exp = 5
      act = lv_result
      msg = '5 + 0 should be 5' ).
  ENDMETHOD.

  METHOD test_divide_ok.
    DATA(lv_result) = mo_calc->divide( iv_a = 10 iv_b = 2 ).
    cl_abap_unit_assert=>assert_equals(
      exp = 5
      act = lv_result
      msg = '10 / 2 should be 5' ).
  ENDMETHOD.

  METHOD test_divide_by_zero.
    TRY.
        mo_calc->divide( iv_a = 10 iv_b = 0 ).
        " If no exception raised, the test should fail
        cl_abap_unit_assert=>fail( msg = 'Expected CX_SY_ZERODIVIDE was not raised' ).
      CATCH cx_sy_zerodivide.
        " Expected — test passes
    ENDTRY.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
  WRITE: / 'Run unit tests: Program → Test → Unit Tests (Ctrl+Shift+F10)'.
  WRITE: / 'All pass = status bar summary only. Any failure = result popup with details'.

How to run tests

SE38: Open the program → Menu Program → Test → Unit Tests (Ctrl+Shift+F10).

  • All tests pass → status bar shows Processed: N programs, N test classes, N test methods. No popup — that message means success.
  • Any test fails → a result popup opens showing each test method with a red X and the assertion failure message.

Eclipse ADT: Right-click the program → Run As → ABAP Unit Test. Results appear in the ABAP Unit view with green/red indicators per method regardless of pass/fail.

Risk levels and durations

Attribute Values When to use
RISK LEVEL HARMLESS, DANGEROUS, CRITICAL HARMLESS = no DB writes; DANGEROUS = writes to custom tables; CRITICAL = changes config
DURATION SHORT, MEDIUM, LONG SHORT < 1 s, MEDIUM < 60 s, LONG = anything longer

One assert per test method

Test methods should test one behaviour. Use multiple small test methods rather than one large method with many asserts — this gives precise failure locations and makes it easier to see which behaviour broke.

See also

Comments