Skip to Content

This is part 2 of  a weblog series on ABAP Unit. If you are interested, you find the other weblogs of the series here: A Spotlight on ABAP Unit Part 1A Spotlight on ABAP Unit Part 3, A Spotlight on ABAP Unit Part 4, A Spotlight on ABAP Unit Part 5

CLASS test_flights IMPLEMENTATION.

  METHOD add_flight.

    DATA:      l_act TYPE i,

           l_exp TYPE i.

cut TYPE REF TO cl_te4h_aunit_flights.

  •     ” some predefined flights:

      flight1 TYPE cl_te4h_aunit_flights=>ty_my_sflight.

      • The other definition are omitted here.

  •   predefine some flights:

    flight1-carrid = ‘AA’.

    flight1-connid = ‘0017’.

    flight1-fldate = ‘20040101’.

    flight1-price = ‘1000’.

    flight2-carrid = ‘UA’.

    flight2-connid = ‘0400’.

    flight2-fldate = ‘20040301’.

    flight2-price = ‘2000’.

    flight3-carrid = ‘LH’.

    flight3-connid = ‘0017’.

    flight3-fldate = ‘20040101’.

    flight3-price = ‘300’.

      • extra flights for test

    flight4-carrid = ‘AL’.

    flight4-connid = ‘0017’.

    flight4-fldate = ‘20040501’.

    flight4-price = ‘300’.

  •   setup class under test:

    CREATE OBJECT cut.

  •   add first 3 flight for our tests

    cut->add_flight( flight1 ).

    cut->add_flight( flight2 ).

    cut->add_flight( flight3 ).

    n_setup_flights = 3.

So much for the preparations for our test. Now the real test:

Surely, you have no trouble understanding what is happening in our test method: First, we define two variables for the expected and the actual number of flights in the internal table IT_Flights, one variable for an instance of the class we want to test and four structures of the line type of IT_Flights. We fill these four structures, create an instance of the production class we test and add three structures to the internal table by using the method add_flight of this class.

In the same manner as in our first example we perform the test in the assert_equals method. In our example we add a fourth structure to the internal table and test if the number of lines in the table has increased by one as it should be if the portion of code under test worked properly. Remember that after the modification of the program it is no longer possible to add a flight with the same price as an already existing flight to the internal table. Flight number 4 has the same price as flight number 3, and so this flight will not be added to the internal table. If the assert condition is violated, an error is raised which is shown by the ABAP Unit Tool after the test is run.

h3. Still Skeptic about ABAP Unit?

Again, the more skeptic-minded among you might not be really convinced. They might suppose a trick: Why did we test by sheer accident just the method which was affected by the side effect with just the right test data? Is this not a very improbable coincidence?

As I will show you in the next part of this flash light on ABAP Unit, it is of course not the mere writing of ABAP Unit tests that does the job all by itself. Obviously, tests have to be cleverly devised according to certain principles. This is what I will tell you next time: a little bit of theory on which tests with ABAP Unit should be based.

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Hi,
    I’m searching for more examples of how to use and implement test classes for ABAP Unit tests.
    In the documentation I found the following recommendation:
    It is recommended that you place all test classes in a program below the production code as this will provide the test classes with access to all data and procedures of the program.

    Actually it would be very helpful how to do this for the following use cases:

    1. Production code is a function module: where should I put the test class? Would the TOP include of the function group be the right place?

    2. Production code is a class: where to put the test class and how does it work?

    3. Production code is a method: where to put the test class?

    Is there any good tutorial to answer these questions, if possible under NW2004s?

    Thanks & regards,
    Michael

    (0) 
  2. Megan Flores
    Most of my ABAP work involved FRICE development. While developing reports, I am interested in knowing the status or return codes and the result of many small subroutines that I create as pieces of logic. How do I apply ABAP Unit testing to such a scenario. I would think this applies to most Abappers as designing reports is bread and butter work for us.

    Consider this simple scnerio:

    Extract from database table
    Routine1
    Routine2
    Routine3
    Display the data in report

    Now I want to use Unit to find out the contents of the db table extract and the ret code. And then I want to know the contents coming out of each of my routine. Would I have to replicate all this code in the test class that will reside after all the production code? And then call methods in CL_AUNIT* classes after each subroutine to determine the results?

    (0) 

Leave a Reply