12

I would like to implement ABAP Unit tests in my ABAP programs, but my first report is a classic ABAP report, no OO classes at all.

Is this even possible? Or ABAP Unit intended solely on OO classes?

Can I use it with subroutines?

PERFORM get_date_range using sy-date changing lv_fromdate lv_todate.

P.S. I'm a long time Java developer still learning ABAP.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
Davidson
  • 1,064
  • 3
  • 20
  • 35

6 Answers6

9

Here's an example report with unit tests:

report ztest.

end-of-selection.

  data number type i value 10.
  perform write_value using number.
  perform add_5 changing number.
  perform write_value using number.
  perform subtract_2 changing number.
  perform write_value using number.


form add_5 changing x type i.
  x = x + 5.
endform.

form subtract_2 changing x type i.
  x = x - 2.
endform.

form write_value using x type i.
  data x_str type string.
  x_str = x.
  condense x_str.
  write: / x_str.
endform.

class lcl_test definition for testing duration short risk level harmless.
  public section.
  protected section.
    methods add_5 for testing.
    methods subtract_2 for testing.
  private section.
    methods setup.
endclass.

class lcl_test implementation.
  method add_5.
    data number type i.
    number = 5.
    perform add_5 changing number.
    cl_aunit_assert=>assert_equals( act = number exp = 10 ).
    number = 20.
    perform add_5 changing number.
    cl_aunit_assert=>assert_equals( act = number exp = 25 ).
  endmethod.
  method subtract_2.
    data number type i.
    number = 5.
    perform subtract_2 changing number.
    cl_aunit_assert=>assert_equals( act = number exp = 3 ).
    number = 20.
    perform subtract_2 changing number.
    cl_aunit_assert=>assert_equals( act = number exp = 18 ).
  endmethod.
  method setup.
  endmethod.
endclass.
René
  • 2,912
  • 1
  • 28
  • 46
  • 2
    Thank you for the additional examples. I very appreciate the effort. And, yes, you did understand the question. – Davidson Oct 08 '12 at 14:07
7

I am not sure whether I understood the question correctly, but you can most certainly include unit tests in ABAP programs, e.g. report programs. Include the below code in a report program, then compile it.

Afterward, when you go to the Object List (Ctrl+Shift+F5 to show), you can right-click on your program, then choose Execute -> Unit Tests from the menu.

The important part is that unit tests are marked as FOR TESTING and have at least one method marked FOR TESTING as well. The RISK LEVEL addition will also determine whether, according to the system settings, a test is allowed to run. (Hit F1 on the keyword in the editor to read more).

* The following defines a unit test class
class unit_tests definition for testing risk level harmless.
  public section.
    methods: test_query for testing.
endclass.

class unit_tests implementation.
  method test_query.
    data: lv_result type string.
    perform execute_query_b using '123' changing lv_result.
    assert lv_result = 'Expected_value'.
  endmethod.
endclass.

* Here is a subroutine in our program we wish to test
form execute_query_b using a changing res.
  res = 'Expected_value'.
endform.
mydoghasworms
  • 18,233
  • 11
  • 61
  • 95
5

You could very well in your test class/method use

PERFORM <form> IN PROGRAM <prog>

And then validate the results you get back.

Edit:

Furthermore, the SAP help states this :
Creating ABAP Unit Tests
ABAP Unit tests are implemented in the form of test methods in local test classes in ABAP programs and the expected results are checked using the static methods of the auxiliary class CL_AUNIT_ASSERT.

Which validates the point that tests for ABAP programs should be local test classes as per some below answers. One can still use PERFORM <form> IN PROGRAM <prog> but I would venture it is a better approach to have the tests locally.

T.

tomdemuyt
  • 4,572
  • 2
  • 31
  • 60
  • ahh... I didn't now about that syntax 'IN PROGRAM'.... the reason I asked the question is because I am so new the ABAP way. There is no appetite for Abap Unit testing in my shop. It's just a personal thing with me to use TDD techniques, and get Abap Unit up and running in the code I write. – Davidson Oct 05 '12 at 21:18
  • If you change the program name the unit test will fail, so no problem there. – Esti Oct 06 '12 at 00:58
  • 4
    When your test class is locally defined and implemented in the report (at the end of it or in a separate include), you can call PERFORM directly from it without the need for IN PROGRAM (FORM routines are global in a program). You can also write test classes for function modules by adding a local test class to the function group. – Philipp Oct 08 '12 at 09:13
  • Esti thanks true and that's why I would consider this as a unit test. – fabiopagoti Oct 08 '12 at 10:30
1

In ABAP programming it will allow the developer to do the unit testing.

ABAP also contains MACRO's concept as like in C-programming, but in ABAP MACROS does not allows unit testing.

REPORT ZDEMO_INTERNALTABLES.

TYPES : BEGIN OF ty_scarr,

        carrid TYPE scarr-carrid,
        carrname TYPE scarr-carrname,
        END OF ty_scarr.

DATA : it_scarr TYPE STANDARD TABLE OF ty_scarr,

       wa_scarr TYPE ty_scarr.
      PERFORM SA .

*&---------------------------------------------------------------------*
*&      Form  SA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*

FORM SA .

  wa_scarr-carrid = 'AA'.
        wa_scarr-carrname = 'American airlines'.
        insert wa_scarr into table it_scarr .

         wa_scarr-carrid = 'df'.
        wa_scarr-carrname = 'xmy demy airlines'.
        insert wa_scarr into table it_scarr.

         wa_scarr-carrid = 'AC'.
        wa_scarr-carrname = 'AIRLINES'.
        APPEND WA_SCARR TO IT_SCARR.

         wa_scarr-carrid = 'AD'.
        wa_scarr-carrname = 'American airlines'.
        insert wa_scarr into table it_scarr.


        if SY-SUBRC = 0.
          ENDIF.
ENDFORM.                    " SA
S.I.
  • 3,250
  • 12
  • 48
  • 77
Surya Meda
  • 21
  • 2
0

SAP has official recommendations where to create test classes, according to which

Create ABAP Unit local classes and test methods at the end of the ABAP program. There is currently no separate ABAP include in ABAP programs and reports for ABAP Unit tests

So you should simply add the local test class at the end of the report, like was suggested by Rene.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
0

Program <mainprogram_name> with or, you can write Macro's with Define <your_macro> end definition to reiterate a process. You can call a class method:

class=>method(exporting = something 
importing = others)

Or you can create subrutines in your main program. And so on

ouflak
  • 2,458
  • 10
  • 44
  • 49
MrSamael
  • 5
  • 2