ABAP Units – Test a Little, Implement a Little, Reflect a Little

ABAP Units – Test a Little, Implement a Little, Reflect a Little
Test Driven Development

Introduction

ABAP Units is a significant idea according to the viewpoint of testing which permits us as the engineer to test each piece of code as a free substance with various test situations, cases proficiently which ensures that all reason for testing is covered and there would be no issues when code is moved underway.

1.    Test Driven Development (TDD)   


Figure 1: Architecture of Test Driven Development

The proverb is ‘Test a bit, execute a bit, mirror a bit,” or ‘Red, Green, Refactor.’ In TDD, we compose a test strategy and run it. The test technique falls flat, on the grounds that the code under test isn’t carried out. We execute the code under test to such an extent that the test is green and rehash the cycle, until the strategy under test and in the end the whole class are completely evolved and furthermore completely testable.

ABAP Units is a piece of Test Driven Improvement Approach. In this approach the practical is intended to give all legitimate and invalid experiments or in short needs to give all experiments before hand and afterward the real code for creation is to be composed.

Features of ABAP Unit

ABAP Unit is essential for the test scene for ABAP programs. ABAP Unit permits us to carry out unit tests and to execute them both physically and consequently being developed server. ABAP Unit tests are techniques for uncommonly assigned ABAP classes. ABAP Unit is reasonable for test-driven improvement.

ABAP Unit is straightforwardly implanted into the ABAP improvement climate and into the ABAP runtime climate. In ABAP Unit tests are, test strategies in committed test classes. A test class might contain a few test strategies. Typically test classes are nearby classes inside the program under tests.

The main elements of ABAP Units for writing computer programs are:

  • The tests are modified in ABAP. We want not need to gain proficiency with any extra test script dialects.
  • The tests are created in the ABAP advancement climate. We want not need to gain proficiency with any extra connection point activity.
  • With the ABAP Unit Wizard, we can create Test Classes for classes (Class Pools) and Capability Gatherings. For other program objects, we can make test classes physically.
  • ABAP Unit test classes can be carried out in the tried advancement objects. This guarantees that the connection between unit test and tried code is clear. Furthermore, since unit tests are shipped with the code under test, they are accessible in all frameworks of the turn of events and test scene.
  • Anybody can execute ABAP unit tests. Since ABAP Unit Tests don’t have boundaries, you must have no master information to begin unit tests, regardless of whether you are curious about the ones you are running. .
  • The outcomes show makes it simple to assess and break down unit test blunders.
  • ABAP Unit incorporates ABAP’s capacities for estimating code inclusion. You can approve your ABAP Unit tests by really looking at their code inclusion. You can utilize the code inclusion showcases to find out where there are holes in your ABAP Unit tests – code that the tests don’t reach.
  • ABAP Unit tests are not executable in useful clients and can in this manner cause neither responsibility on the framework nor issues because of aftereffects. As a matter of fact, ABAP doesn’t create byte code for ABAP Unit test classes and techniques underway frameworks.

Evaluating Test result using ASSERT Class

Class CL_ABAP_UNIT_ASSERT is utilized for the assessment of the outcomes for the item. The class CL_ABAP_UNIT_ASSERT contains the accompanying static strategies for the check of test assumptions inside ABAP Unit test techniques:

ASSERT_EQUALS

Guarantee the balance of two information objects. Kindly note that the contentions are passed by reference. Objects are tried for personality as it were.

ASSERT_EQUALS_FLOAT

Ensure the equality of two decimal floats with a relative tolerance.

ASSERT_DIFFERS

Ensure the value of two data objects not to be equal.

ASSERT_BOUND

Ensure whether the reference of a reference variable is valid.

ASSERT_NOT_BOUND

Ensure whether the reference of a reference variable is invalid.

ASSERT_INITIAL

Ensure that a data object has its initial value.

ASSERT_NOT_INITIAL

Ensure that a data object does not have its initial value.

ASSERT_SUBRC

Requests specific value of SY-SUBRC.

ASSERT_TABLE_CONTAINS

Ensure that an internal table contains a certain line.

All the assert methods have the following mandatory importing parameter:

ACT (Type: Any) – The Actual object to be verified

EXP (Type: Any) – The Expected object to be verified

MSG (Type: CSEQUENCE) – Contains a more detailed description of the error (if applicable)

Cl_abap_unit_assert=>assert_equals(
 act = it_info
 exp = it_excp
msg = msg )

