abapcoderetreat 23.7.2016 - unit testing

14
© Kaufland 2016 | ABAP CODERETREAT WEINSBERG Unit Testing KIS, Weinsberg, 23.07.2016 ABAP CodeRetreat Weinsberg 1 21.07.2016

Upload: abapcoderetreat

Post on 11-Apr-2017

405 views

Category:

Software


0 download

TRANSCRIPT

© Kaufland 2016 |

ABAP CODERETREAT WEINSBERGUnit Testing

KIS, Weinsberg, 23.07.2016

ABAP CodeRetreat Weinsberg121.07.2016

© 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, jobsare safe workplaces and real satisfaction

Diversity and stability

We offer a wide range of jobs with many different entry-levels andcareer opportunities. Our company is continuously growing, thatmakes us self-confident

Openness and friendly cooperation

We support a cooperation based on friendliness and mutual trust

ABAP CodeRetreat Weinsberg221.07.2016

© 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 ourinternational colleagues

• According to the area of deployment you will have theopportunity to collect experience abroad, in one of ourEuropean locations

ABAP CodeRetreat Weinsberg321.07.2016

© 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 trackis our daily responsibility

Keep IT simple and safe

Our objectives are to comprehensively advice our businessdepartments in designing their business processes and toimplement software solutions

OUR IT IN FIGURES

21.07.2016

220 SAP Systems

40SAP Developers

25k Z Reports

© Kaufland 2016 |

UNIT TESTING

ABAP CodeRetreat Weinsberg521.07.2016

Unit Testing

Integration Testing

System Testing

Acceptance Testing

© 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

© 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 ( ).

© Kaufland 2016 |

EXAMPLE

ABAP CodeRetreat Weinsberg8

UNIT TESTING

21.07.2016

© 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

© 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 RETURNINGr_has_fuel TYPE xfeld .

ENDCLASS.

CLASS testcar FOR TESTING.

PRIVATE SECTION.

DATA g_testobject TYPE REF TO car .

METHODclass_setup .

g_testobject = NEW #( ).

METHODtest_fuel .

g_testobject- >set_fuel ( 30 ).

METHODtest_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 beforeeach testcase so that you know the currentstate! (e.g. use setup () instead ofclass_setup () )

� Each testmethod has to return the same result each time it's called otherwise we cannottest it.

© Kaufland 2016 | ABAP CodeRetreat Weinsberg11

PITFALL #2

CLASS order .

DATA g_order_number TYPE i .

METHODget_positions .

" load data

SELECT * FROM msegWHEREmblnr = g_order_number .

" prepare data

" do something else

" return result.

ENDCLASS.

UNIT TESTING

21.07.2016

• We do not know what is currently in thedatabase!

• We do not know which data will beprepared!

• We cannot assume what data will bereturned!

� Do not load information from sources thatcannot be controlled during testing. Load data before instantiating the order object. This will enable us to write a testmethod forthe data preparation.

� Alternative solution: use a database-handler-class and pass it as a parameter. During testing you can use a mock of thedatabase-handler that returns fix results.

© Kaufland 2016 | ABAP CodeRetreat Weinsberg12

PITFALL #3

CLASS store .

METHODis_opened .

CALL FUNCTION'DATE_COMPUTE_DAY'EXPORTING

date = sy-datumIMPORTING

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 thetestmethod will only succeed on sundays �

• Testmethods expect a hard-coded result. Not possible in this case!

� Don't use system (or other) variables that youcannot control. Better give the date as parameter. So your testmethod can calltestobject- >is_opened ( '20160505' ).

© 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

© 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