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

Lesson 5 of 5

Testing CDS view entities

10 min

Listen to this lesson

0:00 / 11:56

A CDS view is code. Its joins, CASE expressions and aggregations can be wrong in the same ways an ABAP method can, and they deserve unit tests just as much. The challenge is data: a view reads tables, and the contents of CMD's tables change daily. SAP's answer is the CDS test double framework, which redirects the view's data sources to test doubles you fill with exactly the rows each test needs.

  1. class_setupCall create on CL_CDS_TEST_ENVIRONMENT for the view under test
  2. setupClear doubles so each test starts empty
  3. Arrangeinsert_test_data with a few rows for each data source
  4. ActSELECT from the view with ABAP SQL
  5. AssertCL_ABAP_UNIT_ASSERT on totals, flags and row counts
  6. class_teardowndestroy the environment
Anatomy of a CDS unit test with the test double framework.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-201

How the framework works

SAP's ABAP Data Models guide explains that when data access in your code under test goes through CDS view entities, the framework redirects the entity's reads to doubled database tables. You create the environment once per test class for the entity under test, insert test data into the doubles of its dependencies, select from the view and assert the result. SAP's ABAP Unit cheat sheet shows the pattern with CL_CDS_TEST_ENVIRONMENT: create in class_setup, clear_doubles in setup, insert_test_data in each test and destroy in class_teardown. For CDS table functions and scalar functions implemented as AMDP, SAP points to the AMDP test double framework instead.

The CDS test double framework redirects the view’s data sources to doubles, so each test sees only the rows it inserted.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-201
Dependency to isolateFramework or classTypical use
CDS view entity logicCDS test double framework (CL_CDS_TEST_ENVIRONMENT)Test a view’s joins, CASE logic and aggregations
ABAP SQL in a classABAP SQL test double framework (CL_OSQL_TEST_ENVIRONMENT)Test a method that selects from tables or views
AMDP-based CDS functionsAMDP test double frameworkTest table functions and scalar functions
RAP business objectRAP BO test double frameworkTest code that calls a RAP BO through EML

Test double frameworks SAP documents for data-related dependencies.

What to test in a view

Test the logic you wrote, not SAP HANA. For Contoso's item view, that means: TotalDeposit equals quantity times deposit; an item with zero quantity gives zero; the per-customer aggregation adds items across agreements; the IsExpired flag flips on the right date boundary; and the many-to-one customer association does not duplicate rows. Each case needs only two or three rows of test data, which is exactly what doubles make easy. Real data in CMQ is still needed for the business acceptance test, but it is the wrong tool for proving each rule.

Each rule in the view becomes one small test with two or three rows of test data.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-201
Exploratory checksData Preview on CMQ data. Good for questions, weak as evidence.
CDS unit testsDoubles with a few rows per rule. Fast, repeatable, run on every change.
Business acceptanceMaria’s team on CMQ with realistic agreements. Proves fitness for use.
Regression packsAutomated end-to-end cases with managed test data. Protect releases.
Where CDS unit tests sit among other kinds of evidence.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-201

Write the expected values by hand from the test rows, not by running the view and copying its output; a test that copies the current result only proves the view does what it already does. Keep each test small enough that a failing assertion points straight at one rule, and name the test method after that rule, for example zero_quantity_gives_zero. Once the test class is in the same package as the view, it runs whenever someone runs unit tests for the package and in any ATC or unit test run attached to the transport.

What would you do?

Kenji's developer proposes testing the deposit total view by selecting from it in CMD and comparing to numbers he noted from last week's data. What should Kenji suggest instead?

Key points
  • CDS views contain logic and need unit tests.
  • The CDS test double framework redirects a view’s data sources to doubles.
  • Pattern: create in class_setup, clear_doubles in setup, insert_test_data per test, destroy at the end.
  • Use the ABAP SQL test double for code that selects data, and the AMDP framework for table functions.
  • Unit tests prove rules; access control and business acceptance need their own tests.

Check your understanding · not graded

Lesson check

  1. 1. Which class creates the environment for CDS test doubles?

  2. 2. Which framework does SAP point to for testing CDS table functions implemented as AMDP?

  3. 3. Why are test doubles better than live CMD data for unit testing a view?

  4. 4. Where in the test class is clear_doubles typically called?