Write a CDS unit test
What you'll learn
Spin up cl_cds_test_environment for the entity, inject known rows, SELECT, assert with cl_abap_unit_assert, and destroy the environment in teardown.
- 1. Declare the test class FOR TESTING RISK LEVEL HARMLESS.
- 2. Build the environment with cl_cds_test_environment=>create( i_for_entity = '...' ).
- 3. Seed rows via lo_env->insert_test_data( ... ).
A CDS view embeds logic — joins, aggregations, filters — that you cannot trust to be correct just because it activates. The CDS Test Double Framework lets you unit-test that logic in isolation: it builds a test schema, injects rows you control, runs your SELECT against the entity, and lets you assert on the result, all without depending on whatever happens to be in the real tables.
The test class is declared FOR TESTING RISK LEVEL HARMLESS because it touches no productive data — only the generated test doubles. You create the environment with cl_cds_test_environment=>create( i_for_entity = '...' ), naming the CDS entity under test, then seed it with insert_test_data passing a table of rows for the underlying source.
With data in place you SELECT from the entity exactly as production code would and assert the outcome with cl_abap_unit_assert — typically assert_equals on a row count or an aggregated value. The one piece teams forget is teardown: call lo_env->destroy( ) so the generated doubles are cleaned up and tests stay independent of one another.
Key points
- 1. Declare the test class FOR TESTING RISK LEVEL HARMLESS.
- 2. Build the environment with cl_cds_test_environment=>create( i_for_entity = '...' ).
- 3. Seed rows via lo_env->insert_test_data( ... ).
- 4. SELECT from the CDS entity under test.
- 5. Assert the result with cl_abap_unit_assert.
- 6. Call lo_env->destroy( ) in teardown.
Examples
Create the test double for the entity, inject two rows, query, and assert that only the OPEN order is aggregated; destroy in teardown.
ABAPclass ltc_open_orders definition for testing risk level harmless duration short.
private section.
class-data go_env type ref to if_cds_test_environment.
class-methods class_setup.
class-methods class_teardown.
methods aggregates_only_open for testing.
endclass.
class ltc_open_orders implementation.
method class_setup.
go_env = cl_cds_test_environment=>create( i_for_entity = 'ZI_OPENORDERSBYCUSTOMER' ).
endmethod.
method aggregates_only_open.
go_env->insert_test_data( value zorder_hdr_t(
( client = sy-mandt orderid = '01' customerid = 'c1' total = 100 status = 'OPEN' )
( client = sy-mandt orderid = '02' customerid = 'c1' total = 50 status = 'CLOSED' ) ) ).
select * from zi_openordersbycustomer into table @data(lt_actual).
cl_abap_unit_assert=>assert_equals( exp = 1 act = lines( lt_actual ) ).
endmethod.
method class_teardown.
go_env->destroy( ).
endmethod.
endclass.Source notes: clean-core-curriculum §12.5
Ask Claude
Build a prompt from this lesson + your question and open a fresh Claude chat with it pre-filled — handy for adapting a before/after pattern to your own object.