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 Test Problem

former_member82844
Participant
0 Kudos

Here we go again on questions regarding ABAPUnit. We are trying to use Abap unit within a Class object. All the examples we have seen have to do with Classes and Methods within a Report program.

What we have created:

Class: ZCL_MAIN

Type: General Class

Method: test_abapunit

Class: ZCL_MAIN_TEST

Class type: Abap Unit

Sub Class of ZCL_MAIN

Method: Test_abapunit_test

We try and run the Abap Unit Test from the Test Class and it says it is complete but does not execute our code. I know we are doing something wrong but are stumped as to what it is.

Glenn

9 REPLIES 9

uwe_schieferstein
Active Contributor
0 Kudos

Hello Glenn

Have a look at the following WebLog on

<a href="/people/thomas.weiss/blog/2004/12/17/a-spotlight-on-abap-unit-part-1 Unit Test</a>

Did you define your testing class (and methods for testing) like that?

CLASS test DEFINITION FOR TESTING.
  PRIVATE SECTION.
        METHODS test_minus_ten_percent FOR TESTING.
ENDCLASS.                   
CLASS test IMPLEMENTATION.
    METHOD test_minus_ten_percent.
        DATA: testprice type p value 200.
        PERFORM minus_ten_percent CHANGING testprice.
        cl_aunit_assert=>assert_equals( act = testprice exp = 180
        msg = 'ninety percent not calculated correctly').
    ENDMETHOD.                    
ENDCLASS.

Do not forget the FOR TESTING option.

Regards

Uwe

former_member82844
Participant
0 Kudos

The example you have given is for Report Based code. I am trying to run the ABAP unit within the Class Builder.

0 Kudos

Hello Glenn

Where is the difference whether you are testing report or class methods? In the SETUP( ) method you create your instance, set appropriate starting value that should yield defined results when calling methods of the class and then you call the methods and check the results using CL_AUNIT_ASSERT methods.

Regards

Uwe

0 Kudos

Hello Glenn

Let's assume we have a class with a static method MINUS_TEN_PERCENT. Now you define within the global class a private testing class like shown below.

CLASS test DEFINITION FOR TESTING.
  PRIVATE SECTION.
        METHODS test_minus_ten_percent FOR TESTING.
ENDCLASS.                   
CLASS test IMPLEMENTATION.
    METHOD test_minus_ten_percent.
        DATA: testprice type p value 200.
        CALL METHOD ZCL_MYCLASS=>minus_ten_percent CHANGING testprice.
        cl_aunit_assert=>assert_equals( act = testprice exp = 180
        msg = 'ninety percent not calculated correctly').
    ENDMETHOD.                    
ENDCLASS.

As already said, if you need to test instances then create them within the pre-defined method SETUP( ).

Regards

Uwe

0 Kudos

Hello Glenn,

as Uwe already told there is no big difference using ABAP Unit in reports or class-pools. In order to test a class-pool just add your local test class definition and implementation in the implementation include of the class-pool.

In release 7.00 (Netweaver 2004s) onwards there is also a wizard. When you enter the class builder in edit mode the menu of the basic screen offers you a generator option. This generator produces the basic skeleton of a test class.

Regards,

Klaus

0 Kudos

Klaus, Uwe, thanks for your explanations.

We have implemented some local test classes and it works fine. However, unfortunately, we cannot get the wizard Klaus mentions to start. Whenever one chooses <i>Goto->Local Test Classes</i> from the main menu it says something like 'class is not inactive' (!?) in the message bar and nothing happens (regardless of the activation state of the class).

Since writing the actual 'test-frame' as such is a mechanical and tedious work we would very much like to get this wizard to run. Do you have any suggestions?

Regards,

Sebastian Kamp

0 Kudos

Hello Sebastian,

may this is a usability issue. From my experience the navigation to the special test class include (7.00 onwards) issues such a message in case no test class include exists. The wizard however can be started at SE24 entry screen after one is in edit mode.

Kind Regards

klaus

Former Member
0 Kudos

Hi Glenn,

although Uwe and Klaus actually provided for the relevant information we too faced some difficulties at first understanding 'where' you actually put the test classes. So here are the mechanics:

Open SE80 or SE24 and implement your class ZCL_MAIN.

While this class is opened, choose <i>Goto->Class-local types->Local Class Definitions/Types</i> from the menu. An editor for local classes is opened on the right.

Type in your class definition ZCL_MAIN_TEST like this:


CLASS lcl_main_test DEFINITION FOR TESTING.
  "#AU Risk_Level Harmless
  PRIVATE SECTION.
    CONSTANTS: some_initial_test_value TYPE i VALUE 5.

    DATA: main_class TYPE REF TO zcl_main.

    METHODS setup     FOR TESTING.
    METHODS teardown  FOR TESTING.
    METHODS check_get_my_int FOR TESTING.
    METHODS check_.. FOR TESTING.
ENDCLASS.

This is btw a 'normal' local class, no subclass of cl_abap_unit.. whatsoever.

Go back (menu <i>Goto->Class Definition</i>).

Choose <i>Goto->Class-local types->local class implementations</i> from the menu.

Type in your class implementation for ZCL_MAIN_TEST, i.e. implement especially the setup()-method for the test fixture and the various test-methods.

Compile/activate your class ZCL_MAIN.

Run the test by choosing <i>Test->Unit Test</i> from the context menu of the class ZCL_MAIN.

Hope this helps, regards,

Sebastian

0 Kudos

>

> Open SE80 or SE24 and implement your class ZCL_MAIN.

> While this class is opened, choose <i>Goto->Class-local types->Local Class Definitions/Types</i> from the menu. An editor for local classes is opened on the right.

I followed the above menu path and put my test class there but it did not work. Then I moved my test class to menu path Goto->Local Test ClasseS and it worked.