ABAP Unit Test Class Generator
ABAP Unit is built into the ABAP runtime, but many teams skip it because the first test class is tedious to type. This generator creates the boilerplate: a local test class with setup and teardown, a happy path test, a boundary test and a negative test for an expected exception, each using CL_ABAP_UNIT_ASSERT.
Fill in
Test class definition
CLASS ltcl_price_calculator DEFINITION FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA cut TYPE REF TO zcl_price_calculator.
METHODS setup.
METHODS teardown.
METHODS happy_path FOR TESTING RAISING cx_static_check.
METHODS boundary_value FOR TESTING RAISING cx_static_check.
METHODS invalid_input_raises FOR TESTING.
ENDCLASS.Test class implementation
CLASS ltcl_price_calculator IMPLEMENTATION.
METHOD setup.
cut = NEW #( ).
ENDMETHOD.
METHOD teardown.
CLEAR cut.
ENDMETHOD.
METHOD happy_path.
DATA(lv_result) = cut->get_discounted_price( iv_amount = 100 ).
cl_abap_unit_assert=>assert_equals(
act = lv_result
exp = 90
msg = 'Result for the standard input is wrong' ).
ENDMETHOD.
METHOD boundary_value.
DATA(lv_result) = cut->get_discounted_price( iv_amount = 0 ).
cl_abap_unit_assert=>assert_initial(
act = lv_result
msg = 'Boundary input should give an initial result' ).
ENDMETHOD.
METHOD invalid_input_raises.
TRY.
cut->get_discounted_price( iv_amount = -1 ).
cl_abap_unit_assert=>fail( msg = 'Expected zcx_pricing_error was not raised' ).
CATCH zcx_pricing_error.
" expected path, test passes
ENDTRY.
ENDMETHOD.
ENDCLASS.Put this code in the Test Classes include of the global class, or in the same program for a report. Adjust the boundary and exception cases to your real business rules, and remove the RAISING clauses if your method does not raise checked exceptions. For code that reads the database, use test doubles or the test seam concept rather than real data.
Raise ABAP test coverage
Optional: a conversation about drafting unit tests for legacy custom code with a private assistant and developer review.
Optional. The tool and every export work without this. No spam.
How to use the generator
Enter the class and method under test, one input and the result you expect, a boundary value and the exception class your method raises on invalid input. Pick duration and risk level, which tell the runner how long the test takes and what it may touch. Copy the two blocks into the Test Classes include in SE24 or into the Local Test Classes tab in ADT, adapt the assertions to your signature, and run with Ctrl+Shift+F10 in ADT or from the class menu in SE24.
Anatomy of a good unit test
Each test method checks one behaviour with the pattern arrange, act, assert. SETUP creates a fresh object under test before every method, so tests cannot depend on each other. Use meaningful method names that describe the behaviour, and a message in every assertion so the failure explains itself. A useful suite covers the normal case, edges such as zero or empty input, and the error path.
- assert_equals for values
- assert_initial and assert_not_initial for empties
- assert_true and assert_false for flags
- fail for the path that must not be reached
Risk level and database access
RISK LEVEL HARMLESS means the test does not change persistent data or system settings, which is the right level for pure logic tests. DANGEROUS marks tests that change database data, and CRITICAL marks tests that alter customizing or system settings, and both are often blocked by default in shared systems. Isolate database reads behind an interface and inject a test double so your unit tests stay fast, deterministic and harmless.
Common mistakes
Frequent errors include testing several behaviours in one method, sharing state between tests, asserting against live database data that changes, and writing tests that only pass in one client. Another is never running them: connect the tests to the ABAP Test Cockpit so they run as part of the release check. A test that never fails is as bad as no test, so break the code once on purpose to see the test go red.
Frequently Asked Questions
Where do I put the generated test class?
For a global class, put it in the Test Classes include, reached through the menu Goto then Local Definitions and Implementations then Local Test Classes in SE24, or the Test Classes tab in ADT. For a report, it can sit in the same program source.
What does RISK LEVEL HARMLESS mean?
It declares that the test makes no persistent changes to the database or the system settings. The runner uses the risk level to decide whether a test may run in a given system. Pure logic tests with test doubles should be HARMLESS.
How do I test code that selects from the database?
Move the SELECT into a small class behind an interface and inject a test double in the unit test, or use the test seam statements for legacy code. For CDS views, the CDS test double framework lets you supply input data without touching real tables.
How do I run all tests in a package?
In ADT, right-click the package and choose Run As then ABAP Unit Test. In the SAP GUI, run ABAP Unit from the object list in SE80 or include the checks in an ABAP Test Cockpit run, which can also give coverage figures on suitable releases.
If your custom code has little or no test coverage, Netray can discuss how a private AI assistant could draft test cases from your own methods, with developers reviewing every test.
Related Tools
ABAP Custom Exception Class Generator
Fill in a class name, message class and attribute and get a ready-to-paste class-based exception with a TRY/CATCH usage example.
SAPABAP Performance Tuning Checklist
A checklist of database, internal table and memory checks to run on slow ABAP reports before they reach production.
SAPABAP Transport Request Release Checklist
A pre-release checklist for ABAP transport requests that catches missing objects, untested code and import order problems.
SAPABAP Built-in Data Types Reference
A quick lookup of the predefined ABAP data types with length, value range and initial value.
More Developer Tools tools
See all- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
- SAP QM Usage Decision BAPI Skeleton
- ABAP BAPI Return Handling and Commit Generator
- SAP ABAP AUTHORITY-CHECK Snippet Generator