Module 31: Testing & Quality
Testing and quality assurance ensure that ABAP code is correct, maintainable, and production-safe.
In modern SAP landscapes, code without tests is considered incomplete.
1. Unit Testing (ABAP Unit)
What is ABAP Unit?
ABAP Unit is SAP’s built-in unit testing framework used to test:
- Individual classes
- Methods
- Business logic
All new custom classes should have ABAP Unit tests.
1.1 Basic ABAP Unit Structure
CLASS ltcl_calc_test DEFINITION
FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
METHODS test_addition FOR TESTING.
ENDCLASS.
CLASS ltcl_calc_test IMPLEMENTATION.
METHOD test_addition.
DATA(lo_calc) = NEW zcl_calculator( ).
cl_abap_unit_assert=>assert_equals(
act = lo_calc->add( 2, 3 )
exp = 5
).
ENDMETHOD.
ENDCLASS.
Test behavior, not implementation details.
2. Test Doubles (Mocks & Stubs)
Why Test Doubles?
Test doubles isolate:
- External dependencies
- Database access
- RFC calls
Unit tests must be fast and deterministic.
Example Using Dependency Injection
INTERFACE zif_data_provider.
METHODS get_data
RETURNING VALUE(rt_data) TYPE STANDARD TABLE.
ENDINTERFACE.
Production class:
CLASS zcl_service DEFINITION.
PUBLIC SECTION.
METHODS constructor
IMPORTING io_provider TYPE REF TO zif_data_provider.
ENDCLASS.
Test double:
CLASS lcl_mock_provider DEFINITION.
PUBLIC SECTION.
INTERFACES zif_data_provider.
ENDCLASS.
If you mention dependency injection + test doubles, you score high.
3. Code Coverage
What is Code Coverage?
Code coverage shows:
- Which lines were executed during tests
- Untested areas
Measured via:
- ABAP Unit
- ATC integration
High coverage ≠ good tests
But zero coverage = bad quality
Coverage Targets
| Area | Typical Target |
|---|---|
| Business logic | 70–90% |
| Utility classes | 80%+ |
| UI logic | Lower |
4. Clean Code & ATC (ABAP Test Cockpit)
What is ATC?
ATC enforces:
- Syntax correctness
- Performance rules
- Security standards
- Clean ABAP guidelines
ATC checks are mandatory before transport to PRD.
Common ATC Findings
| Finding | Meaning |
|---|---|
| SELECT * | Performance issue |
| Nested SELECT | Performance risk |
| Obsolete syntax | S/4 incompatibility |
| Missing AUTHORITY-CHECK | Security risk |
Fixing ATC Issues
- Fix ATC issues early
- Do not suppress warnings without justification
- Treat ATC as design feedback, not punishment
5. Clean Code Principles (Reinforced)
Clean ABAP Checklist
- Small methods
- No deep nesting
- Meaningful naming
- Avoid global data
- Prefer expressions
- Fail fast
Example: Fail Fast
❌ Legacy
IF lv_flag = 'X'.
IF lv_count > 0.
IF sy-subrc = 0.
...
ENDIF.
ENDIF.
ENDIF.
✅ Clean
CHECK lv_flag = abap_true.
CHECK lv_count > 0.
CHECK sy-subrc = 0.
6. Quality Gates in Real Projects
Code → Unit Tests → ATC → Transport → QAS → PRD
ABAP quality gates integrate into CI/CD pipelines.
7. Common Testing Mistakes
- No unit tests
- Testing database directly
- Hard-coded dependencies
- Ignoring ATC findings
- Testing UI instead of logic
8. Interview-Grade Answer
Q: How do you ensure ABAP code quality?
Answer:
I write ABAP Unit tests with proper test doubles, monitor code coverage, follow Clean ABAP principles, and enforce ATC checks before transports to ensure correctness, performance, and security.
This reflects modern enterprise ABAP practices.
9. Summary
- ABAP Unit tests validate logic
- Test doubles isolate dependencies
- Code coverage measures test reach
- ATC enforces quality standards
- Clean ABAP ensures maintainability
10. Practice Exercises
- Write unit tests for a utility class.
- Introduce a mock for DB access.
- Measure code coverage.
- Fix ATC warnings.
- Refactor code to Clean ABAP.
11. What’s Next?
➡️ Module 32: Error Handling & Logging
In modern ABAP, quality is not optional — it’s enforced.