18. December 2022 Unit Tests

Everybody is talking about unit tests these days. And that is good! Unit tests help to make software more robust. They also help, that programmers need to think more about the design of their application.

Test Driven Development

When talking about unit tests, you often will hear about the Test Driven Development approach. This software development process makes sure, that only the things that are needed will be coded. It’s a different approach to normal programming. When you start programming without unit tests, then you code what you think is needed for the requirement. Afterwards you will check if the output is as expected. When using the TDD process, you first think about what results you expect for different input data. You define the interface for that function and you write unit tests where you set up input values and define what ooutput values are correct for this specific input. This perspective is to reduce errors in the development phase.

In the TDD process you start with one or at least a few unit test. Then you code until all tests are green, which means that all tests have been successfully passed. After that you may start refactoring the code, because there will be some ugly code passages in your code. If the code fails, start over again. If the tests passed, good job!

There is a community event where you can learn the TDD principles using ABAP and Eclipse: The ABAP Code Retreat. The format was brought to life by Damir Majer and Christian Drumm.

Is TDD necessary?

It is not necessary to start with Test Driven Development in order to use unit tests! You can implement unit tests still after your work is done. Unit tests will help you to make your code robust against later changes. If you have written sufficient unit tests, then you can be sure that a change does not break existing functionalities.

I am not a friend of the TDD process, because I like it more to build prototypes to verify that my ideas are working. Never the less, you should make your own experiences using TDD and see if this approach works for you and your team. If not, you should not give up too early, because there is a lot of training needed to beome a master. If afterwards you are still not convienced, then you can argue, why.

Mocking

In unit testing, mocking is a technique that is used to isolate and test the behavior of a specific unit of code, such as a class or a function. This is typically done by creating a mock object that stands in for a real object that the code under test depends on. The mock object is programmed to behave in a predetermined way, and the unit test uses the mock object to exercise the code under test.

Mocking can be useful in a number of situations, such as when the real object that the code under test depends on is difficult or impossible to obtain, or when the real object has complex or time-consuming behavior that is not relevant to the test. By using a mock object, the unit test can focus on testing the specific behavior of the code under test, without being affected by the behavior of the real object.

Overall, mocking is a useful technique for improving the isolation and reliability of unit tests, and can help to make the testing process more efficient and effective.

ABAP Unit Test in real life

One of the most impressive applications of that past years is abapGit. Marc Bernard, one of the main contributors, spectacularly demonstrated how useful unit tests are with his latest twitter post:

There is no doubt, that unit tests are useful but also hard to achieve.

ABAP Unit Test help

Here is some help to dive into the world of unit testing:

Articles

There are a lot of blog posts about unit tests at blogs.sap.com:

OpenSAP

If you want to learn about how to write unit tests, then you should take the free course Writing Testable Code for ABAP in the OpenSAP Online courses.

ABAP Unit Test Examples

I created a github repository where I started to collect some real life ABAP unit test examples. May be this helps you to better understand ABAP unit test. Feel free to add your own examples!

Chat GPT

I also asked ChatGPT:

tell why a programmer should use unit tests and what effects unit tests have on the code architecture

This is, what ChatGPT answered:

Unit tests are an important part of the development process for software because they help ensure that the individual units of code that make up a software application are working properly. This is important because it allows the programmer to catch and fix any issues with the code early on, before they become more difficult and time-consuming to fix. Additionally, unit tests can help to prevent regressions, which are issues that arise when code that was previously working properly is modified or updated in a way that causes it to break.

One of the major benefits of using unit tests is that they can help to improve the overall design of the code. This is because the process of writing unit tests often requires the programmer to think carefully about the architecture of the code, and to decompose the code into smaller, modular units that can be tested independently. This can lead to a more modular and flexible design, which is easier to maintain and modify over time.

Furthermore, unit tests can help to improve the reliability and maintainability of the code by providing a set of automated checks that can be run every time the code is modified. This can help to catch any issues that may arise as a result of the changes, and can help the programmer to ensure that the code continues to function as intended.

Overall, the use of unit tests can help to improve the quality of the code, and can make the development process more efficient and effective.

I think, there is nothing left to say… 😅

Enno Wulff