sapdev logo background
sapdev logo sapdev logo
Comments

ABAP METHODS TESTING Statement syntax, information and example SAP source code



Return to Statement index



METHODS - FOR TESTING

Short Reference

ABAP Syntax_6 METHODS meth [ABSTRACT|FINAL]
FOR TESTING
[RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...].

What does it do? This statement is only possible in a test class . It declares a test method that is called during 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
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 .

Latest 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 environment, the driver can call them. Test methods only need to be public in rare cases where a test executes tests from other test classes.
At present, all instance methods in global test classes are automatically test methods, that is the addition FOR TESTING is added implicitly.
When a test method is executed, the same applies for resumable exceptions as for all other methods. If processing can be resumed successfully, the interrupted test can be resumed.

Example ABAP Coding 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.
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved




METHODS_REDEFINITION
METHOD_KERNEL_MODULE_INTERNAL




comments powered by Disqus