Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
Showing results for 
Search instead for 
Did you mean: 
Former Member

Perhaps you know some of the trademark slogans of test-driven development, like 'Red. Green. Refactor' or 'Test a little, implement a little, reflect a little.'

In TDD, reduced to its basic work cycle, you write a unit test and run it. The test fails because the code under test is not implemented.  So you implement the code under test such that the unit test is green. Then you repeat the cycle until eventually the method under test and the entire class are completely developed, completely documented, and completely testable (using all of the unit tests you have developed along the way).

As a purely practical matter, how do you translate the TDD work cycle into ABAP development with ABAP Unit in the ABAP Workbench?  This weblog shows what ABAP TDD in the Workbench in NetWeaver Release 7.01 (7.0 EHP1) might look like.

We aren't talking rocket science here, of course. But the idea is to save you some time and fumbling around the first time that you try to practice TDD in the Workbench.

If you don't know ABAP Unit, then you won't get very far with TDD until you know a little about it.  Here, you'll find a sample ABAP Unit implementation in the NetWeaver 7.0 documentation:

So what does practical TDD look like in the ABAP Workbench? We'll talk only about classes (you do develop only classes nowadays, right?  See ABAP Programming Guidelines, actually - Modern ABAP Programming - by Horst Keller and Wolf Hagen Thümmel.)

So, you create a brand new class in SE80 in the ABAP Class Builder. What next?

    1. Ideally, you would like to write your first ABAP Unit test method and then go from there.Here, we have to adapt TDD to the Workbench.  In NetWeaver 7.01 (7.0 EHP1), you'll first need to define the signature of a method to test.  There is no way to create an ABAP Unit include without a production method as a starting point. (This is fixed in EHP2.)

      So go ahead and define the signature of the first method in your class under test.  (You may want to keep on defining production method signatures first, since you can't define a method via forward navigation from a test method.)

    2. Create the ABAP Unit include by choosing Utilities -> Test Class Generation. You'll see the little dialog shown below. You can tell ABAP Unit to generate test methods for the production methods you specify. Choose all of the options - you'll save yourself some typing.

You'll find yourself in the class editor, ready to define your ABAP Unit test method.

After this initial create-include step, you can edit your ABAP Unit class by choosing Goto -> Local Test Classes.  Or, if you are lazy, you can call Test Class Generation again and tell it to create a test method for whatever new production method you have just defined.

    1. Implement your test class. Usually, ABAP Unit test classes are local classes in the class pool of a global class.

What is an ABAP Unit test class?  Simply a class definition with the FOR TESTING option, together with method definitions that also have the FOR TESTING option. And then method implementations with calls to CL_AUNIT_ASSERT to evaluate the tests.

CLASS abap_unit_testclass
"#AU duration short
"#AU risk level harmless
METHODS test_toggle_value FOR TESTING.
DATA m_ref TYPE REF TO cl_class_under_test.
METHODS setup.      "Set up a fixture
METHODS teardown. "Clear away a fixture.

ENDCLASS.                    "abap_unit_testclass DEFINITION

CLASS abap_unit_testclass IMPLEMENTATION.
METHOD setup.
ENDMETHOD.                   "setup

METHOD test_toggle_value.
DATA: test_value TYPE i.

m_ref->toggle_value( CHANGING toggle_value = test_value ).
cl_aunit_assert=>assert_equals( exp = 1 act = test_value ).

m_ref->toggle_value( CHANGING toggle_value = test_value ).
cl_aunit_assert=>assert_equals( exp = 0 act = test_value ).

ENDMETHOD.                    "test_toggle_value
ENDCLASS.                    "abap_unit_testclass IMPLEMENTATION

    1. The big moment has arrived. Activate the entire class. Then choose Class -> Unit Test or the appropriate entry from the class context menu. Or just Local Test Classes -> Unit Test from the test class editor to run the ABAP Unit tests. Of course, your empty TOGGLE_VALUE method will fail the unit test miserably, and ABAP Unit will branch to a results display like this one.

With F3 you can leave the ABAP Unit results display and return to the Class Builder.  Or you can jump from the ABAP Unit results display to the test method and from there to the production source code. There, you can implement TOGGLE_VALUE such that it passes the ABAP Unit test. And then you can start over, casting the next requirement on the method or the class in an ABAP Unit test method.

Coming up: Managing Dependencies for ABAP Unit testing