ABAP Unit - Analysis
This article is about the analysis of the test cases that we have written, how do we get the necessary information and how we can interpret it.
Now that you have learned what ABAP Unit actually is, how to set up a simple test and where you can use the framework everywhere, we will deal with the topic of analysis here. What options are still available to you?
If you run the unit test normally, the test class or classes will be searched for in the current object and executed one after the other. At the end, the result is output, which will hopefully be positive.
The output takes place in the ABAP Unit View in Eclipse and offers a simple overview of the test results. You still have the option to switch between the different types of errors with the checkboxes, for example if you only want to see the errors. In the upper part you have some buttons that offer you the following options:
- Open/close the classes, test classes and methods
- Filter the result and only repeat certain test methods
- Repeat test
- History of tests carried out
- More settings
If the test should break off, the picture could look different. In this case, the class with the error is expanded and the method that had an error is displayed. The error trace is displayed in the lower area or on the right side of the window.
By double-clicking on the stack (highlighted line in the screenshot), you can jump directly to the assert in your test case to start analyzing the error more precisely.
With the coverage analysis, you can also analyze the coverage of your test cases. This is about how much of the coding in the object is covered by the test cases. You can store a large number of test cases in a class, but if they all cover the same branches and do not simulate any special cases, then they are not complete and do not cover 100%.
For the sake of clarity, we create the following class, which provides a method and performs a division.
CLASS zcl_demo_coverage DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. TYPES: td_result TYPE p LENGTH 15 DECIMALS 5. CONSTANTS: c_fail TYPE td_result VALUE -999. METHODS: divide_numbers IMPORTING id_num1 TYPE i id_num2 TYPE i RETURNING VALUE(rd_result) TYPE td_result. PROTECTED SECTION. PRIVATE SECTION. ENDCLASS. CLASS zcl_demo_coverage IMPLEMENTATION. METHOD divide_numbers. TRY. rd_result = id_num1 / id_num2. CATCH cx_sy_zerodivide. rd_result = c_fail. ENDTRY. ENDMETHOD. ENDCLASS.
To do this, we define the following test class with a test case for the method:
CLASS ltc_class_basic DEFINITION FINAL FOR TESTING DURATION SHORT RISK LEVEL HARMLESS. PRIVATE SECTION. METHODS: divide_10_and_2 FOR TESTING. ENDCLASS. CLASS ltc_class_basic IMPLEMENTATION. METHOD divide_10_and_2. DATA(lo_cut) = NEW zcl_demo_coverage( ). DATA(ld_result) = lo_cut->divide_numbers( id_num1 = 10 id_num2 = 2 ). cl_abap_unit_assert=>assert_equals( act = ld_result exp = 5 ). ENDMETHOD. ENDCLASS.
This time we run the test with CTRL + SHIFT + F11 and get a successful result again. In this case, however, a coverage measurement was also carried out. We find this in Eclipse in a separate view, the “ABAP Coverage”.
As you can see, the coverage is only 60% although we have tested the method properly? To do this, we switch to the class, but don't forget to deactivate the “Hide Highlighting” button beforehand so that we can see the markings in the source code.
Here you can quickly see that we have not set up a negative test case, so what actually happens if we pass a zero for the second number? Here it would be worthwhile to create a second test method and also to go through this case. The coverage measurement gives you conclusions about the quality and coverage of your code by your test class.
This article is about analyzing your test class in terms of the quality of your test cases and the coverage of the code by your tests. However, you should also take into account that 100% coverage does not always make sense and is sometimes too expensive to realize.