Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ABAPCodeRetreat 23.7.2016 - Unit Testing


Published on

ABAPCodeRetreat Event on 23.7.2016 by Kaufland

Published in: Software
  • Login to see the comments

ABAPCodeRetreat 23.7.2016 - Unit Testing

  1. 1. © Kaufland 2016 | ABAP CODERETREAT WEINSBERG Unit Testing KIS, Weinsberg, 23.07.2016 ABAP CodeRetreat Weinsberg121.07.2016
  2. 2. © Kaufland 2016 | WHO WE ARE KAUFLAND We are more than just a successful international trading company: We are a company where many colleagues become a team, jobs are safe workplaces and real satisfaction Diversity and stability We offer a wide range of jobs with many different entry-levels and career opportunities. Our company is continuously growing, that makes us self-confident Openness and friendly cooperation We support a cooperation based on friendliness and mutual trust ABAP CodeRetreat Weinsberg221.07.2016
  3. 3. © Kaufland 2016 | WHERE WE ARE KAUFLAND Expansive growth, economic size • We are successfully expanding in Germany, Poland, Czech Republic, Slovakia, Croatia, Romania and Bulgaria • Over 150,500 employees form our strong Kaufland team in Europe International cooperation and prospects • We offer an international work environment • We provide a wide variety of tasks in collaboration with our international colleagues • According to the area of deployment you will have the opportunity to collect experience abroad, in one of our European locations ABAP CodeRetreat Weinsberg321.07.2016
  4. 4. © Kaufland 2016 | ABAP CodeRetreat Weinsberg4 KAUFLAND INFORMATION SYSTEMS We are the IT service provider for Kaufland. Ensuring high business performance and carrying data streams on the right track is our daily responsibility Keep IT simple and safe Our objectives are to comprehensively advice our business departments in designing their business processes and to implement software solutions OUR IT IN FIGURES 21.07.2016 220 SAP Systems 40 SAP Developers 25k Z Reports
  5. 5. © Kaufland 2016 | UNIT TESTING ABAP CodeRetreat Weinsberg521.07.2016 Unit Testing Integration Testing System Testing Acceptance Testing
  6. 6. © Kaufland 2016 | ABAP CodeRetreat Weinsberg6 • Set up an initial state • Confront the method under test with test values • Compare actual value with expected test value • Implemented in form of test methods • Test methods are hosted in a test class • Test classes are local classes to the program object under test • Use global test classes only to host reusable logic for tests • Comparisons are done with class CL_ABAP_UNIT_ASSERT UNIT TESTS ABAP UNIT TESTS CREATING ABAP UNIT TESTS UNIT TESTING 21.07.2016
  7. 7. © Kaufland 2016 | • Test classes are local classes defined with the FOR TESTING addition • Test methods are (private) parameterless instance methods, also declared with the FOR TESTING addition • Use Fixtures to ensure particular starting conditions • [class] setup and [class] teardown TEST CLASSES AND TEST METHODS ABAP CodeRetreat Weinsberg7 UNIT TESTING 21.07.2016 setup( ). teardown( ). my_first_test_method( ).
  8. 8. © Kaufland 2016 | EXAMPLE ABAP CodeRetreat Weinsberg8 UNIT TESTING 21.07.2016
  9. 9. © Kaufland 2016 | • Testing starts already during the design of your application • The following slides show some pitfalls you should keep in mind • All coding samples are more or less pseudo-code in ABAP style PREPARATION ABAP CodeRetreat Weinsberg9 UNIT TESTING 21.07.2016
  10. 10. © Kaufland 2016 | ABAP CodeRetreat Weinsberg10 PITFALL #1 CLASS car. PRIVATE SECTION. DATA g_fuel TYPE i. PUBLIC SECTION. set_fuel IMPORTING i_fuel TYPE i. has_fuel RETURNING r_has_fuel TYPE xfeld. ENDCLASS. CLASS testcar FOR TESTING. PRIVATE SECTION. DATA g_testobject TYPE REF TO car. METHOD class_setup. g_testobject = NEW #( ). METHOD test_fuel. g_testobject->set_fuel( 30 ). METHOD test_has_fuel. DATA(has_fuel) = g_test_object->has_fuel( ). ENDCLASS. UNIT TESTING 21.07.2016 • Test cases guarantee no order! • When method test_has_fuel() is called we do not know in which state the test- object already is! • Take care to initialize your test-object before each testcase so that you know the current state! (e.g. use setup() instead of class_setup()) Each testmethod has to return the same result each time it's called otherwise we cannot test it.
  11. 11. © Kaufland 2016 | ABAP CodeRetreat Weinsberg11 PITFALL #2 CLASS order. DATA g_order_number TYPE i. METHOD get_positions. " load data SELECT * FROM mseg WHERE mblnr = g_order_number. " prepare data " do something else " return result. ENDCLASS. UNIT TESTING 21.07.2016 • We do not know what is currently in the database! • We do not know which data will be prepared! • We cannot assume what data will be returned! Do not load information from sources that cannot be controlled during testing. Load data before instantiating the order object. This will enable us to write a testmethod for the data preparation. Alternative solution: use a database- handler-class and pass it as a parameter. During testing you can use a mock of the database-handler that returns fix results.
  12. 12. © Kaufland 2016 | ABAP CodeRetreat Weinsberg12 PITFALL #3 CLASS store. METHOD is_opened. CALL FUNCTION 'DATE_COMPUTE_DAY' EXPORTING date = sy-datum IMPORTING day = day. IF day <> 7. r_is_opened = abap_true. ENDIF. ENDCLASS. UNIT TESTING 21.07.2016 • If we expect the store to be closed the testmethod will only succeed on sundays • Testmethods expect a hard-coded result. Not possible in this case! Don't use system (or other) variables that you cannot control. Better give the date as parameter. So your testmethod can call testobject->is_opened( '20160505' ).
  13. 13. © Kaufland 2016 | ABAP CodeRetreat Weinsberg13 UNIT TESTING 21.07.2016 POSSIBLE ARCHITECTURE Database Database provider Business object Data Access Object (DAO) Database layer Business layerAccess layer
  14. 14. © Kaufland 2016 | • Sample Reports in package: SABP_UNIT_SAMPLE • Executing Tests: Ctrl + Shift + F10 • Verification of test expectations: CL_ABAP_UNIT_ASSERT • Test classes and methods are defined / declared with the FOR TESTING addition • Considerations on how to write testable code must be included in the design avoid the pitfalls HOW TO GET STARTED / KEY TAKEAWAYS ABAP CodeRetreat Weinsberg14 UNIT TESTING 21.07.2016