Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use ABAP Unit tests in classic ABAP report?

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.

like image 905
Davidson Avatar asked Oct 05 '12 20:10

Davidson


People also ask

What is classical unit testing?

The classical school states that it's unit tests that need to be isolated from each other, not units. Also, a unit under test is a unit of behavior, not unit of code. Only shared dependencies should be replaced with test doubles.

Which library is used for unit testing?

To sum up, this is my list of 5 libraries every developer should be familiar with: JUnit, Mockito, Hamcrest, PowerMock, and Selenium. They allow for both unit testing and integration testing on the different levels of The Practical Test Pyramid.

Should devs write unit tests?

Yes, developers typically write unit tests. However, they are largely responsible for writing these tests to ensure that the code works – most developer tests are likely to cover happy-path and obvious negative cases.


1 Answers

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.
like image 164
René Avatar answered Sep 23 '22 14:09

René