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: 

Unit testing for classes

Former Member
0 Kudos

Hello there,

May I know how should I start off on unit-testing on classes? Is there any automated script to be genertated available somewhere or do I have to start everything from scratch? Is there any document on the internet that I could refer to?

Thanks a lot!

Regards,

Anyi

1 ACCEPTED SOLUTION

uwe_schieferstein
Active Contributor
0 Kudos

Hello Anyi

The link provided by the previous SDN user does not hit your question.

Please check the WebLog series by Thomas Weiss on

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

Now I would like to give you an example. Let us assume you have a class (ZCL_MYCLASS) with an instance method DO_CALCULATION. This method does the calculation on instance attributes and stores the result in an instance attribute MD_RESULT.

How would you write ABAP Unit Tests for this method?

The testing class is a private class within your global class thus it has access to all attributes (including private ones) of the class being tested. Like in Java Unit Testing we have two pre-defined methods:

- SETUP which is started prior to any test method

- TEARDOWN wich is started after the test method have been finished

CLASS test DEFINITION FOR TESTING.
  PRIVATE SECTION.
        DATA:
          md_result_expected   TYPE i,
          mo_myclass           TYPE REF TO zcl_myclass.

        METHODS:
          setup,
          test_do_calculation FOR TESTING.
ENDCLASS.                   
CLASS test IMPLEMENTATION.
    METHOD setup.
*      Create an instance for testing
       create object mo_myclass.
*      ... Here you set the attributes for the calculation.
*      The result should be equal to 100.

    ENDMETHOD.

    METHOD test_do_calculation.
*       Call the method being tested
        CALL METHOD lo_myclass->do_calculation.
        cl_aunit_assert=>assert_equals( 
            act = mo_myclass->md_result
            exp = ld_result_expected
            msg = 'Calculation is wrong').
    ENDMETHOD.                    
ENDCLASS.

How many testing classes do you need? One? Two? Many?

This depends on how many different "starting points" do you need for your tests. All tests that have the same setting at the beginning (i.e. can use the starting conditions provided by the SETUP method) are grouped within the same testing class.

I hope I could give you some insight into this exiting subject which fills an important gap between the low-level developer test and the integration tests usually done by key-users.

Regards

Uwe

4 REPLIES 4

Former Member
0 Kudos

Former Member
0 Kudos

Hello,

Thanks for the quick reply.

However, what I really want is to have some kind of code that does the unit testing, not simply determining whether the programs runs right by simply plug in some values manually.

Any other suggestions?

Thanks a lot!

Regards,

Anyi

uwe_schieferstein
Active Contributor
0 Kudos

Hello Anyi

The link provided by the previous SDN user does not hit your question.

Please check the WebLog series by Thomas Weiss on

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

Now I would like to give you an example. Let us assume you have a class (ZCL_MYCLASS) with an instance method DO_CALCULATION. This method does the calculation on instance attributes and stores the result in an instance attribute MD_RESULT.

How would you write ABAP Unit Tests for this method?

The testing class is a private class within your global class thus it has access to all attributes (including private ones) of the class being tested. Like in Java Unit Testing we have two pre-defined methods:

- SETUP which is started prior to any test method

- TEARDOWN wich is started after the test method have been finished

CLASS test DEFINITION FOR TESTING.
  PRIVATE SECTION.
        DATA:
          md_result_expected   TYPE i,
          mo_myclass           TYPE REF TO zcl_myclass.

        METHODS:
          setup,
          test_do_calculation FOR TESTING.
ENDCLASS.                   
CLASS test IMPLEMENTATION.
    METHOD setup.
*      Create an instance for testing
       create object mo_myclass.
*      ... Here you set the attributes for the calculation.
*      The result should be equal to 100.

    ENDMETHOD.

    METHOD test_do_calculation.
*       Call the method being tested
        CALL METHOD lo_myclass->do_calculation.
        cl_aunit_assert=>assert_equals( 
            act = mo_myclass->md_result
            exp = ld_result_expected
            msg = 'Calculation is wrong').
    ENDMETHOD.                    
ENDCLASS.

How many testing classes do you need? One? Two? Many?

This depends on how many different "starting points" do you need for your tests. All tests that have the same setting at the beginning (i.e. can use the starting conditions provided by the SETUP method) are grouped within the same testing class.

I hope I could give you some insight into this exiting subject which fills an important gap between the low-level developer test and the integration tests usually done by key-users.

Regards

Uwe

uwe_schieferstein
Active Contributor
0 Kudos

Hello Anyi

I would like to add a remark by Klaus Ziegler from another thread on the same topic:

<i>"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."</i>

Regards

Uwe