Keep your code clean with ABAP Unit Test Framework in Eclipse

KEEP YOUR CODE CLEAN WITH ABAP UNIT TEST FRAMEWORK IN ECLIPSE – PRACTICAL EXAMPLE

The today’s article is the second from a two part series which focuses on leveraging the ABAP Unit framework in Eclipse. In this second part we will demonstrate the exact procedure in a practical exampleThe first theoretical part offers you the necessary know-how to create Unit Test Cases for your code.

Practical example

As an example, we will take a global class of a calculator which defines the four basic mathematical operations: addition, subtraction, multiplication and division. This will be our codebase for which we will apply the Unit Testing technique inside the Eclipse platform.

Eclipse does not have a wizard for generating automated unit tests, but it does provide some templates you can make use of. Considering the fact that you may be new to this subject, it is far better to start writing tests by hand first. That being said, do not wait for the right moment anymore. Thus, open your Eclipse IDE, switch to the ABAP perspective and create an ABAP class with the following source code:

ABAP coding: exception divide function

Picture 1: Generating an ABAP class

ABAP class creation in eclipse

Picture 2: Generating an ABAP class (extension)

Intuitively, the smallest component of code that can be tested here is a method and this will be our ”unit” too. For instance, we will take the method ”divide” as a method under test and we will cover different cases and exceptions for it.

As presented in the theoretical part, the first step in defining the test cases is to create a test class. For that purpose, choose the ”Test Classes (non existent)” tab from the bottom of the editor view.

defining the test cases in ABAP Unit Framework

Abbildung 3: Setting up Test Classes

In the new window that will appear on your screen press the ”Create Test Classes…” button and an editor page will be displayed in changed mode. You are now ready to start coding your test class.

start coding a test class

Picture 4: Programming a Test Class

If you think of which cases to cover, you should consider different categories of input values that need to be testesd as: positive cases, negative cases, boundary values or combinations of input values. Definitely, each case will be treated in a separate test method. The best practice is to start with the simplest scenarios and than to continue adding more complex sets of values to be tested.

For executing the test methods we can use the shortcut keys CTRL + SHIFT + F10 or we can press the drop-down button ”Run As” and choose the ”ABAP Unit Test” option from the list. The status and the duration of each test case will be shown in the ”ABAP Unit” view.

In case of any failures, the test method that fails will have a different icon, to be easily spotted, and, what is more important, the error messages will be added to the ”Failure trace” section. If we double click on the path under the ”Stack” node, it will directly point us to the block of code that actually went wrong. Unit tests enable changes and that makes our testing safe.

coding failure of a test class

Picture 5: Error message

After the necessary adjustments of the code, we just have to rerun the tests and check if their statuses have changed and no failures are actually present.

Test class coding failure in ABAP Unit Test Framework

Picture 6: Status check of already run tests

Let us add a few more test cases and handle also an exception of the ”dividefunction, the well-known division by zero. Subsequently, our test class should look as follows:

Divide function in ABAP Unit Test Framework

Picture 7: Exception of the ”divide” function

ABAP Coding: divide function exception

Picture 8: Exception of the ”divide” function (extension)

ABAP Coding: exception divide function

Picture 9: Division by zero

Please notice that tests are executed in an indefinite sequence and not in the order they have been defined in the test class:

ABAP Test executed in an indefinite sequence

Picture 10: Indefinite executed tests

We invite you to create even more test cases, for the purpose of learning and for a better understanding of the ABAP Unit framework in Eclipse. Anyway, we have not come to an end yet. Our test code is functional but is it clean and optimized?

In other words, after all tests are green, we are ready to apply some refactoring to the code. Then again, what is refactoring? Refactoring is the process of changing and improving the internal structure of the existing code by reducing the complexity without altering its external behaviour. In this way, the next tests will be shorter to implement by means of code reusability and legibility.

Furthermore, we will leverage the special methods provided by the Unit framework and we will use the ”SETUP” method to implement the ”given” part defined in our test methods. This method will be executed before each test case and its ultimate purpose is to reference the code under test. On the consideration that writing tests should be efficient, we should also use ”helper methods” to make the test code readable, clean and small.

All things considered, after refactoring, the test class will have the following design:

Refactoring a test class in ABAP Unit Test Framework

Picture 11: Test class design

ABAP Unit Test: refactored test class

Picture 12: Test class design (extension)

Our test class has now a better code structure, efficiency, clarity and quality. Finally, we can get another sip of coffee.

Do you think it is unrealistic having your product code and tests ready at the same time? Not anymore..

Test Driven Development – TDD

Nevertheless, when talking about Unit Testing, it is essential to mind the Test Driven Development (TDD) technique. Also known as the ”Test First” procedure, it implies starting with small requirements and creating the unit tests in the first place. To be more specific, we should outline the behaviour of the application and what should it do from a user perspective, and write the small features in a text document or even as comments in the test classes, to be further used in the development. These requirements will subsequently be converted into specific test cases.

Using the TDD technique, you will always get an answer to the question ”What code should I write next?”. For instance, each new feature will be added by firstly creating a test for a single feature and not for multiple ones at a time. Obviously, running this test will demonstrate a failure, due to the absence or deficiency of the code under test. At this point, we should write just enough of the production code to pass the failed unit test. If the test will not pass, the production code must be adjusted until it does.

Considering this technique is a development cycle, we should repeat the above steps for all the required functionalities in order to have a major code coverage. The next test will drive the next step, in other words, test development drives feature development. At the end of the cycle, when all tests are green, the production code and the test code are ready for refactoring. This phase is fundamental in order to increase readability, to reduce duplicates and to have a clean code. For more information about Unit Testing and Test Driven Development, I kindly recommend you to enroll to the OpenSAP course ”Writing Testable Code for ABAP”, which also represents a strong base of the article today.

Conclusion

Furthermore, unit testing is usually automated. As a matter of fact, Eclipse provides a Unit Test framework which is embedded in the ABAP Development Tools plugin and facilitates the process of defining and executing automated unit tests. Unit tests are simple, stable and provide a major code coverage when having a right setup. Tests can be built during the development process and they can even be transported together with the source code. Unit tests are self-dependent and their execution takes place in a random order. Keep in mind when writing tests that they should not go beyond their class edges and interact with other components. They should only test the behaviour of that class or of the small component of code under test.

As you may already be familiar with, many typical software projects are formed of multiple modules often coded by various developers. The individual modules are first tested in isolation and here is the moment when the ABAP Unit comes to help, as a first level of testing our code. After all these units are developed and tested, we can cluster the ”unit tested” components and test their behaviour using the Integration technique. But this is another story to talk about..

Looking forward to more knowledge each day? We would be very happy to see you on our Inspiricon blog again. Until then, feel free to explore the ABAP Unit framework in Eclipse. And do not forget.. ”Truth can only be found in one place: the code”. Happy coding! 😊

 

Author
Andra Atanasoaie Associate
Phone: +49 (0) 7031 714 660 0
Email: cluj@inspiricon.de