Calling ABAP Unit Assert Equals method

A basic model for this approach can be approval of material (matnr) and plant(werks) blend. For this situation the utilitarian needs to before hand give all substantial blends of material and plant mix for which code ought to be set off. This blends can be tried before hand itself utilizing this idea and that to autonomously.

Implementation Example of above case.

Definition of class containing logic which does validation for plant and material combination.

Implementing class fetching data from marc table


Class definition which would be used for implementing ABAP UNITS

Key points to be noted here: a) Key word Testing b) Risk level & Duration

Implementing class for ABAP UNITS

In the above class/preview we have give the conceivable blend which ought to be available and code ought to work.

Output

Case 1) Invalid Test Case

As should be visible in above depiction, utilizing the strategy asserts_equal of class cl_aunit_assert it gives detail clarification of where in table worth isn’t matching including the record of the table.

Case 2 : Valid Test Case

Displays test executed successfully, all required values are present. 

Key Points:

a) Key word Testing 

Whenever FOR TESTING follows after the class definition, it signifies that it is a class for abap units and it won’t get executed in production environment.

Syntax:

Class lcl_test  definition FOR TESTING  

b) Risk level  & Duration

Risk level is maintained in client settings and it would define the level of risk which is present in executing the test.

There are 3 levels of risk: 1) Harmless 2 ) Dangerous 3) Critical

Duration is the required time for execution of test case.

Types: 1) Short 2) Medium 3) Long

Use of Transaction SECATT in Abap Units.

SECATT is a t-code utilized for making scripts which can be utilized for robotizing monotonous interaction. In this t-code the client can make a content which can contain all the material and plant mixes which is utilized in model above which is then utilized as test information for our situation.

Use of ABAP Units using Global Classes:

Abap Units can likewise be utilized with worldwide classes for doing testing of the creation code.

The process remains similar which is as follows:

  • We initially make a worldwide class with techniques which would contain the expected creation codes.
  • When the worldwide class is made we can make a testing technique in the class which would contain a neighborhood class to do the execution for testing the strategies for class which is like idea of nearby classes.

Different Methods used in ABAP units:

Methods in ABAP units are referred to as Fixture Methods.

  • Setup Method: It is like instatement occasion of SAP. It’s an occasion technique which is called before execution of each test apparatus strategy in Abap Units.
  • Tear Down: It’s a strategy which is called after execution of the test installation techniques, which can be utilized to clear or revive factors.
  • Class Setup: The idea of this strategy is identical to class constructor idea in SAP. This technique is executed when the abap units classes are stacked without precedent for memory
  • Class Teardown: The idea of this strategy can be generally contrasted with end-of-determination occasion in SAP ABAP. This strategy is executed when the last experiment is executed in the class.

Test Data Container (TDC):

It’s a holder which is made utilizing tcode-SECATT which contains set of test information put away in variations which are utilized for testing the creation code.

Example of Global Class using SECATT transaction:

Maintain Data for testing (Done by Functional)
T-Code : SECATT

 

Make a boundary and boundary reference would be the Table Sort containing the fields for approval; for our situation matnr and werks.

Create a variant and then double click on the value tab where functional would create the test data.

Add the required matnr and werks combination and save the required data.

Make a worldwide class containing the fetch_data strategy which contains the necessary rationale which gets matnr and werks blends.

In ABAP workbench open the class and by right clicking, one can make new nearby test class by adhering to the necessary guidance and choosing the expected test technique alongside the strategy for which test technique should be made

In the neighborhood class utilizing the class cl_apl_ecatt_tdc_api we can get the case of are Test Information Compartment (TDC).

Utilizing the example we can get the variations put something aside for the expected trial of strategy which gives the test information.

We then, at that point, call the fetch_data strategy for worldwide class which contains the information got from creation code which is checked against variation anticipated information.

Assuming that there is contrast in information, we get the accompanying result.

ABAPers, we comprehend ABAP Unit Test is somewhat new idea utilized sparingly. However, assuming you begin utilizing it all the more regularly it is simply matter of time when you would begin adoring ABAP Units and entire ABAP people group would move from customary testing to this powerful new ABAP Units.

 

YOU MAY BE INTERESTED IN

Power of Parallel Cursor in SAP ABAP

Performance Optimization in ABAP on HANA: Uniting CDS and AMDP

SAP HANA: Revolutionizing Data Processing with ABAP on HANA

 

X
WhatsApp WhatsApp us