Unit Test Automation: The prerequisite for Continuous Delivery in SAP environments
With the constant demand to develop and change SAP applications faster, the need for continuous delivery has never been greater.
Agile, continuous delivery and DevOps have been commonplace in many IT organizations for some time now and the need to apply these concepts to SAP is starting to catch on.
But what effect will that have on the way development is done in SAP?
One of the core concepts in DevOps is that of Continuous Integration (CI). When code is changed, unit tests are automatically executed to verify its quality. The goal is to identify problems and bugs early in the development lifecycle so they don’t cause issues later in QA or worse, in production.
Planning and executing unit tests is time-consuming and tedious, so a solution to automate it would seem like a no-brainer. So why don’t we see this being done very often in SAP?
SAP development is different, but unit testing is still relevant.
Outside of SAP development, CI ensures that changes are built and verified via automated unit tests when a developer checks their local code into the central repository. If something has broken the build, developers are informed quickly so they can implement the necessary fixes.
SAP, and particularly ABAP, is different as all development is carried out in a shared system. Many changes are then immediately visible, and affect, everyone.
Unit testing capability exists as standard within SAP, so how can we start using it effectively to implement CI processes?
Unit testing and test-driven development (TDD)
Firstly let’s understand what unit testing is about. Essentially we’re aiming to verify that when code is created or changed it behaves as intended. We’re also trying to ensure that anything using that code will work properly as long as the unit test is passed.
When writing unit tests there are some key principles that need to be considered:
- Independence - to make sure that one failing unit test doesn’t affect others, you must make sure that each can be run independently.
- Consistency - a test should deliver the same results when it is re-run (all other things being equal).
- A single unit of work - it’s important to test small parts (units) of code that have distinct functions.
- Good code coverage - the test needs to ensure that all the code is executed, including exceptions.
- Run fast - if the test fails the developer needs to know quickly so that it can be addressed.
TDD goes hand-in-hand with unit testing. The concept is that the automated unit test is written before any code - initially the test will fail as the code is not written yet. Then the minimum amount of code is written in order to pass the test, which can then be refactored as required.
A change in approach to SAP development.
In order for unit testing to be effective we need to ensure that the code “units” are sized correctly so they can be effectively tested. It therefore follows that how code is designed and written needs to change so that applications are built based on these units.
Traditional programming where 100s or 1000s of code lines are written to perform an application function are very hard to test as the code does many things.
This is where object-oriented programming comes in to break down applications into smaller reusable objects and classes.
It helps to follow design principles like SOLID and in particular the concepts of single responsibility and dependency inversion.
Having single responsibility in your code (i.e. it only does one thing) will improve your ability to inject mock data into the precise and isolated locations where it is needed, and thus enhances the granularity of the objects to be unit tested.
Dependency invertible code will allow you to easily replace the processing of real data by injecting your mock unit testing data generator object, so that you get predictable test results.
A definition of SOLID can be found here .
It can be a challenging mind shift to adopt this technique but as long as classes are designed and built in the correct way the process should be reasonably straightforward.
Tools like ABAP Unit can then be used to develop and build unit tests. Ideally, these tests should be executed automatically when
Some useful examples on the usage of ABAP Unit can be downloaded in this e-Book here.