Designing High Quality Apps using Test-Driven ... - ASUG.com

17 downloads 55 Views 1MB Size Report
Designing High Quality Apps using. Test-Driven Development with ABAP Objects . JK (JayaKumar Pedapudi) - Principal Consultant - NTT DATA, Inc.
[

Designing High Quality Apps using Test-Driven Development with ABAP Objects. JK (JayaKumar Pedapudi) - Principal Consultant - NTT DATA, Inc.

[ Agenda  Introduction  Agile Programming Strategy    

Learning Points Return on Investment (ROI) About Test Driven Development (TDD) TDD Mantra & TDD Basics

 Simple TDD – Demo  3 Laws of TDD

 The Unit-Testing Framework  ABAP Unit – A Design Methodology  Myths on Unit Testing & Best Practices  Wrap Up, Take Away Points & Q&A

 Appendix Real Experience. Real Advantage.

2

[ About NTT DATA, Inc. - NTT DATA Americas Integration Americas – 17,000 employees and $1.3B in revenues – Offering clients more options through increased depth and scale across: • Consulting & Systems Integration • Applications Services / ERP • Infrastructure Services • BPO Services • BI, Analytics & Performance Mgmt • Strategic Staffing – Balanced Industry depth in healthcare, commercial, financial and public sector

– Successful long-term relationships with rich and diverse customer base

Onshore

– Balanced global delivery model that maximizes consistency and cost efficiencies

Offshore

‘NTT DATA Your Innovation Partner, Anywhere around the world’ Real Experience. Real Advantage.

3

[ Agenda  Introduction  Agile Programming Strategy    

Learning Points Return on Investment (ROI) About Test Driven Development (TDD) TDD Mantra & TDD Basics

 Simple TDD – Demo  3 Laws of TDD

 The Unit-Testing Framework  ABAP Unit – A Design Methodology  Myths on Unit Testing & Best Practices  Wrap Up, Take Away Points & Q&A

 Appendix Real Experience. Real Advantage.

4

[ Agile Programming Strategy  There is an urgent need for an agile programming strategy that can help customer/businesses with following benefits.  Reduce the software development costs  Improve software reliability  Decrease development time  Working application product

 Better Return On Investment

 “Test-driven development (TDD) is a software development strategy that requires that automated tests be written prior to writing functional code in small, rapid iterations.”

Real Experience. Real Advantage.

5

[ Learning Points  Learn how TDD enables the writing of software that has better structure, in less time and with fewer defects.  Discover how TDD results in better code design, usability design, safety net, instant feedback, modularity and documentation.  Learn about the techniques and best practices to make it almost impossible to deliver bugs in the next software project.

Real Experience. Real Advantage.

6

[ Return on Investment (ROI) No Feedback

Number of Bugs

Without TDD

Done ?

Time Development

Quality Assurance Integration & System Testing

Development

Quality Assurance

Traditional

Real Experience. Real Advantage.

7

[ Return on Investment (ROI) Life Cycle Benefit - Investment ROI = Investment No Feedback

Number of Bugs

Without TDD

Done ? With TDD Done Sooner ! Continuous Feedback

Time

Development

Quality Assurance Integration & System Testing

Development

Quality Assurance

Traditional TDD

Development Investment

Life Cycle Benefit Net Return Real Experience. Real Advantage.

8

[ Return on Investment (ROI) (cont’d)  Better Code Design

 Usable Code  Safety Net

 Modularity  Instant Feedback

 Increased Productivity  WYGIWYM (What You Get Is What You Meant)

 Documentation

Real Experience. Real Advantage.

9

[ About TDD…  What is Test Driven Development (TDD)?  Test Driven Development (TDD) is a software development technique and one of the aspects of Extreme Programming (XP).  TDD is a design methodology.  TDD is NOT about testing.  TDD is a software development technique that uses short development iterations based on pre-written test cases that define desired improvements or new functions.  TDD is about design and development. The process of designing software by writing a validating test before the code.  TDD is a technique whereby you write your test cases before you write any implementation code.  These tests drive or dictate the code that is developed. Real Experience. Real Advantage.

10

[ About TDD (cont’d)  Why TDD?  Better Code Design  Usable Code

 Safety Net  Modularity  Instant Feedback  Increased Productivity  WYGIWYM (What You Get Is What You Meant)  Documentation Real Experience. Real Advantage.

11

[ About TDD (cont’d)  Why TDD? (Cont’d)  The test is the executable specification.  You start visualizing the goal first, design the API, then later think about the possible implementation.

 You develop just enough.  You get to the goal as quick as possible.

 You don’t develop unnecessary code.  There is no code without a test.  There is no test without a user requirement.

Real Experience. Real Advantage.

12

[ About TDD (cont’d)  Why TDD? (Cont’d)  Once you get test working, you know it is working.  The test gives us the courage to refactor.  It’s more fun that way, it reduces fear and builds confidence.

“Regression testing = Stable software = Quality software”

Real Experience. Real Advantage.

13

[ TDD Mantra The TDD Mantra…  Write a single Unit Test for a very small piece of functionality and then Compile/Check syntax/Run and see it fails (RED).  Write the simplest amount of code that will make the test compile and pass (GREEN).  REFACTOR the code and/or tests to improve the design and still pass (GREEN). Kent Beck writes in his book "Test-Driven Development: By Example” as Red/Green/Refactor – the TDD mantra. Real Experience. Real Advantage.

14

[ TDD Basics How does TDD help?  TDD helps you produce clean working code that fulfills requirements.  Write Test Code  Code that fulfills requirements.

 Write Functional/Production Code  Working code that fulfills requirements.

 Refactor  Clean working code that fulfills requirements.

 Refactoring?  “A change to the system that leaves its behavior unchanged, but enhances some nonfunctional quality – simplicity, flexibility, understandability, performance” - (Kent Beck, Extreme Programming Explained). Real Experience. Real Advantage.

15

[ TDD Basics (cont’d) Principle of TDD (In Practice) Start Write a Test Run the Test

Refactoring

Write (just enough) Production Code to compile Run the Test

Run the Test

Write (just enough) Production Code to pass

Real Experience. Real Advantage.

16

[ TDD Basics (cont’d)  What it takes to adopt TDD?  Requires good knowledge on Object Oriented Concepts.  Requires minimum knowledge on ABAP Objects.  Requires minimum SAP NetWeaver 2004 version.  Prior version don’t have the Unit Testing Framework available.

 Requires Unit-Testing Framework  (Built into ABAP workbench from SAP NetWeaver 2004 onwards)

 Requires better coding techniques to write clean code.  Requires real commitment and discipline to follow this process, in order to get better quality results.

Real Experience. Real Advantage.

17

[ Agenda  Introduction  Agile Programming Strategy    

Learning Points Return on Investment (ROI) About Test Driven Development (TDD) TDD Mantra & TDD Basics

 Simple TDD – Demo  3 Laws of TDD

 The Unit-Testing Framework  ABAP Unit – A Design Methodology  Myths on Unit Testing & Best Practices  Wrap Up, Take Away Points & Q&A

 Appendix Real Experience. Real Advantage.

18

[ Simple TDD – Demo  Simple tip calculation program.

(Consider standard 15 percent for easy calculation) Real Experience. Real Advantage.

19

[ Simple TDD – Demo (cont’d)  Simple test for testing this tip calculation routine:

Real Experience. Real Advantage.

20

[ Simple TDD – Demo (cont’d)  Executing ABAP Unit Test  Menu "Program->Test->Unit Test" or by pressing Ctrl+Shift+F10.

 It is important that the system is configured in such a way that it allows the ABAP Unit tests to be performed.  This means completing the configuration in transaction SAUNIT_CLIENT_SETUP, as well as setting the profile parameter abap/test_generation equal to ON.  This profile parameter should be defined by the SAP NetWeaver administrator for the system. Real Experience. Real Advantage.

21

[ Simple TDD – Demo (cont’d)  Understanding ABAP Unit Result Display  ABAP Unit Test Overview (On the left hand side of the result display)

 The test overview section provides a simple overview of all of the tests that were executed and includes the aggregate status of each test.  It also displays the number of assertions and the assertion error level for each test. Real Experience. Real Advantage.

22

[ Simple TDD – Demo (cont’d)  Understanding ABAP Unit Result Display (cont’d)  ABAP Unit Assertion Messages (On the top right hand side of the result display)

 The messages section provides a listing of all of the assertion messages that were logged during the test.  The message displayed here come from passing the failure text to the MSG parameter when calling one of the ASSERT methods.  By double-clicking on an entry, the detail of the assertion message is shown in the detail section of ABAP Unit result display.

Real Experience. Real Advantage.

23

[ Simple TDD – Demo (cont’d)  Understanding ABAP Unit Result Display (cont’d)  ABAP Unit Message Detail (On the bottom right hand side of the result display)

 The message details section provides details for each assertion message.

 To see where exactly the failure occurred in our test, we need to click on the link (Line: 44) provided under Stack node.

Real Experience. Real Advantage.

24

[ Simple TDD – Demo (cont’d)  Understanding ABAP Unit Result Display (cont’d)  Assertion failure location (Clicking on the Link, i.e. "Line: 44")

 Assertion failed, because we are expecting '45' but the resulted TEST_AMOUNT was 300, which is wrong.  This resulted value is calculated inside the form routine CALC_TIP_AMOUNT. Real Experience. Real Advantage.

25

[ Simple TDD – Demo (cont’d)  Understanding ABAP Unit Result Display (cont’d)  Reviewing the CALC_TIP_AMOUNT form routine

Real Experience. Real Advantage.

26

[ Simple TDD – Demo (cont’d)  Understanding ABAP Unit Result Display (cont’d)  Fixing the issue/error in CALC_TIP_AMOUNT form routine and activating it.

 Running again ABAP Unit test by pressing Ctrl+Shift+F10 will result in successful message.  ABAP Unit can help us detect the failures/errors easily and can be tracked down. With ABAP Unit tests in place and with a open mind to simple analysis will bring great value and confidence.  All developers will love to use once they understand its easiness and the value it brings to the code quality and project. Real Experience. Real Advantage.

27

[ 3 Laws of TDD First Law

Write no production code unless to pass a failing test. Second Law

Write no more of a test than is sufficient to make it fail. Third Law

Write no more production code than is sufficient to pass a failing test.

Real Experience. Real Advantage.

28

[ Agenda  Introduction  Agile Programming Strategy    

Learning Points Return on Investment (ROI) About Test Driven Development (TDD) TDD Mantra & TDD Basics

 Simple TDD – Demo  3 Laws of TDD

 The Unit-Testing Framework  ABAP Unit – A Design Methodology  Myths on Unit Testing & Best Practices  Wrap Up, Take Away Points & Q&A

 Appendix Real Experience. Real Advantage.

29

[ The Unit-Testing Framework  Unit testing frameworks are quite popular in the objectoriented programming world.  Frameworks like JUnit (for Java), SUnit (for Smalltalk), and Test::Unit (for Ruby) provide a rich set of functionality.

 Various code-driven testing frameworks are collectively know as xUnit. Unit tests are at the core of TDD.  Unit testing frameworks helps simplify the process of Unit testing and the goal is to make writing test as painless as possible.  As of SAP NetWeaver 2004, ABAP Unit is available as Unit testing framework, which takes the ideas of JUnit from Java environment and applies them to ABAP. Real Experience. Real Advantage.

30

[ Agenda  Introduction  Agile Programming Strategy    

Learning Points Return on Investment (ROI) About Test Driven Development (TDD) TDD Mantra & TDD Basics

 Simple TDD – Demo  3 Laws of TDD

 The Unit-Testing Framework  ABAP Unit – A Design Methodology  Myths on Unit Testing & Best Practices  Wrap Up, Take Away Points & Q&A

 Appendix Real Experience. Real Advantage.

31

[ ABAP Unit – A Design Methodology  ABAP Unit is a test tool highly integrated directly in the ABAP runtime environment.  ABAP Unit can be used for executing module/unit tests – i.e. for checking the functions of code sections in a program.

 ABAP Units are implemented using the ABAP programming language.  Therefore, the advantage is that there is no need to learn any additional test scripting language.  ABAP Unit is standardized process to test and it can be put into a QA process and used for automatic and rigorous testing.

Real Experience. Real Advantage.

32

[ ABAP Unit – A Design Methodology (cont’d) Advantages of ABAP Unit.  The tests are programmed in ABAP.  Additional test script language is not required.

 The tests are developed in the ABAP development environment.  Additional interface operation is not required.

 In the development phase, tests can be called directly within the editor.  The test execution can be automated.  Mass tests are enabled by integration into the Code Inspector tool.

Real Experience. Real Advantage.

33

[ ABAP Unit – A Design Methodology (cont’d) ABAP Unit implementation.  Tests are developed in the form of test methods in test classes during or even before the actual program development.  Test classes are local classes of the ABAP program to be tested and are thus a part of that program, which also ensures that the version integrity is maintained after transports.  Developers incorporate tests and test conditions (fixtures) into their programs and they themselves executes these tests.  Since the implemented code is first tested by developers themselves the higher quality product can be expected.

Real Experience. Real Advantage.

34

[ ABAP Unit – A Design Methodology (cont’d) ABAP Unit Test Organization.

Real Experience. Real Advantage.

35

[ ABAP Unit – A Design Methodology (cont’d) Main Components of ABAP Unit  Test Class and Test Method looks like:

Real Experience. Real Advantage.

36

[ ABAP Unit – A Design Methodology (cont’d) Main Components of ABAP Unit (cont’d)  Test Attributes.  Test attributes are management attributes of test classes, which are taken into account during test execution.

 The test attributes of local test classes are defined using pseudo comments in the CLASS statement immediately following the FOR TESTING addition.

 You can specify one pseudo comment per line in the program and note that they are Case-Sensitive.

Real Experience. Real Advantage.

37

[ ABAP Unit – A Design Methodology (cont’d) Main Components of ABAP Unit (cont’d)  Test Fixtures.  Fixtures is a test configuration that is created before a test method is called and also ensures a unique test behavior.

 Fixtures consists of test data, test objects, resources, and connections. The complete set of these test objects is called the test fixture.  Fixtures are nothing more than private method defined in the test class, which are used to set up specific conditions for the test.  These method names are reserved and are triggered automatically by the ABAP runtime. Real Experience. Real Advantage.

38

[ ABAP Unit – A Design Methodology (cont’d) Main Components of ABAP Unit (cont’d)  Test Fixtures (cont.)

Real Experience. Real Advantage.

39

[ ABAP Unit – A Design Methodology (cont’d) Test Execution Sequence

Real Experience. Real Advantage.

40

[ ABAP Unit – A Design Methodology (cont’d) ABAP Unit Load Concepts  When this developed production code and test code are moved to production environment, the compiler and the run time will just ignore all the test code.

Real Experience. Real Advantage.

41

[ ABAP Unit – A Design Methodology (cont’d) Services of ABAP Unit  The complete listing of the public static methods of class CL_AUNIT_ASSERT

Real Experience. Real Advantage.

42

[ Agenda  Introduction  Agile Programming Strategy    

Learning Points Return on Investment (ROI) About Test Driven Development (TDD) TDD Mantra & TDD Basics

 Simple TDD – Demo  3 Laws of TDD

 The Unit-Testing Framework  ABAP Unit – A Design Methodology  Myths on Unit Testing & Best Practices  Wrap Up, Take Away Points & Q&A

 Appendix Real Experience. Real Advantage.

43

[ Myths around Unit Testing  Myth #1 : Will it not double the development time if we write unit tests? - No. Writing unit tests is a investment towards time savings over the lifetime of the project.

Real Experience. Real Advantage.

44

[ Myths around Unit Testing (cont’d)  Myth #2 : All my unit tests just get outdated over a time! - No. Unit tests are not parallel to your code like documentation. You will be driving your code with them.

Real Experience. Real Advantage.

45

[ Myths around Unit Testing (cont’d)  Myth #3 : Won’t the client refuse to pay for it? - No. The client is paying for clean, correct code. Unit testing is the means to that end.

Real Experience. Real Advantage.

46

[ Best Practices Always start with a test for better API design. Write no production code unless to pass a failing test. Write no more of a test than is sufficient to make it fail. Always write simplest possible solution to make test pass. Write no more production code than is sufficient to pass a failing test.  Before Refactoring, always run the ABAP Unit test to make sure all the tests are passed and is all working.  Run ABAP Unit test very often to make sure nothing broken in the process. Remember “Your tests should drive or dictate the code that is being developed”     

Real Experience. Real Advantage.

47

[ Agenda  Introduction  Agile Programming Strategy    

Learning Points Return on Investment (ROI) About Test Driven Development (TDD) TDD Mantra & TDD Basics

 Simple TDD – Demo  3 Laws of TDD

 The Unit-Testing Framework  ABAP Unit – A Design Methodology  Myths on Unit Testing & Best Practices  Wrap Up, Take Away Points & Q&A

 Appendix Real Experience. Real Advantage.

48

[ Wrap Up  We now know what is TDD and how it helps in designing better API for producing quality applications.  TDD helps building application that has better structure, in less time with better code design and usability design.

 TDD process produces instant feedback, which acts as safety net when making changes.  The Unit tests helps in writing modularized code and also helps as a documentation.  Learnt about 3 Laws of TDD and TDD Mantra.  ABAP Unit – Unit testing framework &  Best Practices. Real Experience. Real Advantage.

49

[ Take Away Points  ABAP Unit, A Unit Testing Framework is built into ABAP Workbench from SAP NetWeaver 2004 onwards.  ABAP Unit is a test tool highly integrated directly in the ABAP runtime environment.  ABAP Units are implemented using the ABAP programming lang.  The tests are programmed and developed in ABAP development environment.  In a production system the ABAP Unit tests are not part of the productive program load and hence no performance or security drawbacks.  TDD is a design methodology and helps you produce clean working code to build the quality product.  The TDD Mantra – Red, Green & Refactor. Real Experience. Real Advantage.

50

[ Additional References  Test-Driven Development: By Example  Kent Beck, Addison-Wesley, 2003.

 Test-Driven Development: A Practical Guide  Dave Astels, Prentice-Hall, 2003.  Refactoring: Improving the Design of Existing Code  Martin Fowler, Addison-Wesley, 1999

 Test Driven Development with ABAP Objects (Upcoming)  JK (JayaKumar Pedapudi), SAPPRESS.

 https://wiki.sdn.sap.com/wiki/display/ABAP/ABAP+Unit  http://xprogramming.com/index.php Real Experience. Real Advantage.

51

[ Questions? Presented By: JK ( JayaKumar Pedapudi )

Email: [email protected]

Further Info: Pat Gray – Director, Marketing Email: [email protected]

Visit Booth Number: 2509

Real Experience. Real Advantage.

52

[

]

 Thank you for participating. Please remember to complete and return your evaluation form following this session. For ongoing education on this area of focus, visit the Year-Round Community page at www.asug.com/yrc

[

Real Experience. Real Advantage.

SESSION CODE: 0913

53

[ Appendix:  ABAP Unit – A Design Methodology  Main Components of ABAP Unit  ABAP Unit Load Concepts  ASSERT_EQUALS - Method parameters details  Unit Test Flow Control in case of Failure

Real Experience. Real Advantage.

54

[ Appendix:

ABAP Unit – A Design Methodology

Main Components of ABAP Unit  Test Classes.  Test classes are local classes in ABAP programs, with the exception of type groups and interface pools (i.e., executable programs, class pools, function groups, module pools, and subroutine pools).  These test classes are not generated in productive systems and therefore cannot be executed.  Hence it is safe and there is no performance issue caused due to the test classes in production system.

 As of SAP NetWeaver 7.0, test classes can be implemented using the global classes. These global classes are abstract and can only be used in local test classes. Real Experience. Real Advantage.

55

[ Appendix:

ABAP Unit – A Design Methodology (cont’d)

Main Components of ABAP Unit (cont’d)  Test Methods.  Test methods are parameter less (i.e., do not have method signature) instance methods of test class, which are used to trigger an ABAP Unit test.  These test methods are defined using the FOR TESTING addition to the METHODS statement in the CLASS definition block.  Each test method corresponds to an ABAP Unit test, which is responsible to trigger some block of production code in object you are testing.  Test methods should be private, or protected if the methods are inherited. Real Experience. Real Advantage.

56

[ Appendix:

ABAP Unit – A Design Methodology (cont’d)

ABAP Unit Load Concepts  Production Code and Test Code.  The definition of test classes divides a program into production code and test code. Test code (i.e., Test classes and their components) are not generated in production systems.  So you cannot access a test class from the production code.  On the other hand, test classes can access all addressable components of a program and other test classes.  In a production system the ABAP Unit tests are not part of the productive program load.  Hence there are no performance or security drawbacks.  Since the tests are maintained along with corresponding production code, the code transportation would be easier. Real Experience. Real Advantage.

57

[ Appendix:

ABAP Unit – A Design Methodology (cont’d)

ASSERT_EQUALS - Method parameters details

 This static method ASSERT_EQUALS is used to ensures the equality of two data objects. Two data objects in comparison can be a string or any data type object or an internal table, etc.

Real Experience. Real Advantage.

58

[ Appendix:

ABAP Unit – A Design Methodology (cont’d)

Unit Test Flow Control in case of Failure.

Real Experience. Real Advantage.

59

[ Motivation  If you are planning to test after you’ve developed the system, you won’t have enough time for testing.  Write the tests before the actual code!

 If things get complicated, you might fear that “the system” doesn’t work. You loose the confidence.  Execute the tests and get positive feedback (everything still works) or  Get instance feedback to locate the point that does not / no longer work.

 If you don’t have tests for the code, you shouldn’t use it / release it / ship it.  This can’t happen if you write the test first (so you develop better test coverage). Real Experience. Real Advantage.

60

[ Motivation (cont’d)  If you’re overwhelmed by the complexity, you get frustrated.  Start with the simplest thing and proceed in tiny steps!

 If you are planning to skip the regression test, because you don’t have time, then they are more chances that application may go useless.  Writing tests along with development, helps in performing regression tests.

 If performance is only considered towards the end of the project, then you won’t be able to just “add a little more performance” to the system.  Re-use unit tests for performance tests even during the development and don’t start with performance tests late in the project! Real Experience. Real Advantage.

61