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: 

Best way to incorporate ABAP unit tests into classes

Former Member
0 Kudos

I am starting to incorporate ABAP Unit tests into my coding. I would like to know the best way to do this with ABAP objects.

If I create a class, where do I write the unit test code? I can understand where to do this for a report type program, writing a local ABAP Unit class at the end of the program, but I am not sure how to write the ABAP Unit class code within a normal class.

Is a better solution to create a class of type "Test Class (ABAP Unit)" in SE24, and then create my actual class as an attribute of this unit class, and test the class this way?

BR,

Tony.

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

You can write the ABAP Unit code in the Local Types and Implementation Section up to release ABAP 640. Use local types section to have a definition of your local test class. Use the implementation section to actually implement the test class.

When you want to access the private attributes / methods of the main class in your local test class, you need to declare the local class as a local friend of the main class. This definition you can achieve in the Local Types Section.


  CLASS  lcl_test_sum    DEFINITION DEFERRED.

  CLASS zcl_sum DEFINITION LOCAL FRIENDS
    lcl_test_process_address.

CLASS lcl_test_sum   DEFINITION FOR TESTING.
* method definitions...
ENDCLASS.

In the implementation section:


CLASS lcl_test_sum   IMPLEMENTATION.
 * methods
ENDCLASS.

From release 700 onwards, SAP introduced separated section in Class builder for Test classes. You can find it Go To > Local Test Classes. Here you can write both definition and implemenation of the local test class.

Class builder also have the Test class wizard, Utilities > Test Class Generation

When you want to use the same test / test fixture in more than one main class, you can use the Global Test Class (can be created in SE24 from release 700).

This Global Test class must be Abstract

All methods in this class would be type "For Testing" except the fixture methods.

To use this test class in your main class, you still need to create a local test class which is inherited from the Global Test class. This local test class code will go in the Local Test Class section.

Regards,

Naiemsh Patel

5 REPLIES 5

Sandra_Rossi
Active Contributor
0 Kudos

You can create local classes within a global class. I don't know if it works for abap unit (I guess it does).

>

> ... and then create my actual class as an attribute of this unit class ...

I don't understand what you mean.

alejandro_bindi
Active Contributor
0 Kudos

I found out that you can create them as internal classes (Goto > Local Class Definitions / Types > Local Class Definitions in SE24). If you then run the tests from the menu Class > Unit Test, the internal class is used (provided of course you added the FOR TESTING statement in class and methods declaration).

I've discovered this myself though, and found no official documentation on the subject so I can't confirm if this is the best approach.

Regards

naimesh_patel
Active Contributor
0 Kudos

You can write the ABAP Unit code in the Local Types and Implementation Section up to release ABAP 640. Use local types section to have a definition of your local test class. Use the implementation section to actually implement the test class.

When you want to access the private attributes / methods of the main class in your local test class, you need to declare the local class as a local friend of the main class. This definition you can achieve in the Local Types Section.


  CLASS  lcl_test_sum    DEFINITION DEFERRED.

  CLASS zcl_sum DEFINITION LOCAL FRIENDS
    lcl_test_process_address.

CLASS lcl_test_sum   DEFINITION FOR TESTING.
* method definitions...
ENDCLASS.

In the implementation section:


CLASS lcl_test_sum   IMPLEMENTATION.
 * methods
ENDCLASS.

From release 700 onwards, SAP introduced separated section in Class builder for Test classes. You can find it Go To > Local Test Classes. Here you can write both definition and implemenation of the local test class.

Class builder also have the Test class wizard, Utilities > Test Class Generation

When you want to use the same test / test fixture in more than one main class, you can use the Global Test Class (can be created in SE24 from release 700).

This Global Test class must be Abstract

All methods in this class would be type "For Testing" except the fixture methods.

To use this test class in your main class, you still need to create a local test class which is inherited from the Global Test class. This local test class code will go in the Local Test Class section.

Regards,

Naiemsh Patel

0 Kudos

Thank you! Very useful and solves my problems!

BR,

Tony.

Edited by: Anthony Bateman on Aug 28, 2009 12:28 PM

uwe_schieferstein
Active Contributor
0 Kudos

Hello Anthony

Please check the menu functions in SE24 (have no current access in the moment to describe the path).

There you find a function to automatically create local test classes for the methods of your global class.

Regards

Uwe