Now accepting guest editorial from SAP, Oracle, Workday and ServiceNow practitioners.Pitch a guide
The Sign-Off Table

Lesson 2 of 5

ABAP Unit basics

11 min

Listen to this lesson

0:00 / 14:22

An ABAP Unit test is an ordinary ABAP class with a few extra keywords. For a global class such as Contoso's ZCL_CMC_CORE_DEPOSIT, you write local test classes in the class's test include: in ABAP Development Tools (ADT) that is the Test Classes tab of the class editor, separate from Local Types. Test code lives with the production code, is transported with it, and is not executed in production. Typing test and pressing Ctrl+Space in that tab offers the testClass template, which generates the skeleton for you.

Local test classes live in the class’s test include, travel with the production code and never run in production.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-205

The test class declaration

A test class is declared with FOR TESTING plus two attributes: RISK LEVEL and DURATION. A typical declaration reads CLASS ltc_deposit DEFINITION FINAL FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. Test methods are declared with FOR TESTING after the method name, take no parameters, and are best kept in the private section. Any other method in the class is a helper the framework does not call directly.

AttributeValuesMeaning
RISK LEVELHARMLESSThe test changes neither persistent data nor system settings. Aim for this.
RISK LEVELDANGEROUSThe test may change persistent application data.
RISK LEVELCRITICALThe test may change system settings or Customizing. SAP Learning notes this is the default when the addition is omitted.
DURATIONSHORT / MEDIUM / LONGExpected runtime: a few seconds, around a minute, more than a minute. Client settings can set limits.

Risk level and duration. Client-level settings can block tests above a risk level, so a test that writes data may simply not run in some clients.

The fixture: setup and teardown

The fixture is everything a test needs before it runs: the object under test, test doubles and test data. ABAP Unit calls four optional fixture methods with fixed names. The framework creates a fresh instance of the test class for each test method, so tests never share instance attributes by accident.

  1. Once, firstclass_setupStatic. Expensive preparation that tests do not change, such as creating a test double environment
  2. Before each testsetupInstance. Create the code under test and reset doubles and data so every test starts clean
  3. The testtest methodGiven, when, then: arrange inputs, make exactly one call, assert the outcome
  4. After each testteardownInstance. Only when something outside the instance must be undone, such as a rollback
  5. Once, lastclass_teardownStatic. Undo class_setup, for example destroy the test double environment
The order in which ABAP Unit calls fixture methods around each test method.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-205

Assertions

Results are checked with the static methods of CL_ABAP_UNIT_ASSERT. Most take act (the actual value) and, for comparisons, exp (the expected value), plus an optional msg that explains the failure in business words. A failing assertion stops the test method by default and is reported with the message, the values and the source position.

MethodFails whenContoso example
assert_equalsact differs from expDeposit for 10 alternators is not 450.00
assert_differsact equals expA new agreement number equals the old one
assert_initial / assert_not_initialValue is not initial / is initialThe error message table is not empty for a valid order
assert_boundA reference is not boundThe factory returned no deposit calculator
failAlways, when reachedExpected exception was not raised
skipNever fails; marks the test as skippedA prerequisite is missing in this client

Common assertion methods. Use the one that states your intent most precisely.

A first test, given-when-then

Kenji's first test for the deposit rule reads like this in words. Given a fleet agreement with a deposit of 45.00 per alternator, when the calculator is asked for the deposit on 10 units of CMC-ALT-100, then the result is 450.00. In code, the "when" is one line: DATA(deposit) = cut->calculate( quantity = 10 ).
The "then" is one line: cl_abap_unit_assert=>assert_equals( act = deposit exp = '450.00' msg = 'Deposit per line' ).

A first test in given-when-then form: one arranged agreement, one call, one assertion with a business-language message.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-205

Clean ABAP guidance from SAP keeps tests readable: name test methods for what is given and expected (deposit_for_ten_units, rejects_negative_quantity), keep the "when" section to exactly one call, use few focused assertions, and skip teardown unless something outside the test instance really needs cleaning up.

  1. Does the test change persistent data or Customizing?Yes → replace that dependency with a test double so it can be HARMLESS · No → HARMLESS
  2. Does the test need more than a few seconds?Yes → look for a slow dependency to isolate before raising DURATION · No → SHORT
  3. Is the same expensive setup needed by every test?Yes → class_setup, reset per test in setup · No → setup only
Choosing risk level, duration and fixture placement for a new test class.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-205

Running tests

In ADT, place the cursor in the class and press Ctrl+Shift+F10, or right-click and choose Run As > ABAP Unit Test. Results appear in the ABAP Unit view with a green or red bar, each failed method, its message and a link to the failing line. You can also run all tests in a package from the Project Explorer. An ABAP Doc comment such as "! @testing ZCL_CMC_CORE_DEPOSIT on a separate global test class creates a test relation, so the tests run when someone runs tests for the related object.

What would you do?

A new developer on Kenji's team writes a test class without RISK LEVEL and DURATION, and one test method that creates three agreements, calculates deposits for all of them, and asserts nine values. What feedback should Kenji give first?

Key points
  • Test classes are declared FOR TESTING with RISK LEVEL and DURATION; aim for HARMLESS and SHORT.
  • Fixture methods: class_setup, setup, teardown, class_teardown; a fresh instance per test.
  • Assert with CL_ABAP_UNIT_ASSERT and a business-language message.
  • Write given-when-then tests with one call in the "when" section; run them with Ctrl+Shift+F10.

Check your understanding · not graded

Lesson check

  1. 1. Which fixture method runs before every single test method?

  2. 2. A test only calculates values in memory. Which risk level fits?

  3. 3. Where does ADT keep local test classes for a global class?

  4. 4. Which assertion should a test use to check that a calculated deposit equals 450.00?