Saturday 10 December 2016

ABAP TDD in NetWeaver 7.0 EHP2

I showed what the Test-Driven Development cycle might look like in the ABAP Workbench (SE80) in NetWeaver Release 7.01. Unfortunately, the TDD experience in that Release was marred by a hurdle right at the start of the process of implementing an ABAP class. That hurdle has been removed, and there are three other important improvements to ABAP Unit.

With NetWeaver 7.0 EHP2 (Release 7.02), the process has become a little bit more graceful. This weblog reviews what ABAP TDD looks like in practice in Release 7.02.  (Look for even more improvements in support for ABAP TDD in the Customer Engagement: Evaluation of the Pilot Version of ABAP in Eclipse.)

Start by assuming that you have just created a new, empty class in transaction SE80. You are looking at the form-based class builder, in change mode.

1. You can now practice TDD orthodoxy right from the start: You can create ABAP Unit test methods before defining any methods in your global class under test. This means that you can evolve the signatures of your production methods in the course of testing from your test methods. This was not possible in Release 7.01.From the form-based class editor, choose Goto > Local Definitions/Implementations > Local Test Classes.

The source code editor opens with the Local Test Classes include.

Note: The ABAP Unit wizard is still available. As in 7.01, you can define the signatures of the methods of your global class first and then generate the ABAP Unit test methods from the signatures. Call the ABAP Unit wizard with Utilities > Test Classes > Generate (or Utilities > Test Class Generation, if you are working in the source-code based class builder).

2. Type in the definition of your ABAP Unit test class.Do you remember the ugly old pseudo comments for specifying the risk level and duration of ABAP Unit tests in Release 7.01?  In 7.02, these are replaced by arguments of the ABAP CLASS statement. This also means that you can use code completion to help with the definition of your ABAP Unit class:

CLASS lct_test_it DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PRIVATE SECTION.
METHODS test_add_it_up FOR TESTING.
...

3. In your test methods, type in the call to a method-under-test as you think it should be defined.With a double-click on the method name, you can define it via forward navigation in the global class that you are testing.

  method test_add_it_up.

data result type i.

result = cut->add_it_up(  “double-click on add_it_up
addend_1 = 1          “to define the method in the
addend_2 = 1 ).       “class under test

endmethod.

Forward navigation unfortunately does not automatically fill in the parameters of your new method.

Even so, in 7.02 you can drive all of your development from your test methods, right from the start.

4. In your test method, assert the result that you want to see.  There is a new and improved standard class for this in Release 7.02, CL_ABAP_UNIT_ASSERT. Here’s an example:   cl_abap_unit_assert=>assert_equals( act = result exp = 2 ).

5. Run your ABAP Unit test(s). The same convenient mechanism exists as in 7.01. From the ABAP Unit source code editor, choose Local Test Classes > Unit Test.If the tests all run successfully, you see a success message in the status line of the SAP GUI.

Otherwise, the ABAP Unit Result Display opens. You can navigate to the test method and then farther to the method under test, so that you can correct its implementation.

Mass Testing with the ABAP Unit Browser

Running a whole lot of ABAP Unit tests at once may not strictly be part of the TDD process. But it sure is useful for regression testing and verifying that your new code is still okay after you have transported it to the integration or consolidation system.

The ABAP Unit Browser – new with Release 7.02 – helps you with mass testing, both in the development system and in the consolidation or quality-testing system.

Activate the ABAP Unit Browser by selecting it in transaction SE80, in Utilities > Settings, on the Workbench (General) tab.  Among other options, you can run all ABAP Unit tests in a package or in the programs of a particular user. You can also store ‘favorites’, or defined collections of ABAP programs.  See the standard documentation for more information on the ABAP Unit Browser.

No comments:

Post a Comment