Ansicht
Dokumentation

ABAPMETHODS_TESTING - METHODS TESTING

ABAPMETHODS_TESTING - METHODS TESTING

SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3up   PERFORM Short Reference  
This documentation is copyright by SAP AG.
SAP E-Book

METHODS, FOR TESTING

Short Reference



METHODS meth $[ABSTRACT$|FINAL$]
             $[ AMDP OPTIONS $[READ-ONLY$] $[CDS SESSION CLIENT clnt$|CURRENT$]$]
  FOR TESTING
  $[RAISING exc1$|RESUMABLE(exc1) exc2$|RESUMABLE(exc2) ...$].


Effect

This statement is only possible in a test class. It declares a test method that is called as a single test during a test run. A test can be programmed in the method implementation. To check the test assumptions, static methods from the class CL_ABAP_UNIT_ASSERT are used, such as:

  • ASSERT_EQUALS
  • ASSERT_BOUND
  • ASSERT_NOT_BOUND
  • ASSERT_INITIAL
  • ASSERT_NOT_INITIAL
  • ASSERT_SUBRC
  • FAIL

The data is evaluated using the ABAP Unit Framework.

The same applies to the additions ABSTRACT, FINAL, and RAISING as to the general instance methods. Furthermore, the addition AMDP OPTIONS can be specified for test methods, since these methods can also be implemented as AMDP methods.

Test methods are usually used to call units of production code and to check the result. Production code units that are called from a test method must be exited regularly or by a dedicated statement like RETURN. The statements LEAVE PROGRAM LEAVE TO TRANSACTION and SUBMIT without AND RETURN that exit the complete program are not allowed in production code called during a unit test.

Notes

  • Test methods should be private or protected if the methods are inherited. Since test classes implicitly offer friendship to the test driver in the runtime framework, the driver can call them. Test methods only need to be public in rare cases where a test executes tests from other test classes.
  • When a test method is executed, the same applies to resumable exceptions as to all other methods. If processing can be resumed successfully, the interrupted test can be resumed.
  • The special methods setup, teardown, class_setup, and class_teardown of the fixture are not test methods and the addition FOR TESTING cannot be used for this methods.
  • The special ABAP Doc comment "! @testing.can be written in front of the declaration of a test method in order to define a test relation for that method. This allows repository objects to be tested that do no support test classes themselves.
  • To prevent inaccurate test results, the rule that no system fields are passed as actual parameters must be observed, especially for the ASSERT methods of the class CL_ABAP_UNIT_ASSERT. The special method ASSERT_SUBRC is only used for checking the return value.

Example

Definition of a test class mytest with a test method mytest that checks the value of the text attribute after the method set_text_to_x of the class myclass has been called. In this example, the ABAP Unit test reports an error since the value "X" is expected instead of "U".

* Productive classes

CLASS myclass DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA text TYPE string.
    CLASS-METHODS set_text_to_x.
ENDCLASS.

CLASS myclass IMPLEMENTATION.
  METHOD set_text_to_x.
    text = 'U'.
  ENDMETHOD.
ENDCLASS.

* Test classes

CLASS mytest DEFINITION FOR TESTING RISK LEVEL HARMLESS.
  PRIVATE SECTION.
    METHODS mytest FOR TESTING.
ENDCLASS.

CLASS mytest IMPLEMENTATION.
  METHOD mytest.
    myclass=>set_text_to_x( ).
    cl_abap_unit_assert=>assert_equals( act = myclass=>text
                                        exp = 'X' ).
  ENDMETHOD.
ENDCLASS.









BAL_S_LOG - Application Log: Log header data   BAL_S_LOG - Application Log: Log header data  
This documentation is copyright by SAP AG.

Length: 6525 Date: 20240329 Time: 130747     sap01-206 ( 81 ms )