Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP Unit, how to use

Former Member
0 Kudos

Hi,

I created a small example of testing using ABAP Unit shown below.

But i get a syntax error in German, i translated to English and this is what i get

The reference to a test class (marking with FOR TESTING) is only in test classes possible

How do i resolve this?

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.

PRIVATE SECTION.

METHODS mytest FOR TESTING.

ENDCLASS.

CLASS mytest IMPLEMENTATION .

METHOD mytest .

myclass=>set_text_to_x( ).

cl_aunit_assert=>assert_equals( act = myclass=>text

exp = 'X'

msg = 'failed').

ENDMETHOD.

ENDCLASS.

DATA my_ref TYPE REF TO mytest.

CREATE OBJECT my_ref type mytest.

call METHOD my_ref->mytest.

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

You're almost there. You don't need to create a object reference to your ABAP Unit Test class. To run the ABAP Unit test, you need to do the Program > Test > Unit Test


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.
PRIVATE SECTION.
METHODS mytest FOR TESTING.
ENDCLASS.

CLASS mytest IMPLEMENTATION .
METHOD mytest .
myclass=>set_text_to_x( ).
cl_aunit_assert=>assert_equals( act = myclass=>text
exp = 'X'
msg = 'failed').
ENDMETHOD.

ENDCLASS.

Check thread

Regards,

Naimesh Patel

4 REPLIES 4

naimesh_patel
Active Contributor
0 Kudos

You're almost there. You don't need to create a object reference to your ABAP Unit Test class. To run the ABAP Unit test, you need to do the Program > Test > Unit Test


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.
PRIVATE SECTION.
METHODS mytest FOR TESTING.
ENDCLASS.

CLASS mytest IMPLEMENTATION .
METHOD mytest .
myclass=>set_text_to_x( ).
cl_aunit_assert=>assert_equals( act = myclass=>text
exp = 'X'
msg = 'failed').
ENDMETHOD.

ENDCLASS.

Check thread

Regards,

Naimesh Patel

0 Kudos

Thanks Naimesh,

but in the results all i get is "No execution as actual risk level is too high"

I tried giving the correct value X so that the test passes but i get the same thing. Is there some setting problem?

0 Kudos

Hi,

I added

CLASS mytest DEFINITION FOR TESTING. "#AU Risk_Level Harmless

it worked fine.

Thanks for ur inputs.

uwe_schieferstein
Active Contributor
0 Kudos

Hello

Do not miss out the 5-part blog series by Thomas Weiss about ABAP Unit Testing:

[ABAP Unit Testing|https://www.sdn.sap.com/irj/sdn/abap?rid=/webcontent/uuid/e08bec4e-8c57-2910-94a8-9e94caa20bee#section26 [original link is broken]]

Regards

Uwe