Wednesday, 6 June 2018

ABAPUnitTest – TDD implementation

I must confess that I have never-ever written unit test before developing the code. Test Driven Development (TDD) is something which I was not aware of.  When I looked at the first slide I thought this does not make much sense to me. But eventually once I completed week 2, the overall understanding became better and was able to connect the dots.  In order to see how it works with our actual requirement, so thought of implementing it in implicit enhancement. So this blog talks about my experience of implementing a TDD for an enhancement, benefits which I saw, issues which I faced etc. with an intent it will help someone.

SAP Testing and Analysis, SAP ABAP Development, SAP ABAP Study Materials, SAP ABAP Guides
Image from OpenSAP

Before I start, let me also mention one important observation, by sharing my experience here I came to know about so many topics which I was not aware of  for example eCATT for Odata and  ABAP Test double framework.

New Requirement


We have one transaction MB5M which is used to material batches shelf life.  The requirement is to allow only certain types of material to be displayed and rest shall be hidden.

◈ To display material types ZPAC, ZEXP and ZFDP
◈ To hide material types UNBW and ZAPI.

SAP Testing and Analysis, SAP ABAP Development, SAP ABAP Study Materials, SAP ABAP Guides

We are implementing it using an implicit enhancement after data selection which will be used to delete material which are not to be displayed.  Although we also have other enhancement options but I wanted to try out with implicit enhancement

Unit tests to be coded for requirement
We created a basic global class ZCL_SKIP_MATERIAL which will be called in the implicit enhancement.

SAP Testing and Analysis, SAP ABAP Development, SAP ABAP Study Materials, SAP ABAP Guides

A custom table with two fields is created to maintain the allowed and not allowed material types. Based on the requirement above, we created one test method MATERIAL_ALLOWED_OR_NOT which will check whether material type is allowed to be displayed or not. Then we created a local unit test class LTC_SKIP_MATERIAL and defined test methods setup and MATERIAL_ALLOWED_OR_NOT.

SAP Testing and Analysis, SAP ABAP Development, SAP ABAP Study Materials, SAP ABAP Guides

We got an error for ZCL_SKIP_MATERIAL-> DET_MATTYPE_ALLOWED_OR_NOT called does not exist. So we define it .  Please note that data selection of material types of material and materials is included in the constructor. It can also be separated from this class and later added as injectable, but trying to keep things simple.

SAP Testing and Analysis, SAP ABAP Development, SAP ABAP Study Materials, SAP ABAP Guides

So basic set up is done where we have working method which determines whether to display or hide a material based on material type.  So we can see how easy it is to make this productive code from unit testing.

SAP Testing and Analysis, SAP ABAP Development, SAP ABAP Study Materials, SAP ABAP Guides

Once this method was up we created one more method PROCESS_DATA which was called from the enhancement. This method iterates through all selected data and call method DET_MATTYPE_ALLOWED_OR_NOT which we have created previously to filter out records.

SAP Testing and Analysis, SAP ABAP Development, SAP ABAP Study Materials, SAP ABAP Guides

Benefits which I see


◈ One of the most important benefit which I see is I don’t have to run the report each time to see if my code is working for this document type or not, test class does it.

◈ Test classes have actually removed the dependency on test data availability in Dev box. Sometimes if test data is not available in Dev box we normally transport to Quality and test it there and fix bugs. But with implementation of local test class basic mistakes and testing can be done at my end without worrying about test data availability

◈ Clearly less bugs reported during testing and less time wasted.

Challenges which I faced.


◈ Person has to be clear on what will comprise of his/her unit tests and the end result expected.

◈ I myself got confused whether i shall fetch custom table data in our method or in constructor, finally chose constructor.

◈ Another confusion came whether i shall pass the material data from transaction to class at constructor or via separate process methods. Finally keeping it in process method looks logical, otherwise i will have to first pass during constructor and then update it in process data.

1 comment:

  1. This site is really a stroll-by way of for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and also you’ll definitely uncover it. gsn casino

    ReplyDelete