Site icon SAP Human Resources Experts And Solutions

ABAP Unit Test

<p>And don&&num;8217&semi;t tell me I&&num;8217&semi;m a bore&period; ABAP unit test worth it&excl;<&sol;p>&NewLine;<p>Here how it was&period; Once I told you about Test Driven Development methodology&comma; but today I decided to try it in practice how it works in SAP&period; I created basic OData Service and decided to test it automatically&period;<&sol;p>&NewLine;<p><a href&equals;"http&colon;&sol;&sol;saphr&period;ru&sol;wp-content&sol;uploads&sol;segw&lowbar;01&period;png"><img class&equals;"alignnone size-full wp-image-2200" src&equals;"http&colon;&sol;&sol;saphr&period;ru&sol;wp-content&sol;uploads&sol;segw&lowbar;01&period;png" alt&equals;"" width&equals;"1208" height&equals;"371" &sol;><&sol;a><&sol;p>&NewLine;<p>Now I want to test it automatically&period; Earlier I told about <a href&equals;"https&colon;&sol;&sol;saphcmsolutions&period;com&sol;howto-organize-sap-testing&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener">SECATT<&sol;a>&period; Today let&&num;8217&semi;s talk about Unit testing&period; It such a thing which is developed in ABAP and allows to test logic inside of a program&period; It does not simulate user input but verifies internal procedures&comma; methods&comma; functions for a supposed result&period; For example&comma; it should always save correct data but never allows to save incorrect data&period; To implement we create two methods &&num;8211&semi; positive result test and negative result test&period;<br &sol;>&NewLine;<&excl;--more--><&sol;p>&NewLine;<p>There is the class Data Provider Class &lpar;DPC&rpar;&comma; which provides data over CRUD for OData model&period; It means we send HTTP GET request&comma; for example&comma; and it sends us back some data after authentification&period; We need a test which verifies the data is sent and data is equal to etalon&period;<&sol;p>&NewLine;<p>For this&comma; we need to develop a local class which tests DPC methods&period; Sources are below&period;<&sol;p>&NewLine;<p>Benefits&excl;<&sol;p>&NewLine;<h3>ABAP Unit Test Example<&sol;h3>&NewLine;<p>Look&period; Except that program can run these tests&comma; gather statistics&comma; run everything in the background&comma; thereby automating quality assurance&comma; could live together with Code Inspector&semi; besides all of these&comma; it can think&excl; Well&comma; think in its own way &&num;8211&semi; it finds out what code is covered and what is not by our test cases&period;<&sol;p>&NewLine;<div id&equals;"attachment&lowbar;2201" style&equals;"width&colon; 868px" class&equals;"wp-caption alignnone"><a href&equals;"http&colon;&sol;&sol;saphr&period;ru&sol;wp-content&sol;uploads&sol;abu&lowbar;01&period;png"><img aria-describedby&equals;"caption-attachment-2201" class&equals;"wp-image-2201 size-full" src&equals;"http&colon;&sol;&sol;saphr&period;ru&sol;wp-content&sol;uploads&sol;abu&lowbar;01&period;png" alt&equals;"ABAP Unit Test" width&equals;"858" height&equals;"238" &sol;><&sol;a><p id&equals;"caption-attachment-2201" class&equals;"wp-caption-text">ABAP Unit Test<&sol;p><&sol;div>&NewLine;<p><a href&equals;"http&colon;&sol;&sol;saphr&period;ru&sol;wp-content&sol;uploads&sol;abu&lowbar;02&period;png"><img class&equals;"alignnone size-full wp-image-2202" src&equals;"http&colon;&sol;&sol;saphr&period;ru&sol;wp-content&sol;uploads&sol;abu&lowbar;02&period;png" alt&equals;"" width&equals;"702" height&equals;"318" &sol;><&sol;a><&sol;p>&NewLine;<p>The cool thing is the system shows what business-cases you have are not covered in the unit-test &lpar;test scenarios&rpar;&period; Such hint will remind what business case is not developed in the test scenario&period;<&sol;p>&NewLine;<p>Imagine tomorrow you&&num;8217&semi;ll get a new support request&period; Somebody fix something and everything goes down&period; Here not to move all these broken stuff to production system we run unit-tests&period; Unit test will check everything in the development system&comma; ensure this or that piece of code works not how it was supposed in test-case&comma; And then you need to fix code in case of business logic change or fix error in your code&period; Don&&num;8217&semi;t forget&comma; it runs not only one particular unit-test&comma; but dozens and hundreds of them being developed by other developers during system maintenance&period; Very convenient&excl;<&sol;p>&NewLine;<pre lang&equals;"abap">class zcl&lowbar;Ut&lowbar;Z&lowbar;Hrbot definition for testing &NewLine; duration short &NewLine; risk level harmless &NewLine;&period; &NewLine; &NewLine; private section&period; &NewLine; &NewLine; data&colon; mo&lowbar;request&lowbar;context&lowbar;object type ref to &sol;iwbep&sol;cl&lowbar;mgw&lowbar;request&lowbar;unittst&period; &NewLine; data&colon; ms&lowbar;request&lowbar;context&lowbar;struct type &sol;iwbep&sol;cl&lowbar;mgw&lowbar;request&lowbar;unittst&equals;&gt&semi;ty&lowbar;s&lowbar;mgw&lowbar;request&lowbar;context&lowbar;unit&period; &NewLine; data&colon; mo&lowbar;data&lowbar;provider type ref to zcl&lowbar;Z&lowbar;Hrbot&lowbar;01&lowbar;Dpc&lowbar;Ext&period; &NewLine; &NewLine; methods&colon; setup&period; &NewLine; methods&colon; teardown&period; &NewLine; methods&colon; lt&lowbar;get&lowbar;Entityset for testing&period; &NewLine;endclass&period; "zcl&lowbar;Ut&lowbar;Z&lowbar;Hrbot &NewLine; &NewLine; &NewLine;class zcl&lowbar;Ut&lowbar;Z&lowbar;Hrbot implementation&period; &NewLine; &NewLine; method setup&period; &NewLine; &NewLine; create object mo&lowbar;data&lowbar;provider &period; &NewLine; &NewLine; endmethod&period; &NewLine; &NewLine; &NewLine; method teardown&period; &NewLine; &NewLine; &NewLine; endmethod&period; &NewLine; &NewLine; &NewLine; method lt&lowbar;get&lowbar;Entityset&period; &NewLine; &NewLine; DATA&colon; lr&lowbar;data TYPE REF TO data&period; &NewLine; DATA&colon; ls&lowbar;response&lowbar;context TYPE &sol;iwbep&sol;if&lowbar;mgw&lowbar;appl&lowbar;types&equals;&gt&semi;ty&lowbar;s&lowbar;mgw&lowbar;response&lowbar;context&period; &NewLine; DATA&colon; ls&lowbar;data TYPE zmes&lowbar;quota&period; &NewLine; FIELD-SYMBOLS&colon; TYPE ANY TABLE&period; &NewLine; &NewLine; ms&lowbar;request&lowbar;context&lowbar;struct-technical&lowbar;request-source&lowbar;entity&lowbar;type &equals; 'Quota'&period; &NewLine; ms&lowbar;request&lowbar;context&lowbar;struct-technical&lowbar;request-target&lowbar;entity&lowbar;type &equals; 'Quota'&period; &NewLine; ms&lowbar;request&lowbar;context&lowbar;struct-technical&lowbar;request-source&lowbar;entity&lowbar;set &equals; 'QuotaSet'&period; &NewLine; ms&lowbar;request&lowbar;context&lowbar;struct-technical&lowbar;request-target&lowbar;entity&lowbar;set &equals; 'QuotaSet'&period; &NewLine; &NewLine; mo&lowbar;request&lowbar;context&lowbar;object &equals; mo&lowbar;data&lowbar;provider-&gt&semi;&sol;iwbep&sol;if&lowbar;mgw&lowbar;conv&lowbar;srv&lowbar;runtime~init&lowbar;dp&lowbar;for&lowbar;unit&lowbar;test&lpar; &NewLine; is&lowbar;request&lowbar;context &equals; ms&lowbar;request&lowbar;context&lowbar;struct &rpar;&period; &NewLine; &NewLine; try &period; &NewLine; mo&lowbar;data&lowbar;provider-&gt&semi;&sol;IWBEP&sol;IF&lowbar;MGW&lowbar;APPL&lowbar;SRV&lowbar;RUNTIME~GET&lowbar;ENTITYSET&lpar; &NewLine; exporting &NewLine; io&lowbar;tech&lowbar;request&lowbar;context &equals; mo&lowbar;request&lowbar;context&lowbar;object &NewLine; importing &NewLine; er&lowbar;entityset &equals; lr&lowbar;data &NewLine; es&lowbar;response&lowbar;context &equals; ls&lowbar;response&lowbar;context &NewLine; &rpar;&period; &NewLine; &NewLine; catch &sol;iwbep&sol;cx&lowbar;mgw&lowbar;busi&lowbar;exception&period; &NewLine; &NewLine; catch &sol;iwbep&sol;cx&lowbar;mgw&lowbar;tech&lowbar;exception&period; &NewLine; &NewLine; endtry&period; &NewLine; &NewLine; assign lr&lowbar;data-&gt&semi;&ast; to &period; &NewLine; loop at into ls&lowbar;data&period; &NewLine; exit&period; &NewLine; ENDLOOP&period; &NewLine; &NewLine; cl&lowbar;Abap&lowbar;Unit&lowbar;Assert&equals;&gt&semi;assert&lowbar;Equals&lpar; &NewLine; act &equals; ls&lowbar;data-pernr &NewLine; exp &equals; 1 &NewLine; msg &equals; 'Testing one employee' &NewLine;&ast; level &equals; &NewLine; &rpar;&period; &NewLine; &NewLine; endmethod&period; &NewLine; &NewLine; &NewLine; &NewLine;endclass&period; &NewLine;<&sol;pre>&NewLine;

Exit mobile version