In the third blog post in the Unit Tests series, our developers Max and Basti, use a sample tutorial to demonstrate the implementation of unit tests. They use the Eclipse development environment and follow the test-driven development approach.
The first step to implement unit tests in ABAP is to create a global class. The logic will be implemented later. Subsequently, a test class is created for this class. There, the individual unit tests must be implemented in the form of methods in order to test the logic. In this tutorial, we use Eclipse because it simplifies matters and offers good features in creating unit tests. Furthermore, we are writing test-driven in this tutorial and this means that we are writing the test before the logic.
This is best explained by a sample task :
The task is as follows: Create a class with a method that checks what type of availability check is made when the following input parameter meets a predefined value (in the “real” case. There are certainly more input parameters but for this tutorial we’re trimming it to something smaller in order to get a basic understanding of unit tests):
Possible input values are:
- For MTART:
Possible return parameters:
o N: No test/check
o V: full test/check
o S: quick test/check
Unless otherwise determined by the method, a quick check should always be performed. In case something else is determined, the following rules apply:
- MTART PROD = Full Test/check (V)
• MTART HALB = Full Test/check (V)
• MTART FERT = No Test/check (N)
# Implementation ## 1. Creating a Global Class in Eclipse! [Create New Class] (./ assets / gif / new-class.gif)
This class remains empty for now. We will implement the logic at a later stage.
Creation of the test class
To create a test class we use the tab “Test Classes”.
After entering test, a template for implementing a test class from Eclipse will be displayed.
Implementation of the first Test-Method
At this point, we will write our first test method. Only then we will implement the associated logic in the actual method. According to the rule MTART = HALB, the result should be “V”, so make sure that you this also tested. Any logic is not part of the test method.
To implement a simple unit test, we need the class cl_abap_unit_assert, and in this case its method assert_equals.
Using this method, we can check the return value of the tested method for a predefined value and perform our unit test.
At this point, we are also creating the actual method into which we will implement the logic at a later stage in the tutorial. We call this method check_all and directly define the input and return parameters.
- iv_mtart type mtart
- r_result type string
The test method should look like this:
The first Test
We are now ready and we can carry out the first test. To do this we use the shortcut CTRL + SHIFT + F10 in Eclipse. Unfortunately, the test method will not be successful at this point, as nothing has yet been implemented in the tested Global class. Nevertheless, we perform the test to better illustrate the example:
The Failure Trace now makes it easy to see what was expected as a test result and what the verified method actually returns:
As we noted in Section 4, our test failed. So let’s take this opportunity: Let’s fix the BUG! This is the first logic of our class which is found in the Global Class tab below.
In step 3 we have already created the method check_all. In this we will implement our logic to pass the test:
A few lines of code and the test should be passed:
In principle, it is important to note that the test covers the entire method (100% test coverage). This is clearly visible in Eclipse by the color markings of the code.
On this basis, one can now proceed and implement further tests for MTART PROD and MTART FERT until a logic develops in the global class.
Max & Basti