MARCDERE LOUIS [ ASUG INSTALLATION MEMBER MEMBER SINCE: 2009 RAJEEV KAPUR [ ASUG INSTALLATION MEMBER MEMBER SINCE: 2008 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
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
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
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.
Done Sooner ! Development Quality Assurance Integration & System Testing Number of Bugs Time Continuous Feedback Without TDD With TDD No Feedback Done ? TDD Development Traditional Development Quality Assurance Investment Life Cycle Benefit Net Return ROI = Life Cycle Benefit - Investment Investment
(cont…) Better Code Design Usable Code Safety Net Modularity Instant Feedback Increased Productivity WYGIWYM (What You Get Is What You Meant) Documentation
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.”
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).
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!
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.
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.
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”
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.
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).
Write (just enough) Production Code to pass Run the Test Write (just enough) Production Code to compile Run the Test Refactoring Start Write a Test Run the Test 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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. 38 ABAP Unit – Demo (cont…)
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. 39 ABAP Unit – Demo (cont…)
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 40 ABAP Unit – Demo (cont…)
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. 41 ABAP Unit – Demo (cont…)
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. 42 ABAP Unit – Demo (cont…)
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. 44 ABAP Unit – Demo (cont…)
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.
(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.
(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?
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”
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.
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.
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
for participating. SESSION CODE: 1106 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