Вы находитесь на странице: 1из 53

Developing High Quality Products using

Test-Driven Development with ABAP Objects ]


JK ( JayaKumar Pedapudi )
Senior Business Consultant
NetWeaver Practice - Intelligroup

Session Code: 1106


May 18, 2010

[ RAJEEV KAPUR
ASUG INSTALLATION MEMBER
MEMBER SINCE: 2008 [ MARCDERE LOUIS
ASUG INSTALLATION MEMBER
MEMBER SINCE: 2009

[ JILL MIKROS
ASUG INSTALLATION MEMBER
MEMBER SINCE: 2000
[ Agenda
  Introduction
  Learning Points
  Return on Investment (ROI)
  Agile Programming Strategy
  Motivation
  About Test Driven Development (TDD)
  TDD Mantra
  TDD Basics & 3 Laws of TDD
  The Unit-Testing Framework
  ABAP Unit – A Design Methodology
  ABAP Unit – Demo
  Myths on Unit Testing & Best Practices
  Wrap Up
  Take Away Points & Q&A
Real Experience. Real Advantage. 2
[ About Intelligroup?
  US Based Company - Headquartered in Princeton, New Jersey.
  Over 2300+ Employees. (18% in United States, 77% in India, 5% Other)
  Global Provider of Innovative Consulting, Technology, & Outsourcing Services.
  Global Delivery Platform.
  Operations in the North America, India, Middle East, Japan, UK and Denmark.
  Certificates:
  SEI CMMI Level-5 Ver. 1.2 & PCMM Level 4 and ISO 9001:2000
  Awards:
  2010 SAP Pinnacle Award Finalist –Communities Leadership
  2009 SAP Pinnacle Award Winner –SDN
  2009 SAP Pinnacle Award Finalist -BPX, SOA Thought Leadership
  2009 SAP Award of Excellence –For Upgrades –Japan
  2008 SAP Pinnacle Award Finalist –eSOA
  2007 SAP Pinnacle Award –Technology Go To Market
  2007 SAP Tech-Ed award Winner

Real Experience. Real Advantage. 3


[ 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
projects.

Real Experience. Real Advantage. 4


[ Return on Investment (ROI)
Life Cycle Benefit - Investment
ROI =
Investment
No Feedback
Without
TDD
Number
of
Bugs


Done ?
With TDD
Done Sooner !

Continuous Feedback
Time
Development Quality Assurance
Integration & System Testing

Development Quality
Assurance
Traditional

Development
TDD


Investment

Life
Cycle
Benefit

Net
Return
Real Experience. Real Advantage. 5
[ Return on Investment (ROI) (cont…)
  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. 6


[ Agile Programming Strategy
  There is an urgent need for an agile programming strategy that
can help customer/businesses with following benefit.
  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. 7


[ 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. 8


[ Motivation (cont…)
  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. 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…)
  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…)
  Why TDD? (Cont…)
  The test is the executable specification.
  You start visualizing the goal first, design the API, then later think
about the possible implementation.
  You understand the program’s behavior by looking at the tests. The
tests describes you more than just an API description, they show
the dynamics, how to use the API.
  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…)
  Why TDD? (Cont…)
  Once you get test working, you know it is working.
  You use the tests as regression tests without much effort.
  The test gives us the courage to refactor.
  You can prove that everything still works after the Refactoring by
simply executing the tests.
  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.

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…)
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…)
  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


[ 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. 18
[ The Unit-Testing Framework
  Unit testing frameworks are quite popular in the object-
oriented 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. 19


[ 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. 20


[ ABAP Unit – A Design Methodology (cont…)
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. 21


[ ABAP Unit – A Design Methodology (cont…)
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.
  The additional test code does not negatively affect the
production system since test classes are not generated in
production systems.
  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. 22
[ ABAP Unit – A Design Methodology (cont…)
ABAP Unit Test Organization.

Real Experience. Real Advantage. 23


[ ABAP Unit – A Design Methodology (cont…)
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. 24


[ ABAP Unit – A Design Methodology (cont…)
Main Components of ABAP Unit (cont…)
  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. 25


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

Real Experience. Real Advantage. 26


[ ABAP Unit – A Design Methodology (cont…)
Main Components of ABAP Unit (cont…)
  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. 27


[ ABAP Unit – A Design Methodology (cont…)
Main Components of ABAP Unit (cont…)
  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. 28


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

Real Experience. Real Advantage. 29


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

Real Experience. Real Advantage. 30


[ ABAP Unit – A Design Methodology (cont…)
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. 31


[ ABAP Unit – A Design Methodology (cont…)
ABAP Unit Load Concepts (cont…)
  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. 32


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

Real Experience. Real Advantage. 33


[ ABAP Unit – A Design Methodology (cont…)
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. 34


[ ABAP Unit – A Design Methodology (cont…)
Unit Test Flow Control in case of Failure

Real Experience. Real Advantage. 35


[ ABAP Unit – Demo
  Simple Tip calculation program.
(Consider standard 15 percent for easy calculation)

Real Experience. Real Advantage. 36


[ ABAP Unit – Demo (cont…)
  Simple test for testing this Tip calculation routine:

Real Experience. Real Advantage. 37


[ ABAP Unit – Demo (cont…)
  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. 38
[ ABAP Unit – Demo (cont…)
  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. 39
[ ABAP Unit – Demo (cont…)
  Understanding ABAP Unit Result Display (cont…)
  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. 40


[ ABAP Unit – Demo (cont…)
  Understanding ABAP Unit Result Display (cont…)
  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. 41


[ ABAP Unit – Demo (cont…)
  Understanding ABAP Unit Result Display (cont…)
  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. 42


[ ABAP Unit – Demo (cont…)
  Understanding ABAP Unit Result Display (cont…)
  Reviewing the CALC_TIP_AMOUNT form routine

Real Experience. Real Advantage. 43


[ ABAP Unit – Demo (cont…)
  Understanding ABAP Unit Result Display (cont…)
  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. 44
[ 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.

  We won’t need to constantly validate our code each time we


make changes.
  Finding the bugs early means far less time fixing them later.
  If our unit tests are good, we won’t need to spend hours or days
tracking down bugs in the most fundamental layers of our code.

Real Experience. Real Advantage. 45


[ Myths around Unit Testing (cont…)
  Myth #2 : Will it not all my unit tests just get outdated?
-  No. Unit tests are not parallel to your code like documentation.
You will be driving your code with them.

  TDD is a programming paradigm.


  Unit tests are not an artifact of your code. They represent how
you write it!
  However, if you don’t appreciate unit testing, they likely will not
be worth the time to create.

Real Experience. Real Advantage. 46


[ Myths around Unit Testing (cont…)
  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.

  Do they pay us to test our code?


  Do they pay us to use design pattern?
  Do they pay us to use UML?

Real Experience. Real Advantage. 47


[ 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. 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 language.
  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: jaya.kumar@intelligroup.com

Further Info: Pat Gray - Director Marketing

Email: pat.gray@intelligroup.com

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
]
[ SESSION CODE:
1106

Real Experience. Real Advantage. 53

Вам также может понравиться