Show TOC

ABAP Unit - Notes on UseLocate this document in the navigation structure

Use

ABAP Unit tests are developer tests. With ABAP Unit you can implement, execute, and evaluate tests that run alongside development quickly and efficiently.

You can start ABAP Unit tests directly from the development tools. You can also have them executed automatically after changes or enhancements to verify the integrity of whole ABAP components.

Assuming that you develop an ABAP class pool, a 'test-driven' development procedure or a development procedure ensured by tests could look as follows:

  1. Create methods together with signatures.

  2. In parallel to this, creating Unit Tests for the Methods using the ABAP Unit Wizard.

    The wizard creates a local Test Class for you with test methods for the methods that you have specified.

    Ideally, you only have to make a few adjustments to complete the test methods, for example, enter data for calling methods or adjusting the assert condition for the requirements of the methods.

    If you want to define other methods, repeat this step to extend the test class with additional test methods.

    You can use the same procedure for function groups. For other program objects you have to create local test classes manually.

  3. Activate the class (and all includes).

  4. While you implement the methods, use the relevant test methods to check that the methods are functioning correctly under all conditions.

  5. Execute the unit tests as regression tests after any change or enhancement. In this way you can be sure that the new source text is functioning correctly and that existing functions are not impaired.

    You can execute ABAP Unit tests manually or automatically .

If you implement unit tests with ABAP, be aware of the following general recommendations :

  • Create ABAP Unit tests for every program object.

    Unit tests of ABAP Unit fill the gaps between static checks such as the ABAP Enhanced Check and Integration Tests and Decrease Tests .

    With ABAP Unit you can verify the correct function of your software components before you leave the development environment.

    With ABAP Unit you prevent avoidable malfunctions in the integrations and decrease phase.

  • Write your unit tests before you write the production code.

    The test code should check if the productive code functions as required. If you write your tests after the implementation of the production code then you run the risk that the test class only verifies the functions that you already know are without errors.

  • Ensure that you check the unit tests of individual software components. In ABAP, software components are usually methods of a class of function modules of a function group.

    Be sure that you only test individual software components in the ABAP Unit. Do not create any integration tests in the ABAP Unit.

    A test that only depends on one component can be executed at an earlier point in time. It is quick to execute tests and it is easy to locate causes of errors.

    In practice it is often difficult to execute only one method, for example. There are often dependencies of other methods of the same or other classes so that existing tests often execute more than one method.

    The important thing is that it is quick to execute tests and the location of the error is retained. A test is not a unit test if the cause of an error remains unknown, if data has to be present in the persistence layer, or if user interaction is required for the execution.

  • Use test-driven programming techniques to achieve a higher level of source coverage with fine granularity for your unit tests. For example, ensure that you resolve dependencies that could restrict tests, such as the direct use of date entries using the system field SY-DATUM or non-encapsulated database operations.

    Such dependencies could mean that a unit test only check the code unit to be tested or that a unit test is not possible at all.

  • Use [ class_] setup- and [ class_] teardown methods to construct (and then deconstruct) the environment ( Fixture ) required for a unit test.

    Does a methods need any example data or does it need another component?

    You can use the fixture methods of the ABAP Unit to create the required test data and Test Dummies and then to remove them again at the end of the test.

  • Do not forget to set the Risk Levels of the ABAP Unit Class ( Risk Level ) appropriately when the test fixture is to make high-risk changes to the system, such as creating test data in the persistence layer.