Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  program editing →  Testing and Checking Programs →  ABAP Unit 

CLASS - FOR TESTING

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


CLASS class DEFINITION FOR TESTING [RISK LEVEL {CRITICAL|DANGEROUS|HARMLESS}] 
                                   [DURATION   {SHORT|MEDIUM|LONG}].

Extras

1. ... RISK LEVEL {CRITICAL|DANGEROUS|HARMLESS}

2. ... DURATION {SHORT|MEDIUM|LONG}

Effect

The FOR TESTING addition is used to defined a class as a test class for the ABAP Unit tool. Test classes can contain test methods that are called as individual tests during a test run.

A test class is usually a local class. The source code in a test class is not part of the production code of the program and is not generated in production systems (controlled using the profile parameter abap/test_generation) . A test class and its components cannot be addressed in the production code of the program therefore, only in other test classes. In particular, a subclass of a test class must be a test class itself and be declared using the FOR TESTING addition. The only exception to this rule is that test classes can be specified as friends in the definition of production classes to make it possible to test private components.

A local test class can contain special private methods that implement the fixture for the tests of the class. These methods have the following predefined names:

  • An instance method setup, which is executed before each individual test or before each execution of a test method.
  • An instance method teardown, which is executed after each individual test or after each execution of a test method.
  • A static method class_setup, which is executed once before all tests of the class.
  • A static method class_teardown, which is executed once after every test of the class.


Notes

  • Create test classes for class pools and function groups only in the test includes designed for them. In other programs, these tests should run at the end of the program.
  • In addition to the test methods and special methods for the fixture, a test class can also contain other components. These components can be used in a user's own test class only or in other test classes, depending on the visibility. In this way, help methods can be defined for tests, for example.
  • All components required for tests in the context of ABAP Unit should be defined exclusively in test classes. This ensures that they are not generated in production systems and cannot be addressed by production code. Alongside test classes with test methods, this also includes test dummies and other helper classes that do not contain test methods.
  • Test classes can inherit from any other classes, in particular from global classes. This allows general global tests to be implemented that can be used in local test classes.
  • Global test classes can be created for reusing complex test preparations. Global test classes should always be abstract and can only be used in local test classes. Currently, all instance methods of a global test class are automatically test methods.
  • In test classes, the addition PARTIALLY IMPLEMENTED of the statement INTERFACES can be specified, which allows interfaces to be implemented only partially. This is particularly useful in test doubles.
  • The addition FOR TESTING in a local class created in the area for local types of the class pool of a global test class. The addition is not needed in auxiliary classes for the global test class, since its property test class has an effect on the entire class pool with regard to its generation. In rare cases in which a global test class is to be tested itself, local test classes can be created as recommended for other global classes in the area local test classes.
  • The methods of the fixture are not test methods and the addition FOR TESTING cannot be specified for their declaration.
  • Global test classes can also contain AMDP methods as test methods, in which case they must include the appropriate tag interface IF_AMDP_MARKER_... for AMDP classes.

Addition 1

... RISK LEVEL {CRITICAL|DANGEROUS|HARMLESS}

Addition 2

... DURATION {SHORT|MEDIUM|LONG}

Effect

These additions assign test properties to a test class. RISK LEVEL defines the risk level for a test and DURATION the expected execution time. The test properties are checked during when the test is executed.

  • Tests whose risk level is higher than allowed in a system are not executed.
  • The behavior of tests that run longer than expected is determined by the run mode and the runtime monitor.


Notes

  • A risk level must be defined for SAP test classes.
  • The additions RISK LEVEL and DURATION replace the pseudo comments for test classes. Existing pseudo comments are not ignored, but should no longer be used.

Example

See the program DEMO_ABAP_UNIT.

  • A class demo represents the productive part of the source code. A method get_sum adds the values of the fields of a row of a database table and assigns the result to the private attribute sum.
  • The class test_demo is a test class for the class demo.
  • In the method setup, an object of demo is created and the database table is prepared with test data. Therefore, the addition DANGEROUS is specified for RISK LEVEL.
  • The method test_sum calls get_sum and compares the private attribute sum with the expected value. For this reason, test_demo must be a friend of the class demo.
  • In the method teardown, the test data is removed from the database table.
The test method is called when the module test of DEMO_ABAP_UNIT is executed and should not produce an error.