Why unit tests?


Why is the use of unit tests extremely useful? When addressing this topic, we highlight the advantages and disadvantages and impart basic knowledge.
Test-driven development is a method of agile software development. Here, developers consistently create unit tests before the code sections or components to be tested. Unit tests can be used to check whether functions are working as intended. This can be done either manually or automatically. It is worth noting that the use of unit tests is not necessarily synonymous with test-driven development, as unit tests can also be implemented retrospectively.
SAP development environment
Abap Unit is a central component of the SAP development environment. It is supported in Eclipse as well as in the SAP internal development environment SE80. Unit tests are performed in a separate runtime environment, which offers developers a wide range of options and diagnostic possibilities.
Unit tests make the difference between testing and trial and error. While developers like to test the logic with individual parameters (test program or debugging), unit tests allow all predefined combinations of input parameters and their results to be checked for correctness with a single click. The large number of defined initial situations ensures that they still deliver the desired result after changes (bug fixing, changed requirements, or refactoring) – and that means all of them, not just the two, three, or five that you try out after a change.
You realize how useful unit tests are when you encounter code that looks something like this:

With this type of coding, it is difficult to determine which values should be delivered in which constellations without conducting research. If changes are necessary, it is not unlikely that the logic will no longer work as originally intended in certain constellations. Unit tests do not make the coding clearer, but they do define exactly which parameters should produce which results. When making changes, I as a developer can therefore incorporate new requirements on the one hand and perform refactoring on the other—both with the certainty that the existing logic will still work.
The good thing about unit tests is that they can be defined retrospectively for a class without affecting the logic of the class itself. For example, I can create a unit test for the following constellation: Ensure that the assertion i_vkorg = ‚1000‘, i_vtweg = ’10‘, and i_auart = ‚TA‘ returns the value „A.“ I can verify the result of this assertion with the press of a button.
Unlike what we learned
In conjunction with many other unit tests, this creates a safety net that gives programmers the certainty that existing business logic has not been unintentionally changed. Since solving problems step by step while testing is generally not the way programming is taught, many developers are not accustomed to using unit tests.
Many people assume that implementing unit tests involves a lot of effort. While this statement is true, it can be refuted because unit tests form a safety net that makes changes more secure. If changes are made to the code, errors can be located quickly and precisely. This saves time and frustration when searching for a needle in a haystack and manually executing business processes.
Another advantage that results from this is that the customer saves costs, as less effort is required for testing in relation to future developments. In addition, writing the tests promotes your own development, as the code quality improves through mental work and engagement with the code, since it is constantly being rethought.
One disadvantage of unit tests is that developers could use them incorrectly, thereby creating a false sense of security. In addition, there is an initial effort that must be taken into account when calculating software projects. Furthermore, developers must be aware that unit tests only check the functionality of the source code. Performance and usability do not fall within the scope of unit tests. Private methods are not usually tested, as the test classes only test the interface of public methods.
Unit tests in the ABAP environment
SAP offers the „Abap Unit“ test framework for unit tests. This has been part of the Abap stack since Web AS 6.40 and is fully integrated into the Abap Workbench and Eclipse. Abap developers can look forward to dynamic enhancements to the test framework in almost every release.
Conclusion:
Unit tests are a powerful tool that has proven itself in software development. Once a rethinking and internalization of the new approach to development has taken place, the quality of the source code increases while the amount of work required decreases proportionally. This not only saves time, but also costs.
Source: Inwerken






