Show TOC

ABAP Unit and the Test-Driven Development CycleLocate this document in the navigation structure

Use

How can you use ABAP Unit for test-driven ABAP development? Here is how the TDD development cycle translates into development in the ABAP Workbench.

Development Cycle

In orthodox TDD, you write a test method and run it. The test method fails, because the code under test is not implemented.

You implement the code under test such that the test is green and repeat the cycle, until the method under test and eventually the entire class are fully developed and also fully testable.

The motto is 'Test a little, implement a little, reflect a little,' or 'Red. Green. Refactor.'

You can use ABAP Unit with ABAP classes, function modules, and executable programs. We restrict ourselves here to ABAP classes; the other use cases are similar.

  1. When you start developing a class, create an include for ABAP Unit test classes.

  2. Implement the test class or classes directly in the source code-based editor. Usually, ABAP Unit test classes are local classes in the class pool of a global class.

    Do your unit tests need a test fixture? You can set up and tear down the fixture with special methods that ABAP Unit runs automatically.

    ABAP Unit error messages include the name of the test method and test class. You should therefore choose names that help describe the cause of an error. Here are some suggested naming conventions.

  3. When you specify the signature of a method under test in one of your test methods, then define the method under test in the Class Builder.

    At least the signature of the method under test must be defined and activated before you can activate an ABAP Unit test method.

    Currently, you must define the method by hand. You cannot generate the signature of the code under test from the test method.

  4. Activate your class (and all includes).

  5. Run the ABAP Unit tests to drive the implementation work that you do in the code under test.

    Here are the easiest ways to run the ABAP Unit tests in the code on which you are working:

    • Enter the key combination CTRL-SHIFT-F10, or

    • Choose Start of the navigation path <Object Menu> Next navigation step Run Next navigation step Unit tests End of the navigation path

      You can also run your unit tests with measurement of their code coverage: Start of the navigation path <Object Menu> Next navigation step Run Next navigation step Unit Tests With Next navigation step Coverage Measurement End of the navigation path.

    If you have ABAP Unit errors, then repeat from step 2. Otherwise, design the next set of unit tests.

Note

Are the methods under test already created?

Then you can use the ABAP Unit Wizard to generate ready-to-implement definitions of test classes and methods from the methods under test. The wizard can add new test methods incrementally, as you add new methods to test.

You can also use the wizard to create ABAP Unit test classes for function modules in a function group. The wizard is not available for executable programs.

After development is completed, you can of course continue to use ABAP Unit tests as regression tests. If you change or enhance the tested code, then you can be sure that the changed code is functioning correctly and that existing functions are not impaired.

Automated mass testing with the Code Inspector is also available.

You can also set up your own personal automated execution of ABAP Unit tests. In transaction SE80, open the context menu for one or more selected objects and choose Start of the navigation path Run Next navigation step Unit Tests With Next navigation step Scheduling End of the navigation path. You can display the unit test results, with or without code coverage measurement. Or you can run the unit tests in the background system and have the results mailed to you.