Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.

Slides:



Advertisements
Similar presentations
Computer Science 209 Testing With JUnit. Why Test? I don ’ t have time, I ’ ve got a deadline to meet The more pressure I feel, the fewer tests I will.
Advertisements

J-Unit Framework.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
BTS530: Major Project Planning and Design Iterative Development References: Agile & Iterative Development, by Craig Larman, 2004, Addison Wesley. Agile.
Chapter 21 Test Driven development. TDD Write the test first Show that test fails Write code to pass the test Run whole suite of tests! This is unit testing.
Test-Driven Development and Refactoring Project 3 Lecture 1 CPSC 315 – Programming Studio Fall 2009.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
25-Jun-15 Refactoring III. General philosophy A refactoring is just a way of rearranging code Refactorings are used to solve problems If there’s no problem,
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Test-Driven Development “Test first, develop later!” –OCUnit.
Refactoring Lecture 5 CIS 6101 Software Processes and Metrics.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Refactoring Improving the structure of existing code Refactoring1.
17-Oct-15 Refactoring. 2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Refactoring1 Improving the structure of existing code.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Coding.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
A tool for test-driven development
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
Object Oriented Analysis and Design 1 Chapter 9 From Design to Implementation  Implementation Model  Forward, Reverse, and Round-Trip Engineering  Mapping.
Refactoring Advanced Software Engineering Dr Nuha El-Khalili.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
Refactoring. 2 Process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that.
Testing JUnit Testing. Testing Testing can mean many different things It certainly includes running a completed program with various inputs It also includes.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
10-Jun-16.  Refactoring is: ◦ restructuring (rearranging) code... ◦...in a series of small, semantics-preserving transformations (i.e. the code keeps.
Unit testing with NUnit Anne Lam & Chris James CMPS 4113 – Software Engineering April 15, 2015.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Mapping Designs to Code. It specify how to map the design into object oriented language The UML artifacts created during the design work, the interaction.
Principles and examples
TESTING TEST DRIVEN DEVELOPMENT
Module Road Map Refactoring Why Refactoring? Examples
Computer Science 209 Testing With JUnit.
Software Engineering 1, CS 355 Unit Testing with JUnit
Overview of Eclipse Lectures
Test Driven development
Test Driven development
Improving the structure of existing code
Refactoring.
Presentation transcript:

Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring

Software Engineering 2 Object-oriented Analysis and Design Test-Driven Development 1  The test is written first, imagining the code to be tested is written.  Advantages include m The unit tests actually get written m Programmer satisfaction leading to more consistent test  The psychological aspects of development can't be ignored programming is a human endeavor. m Clarification of detailed interface and behavior  That reflection improves or clarifies the detailed design. m Provable, repeatable, automated verification m The confidence to change things  When a developer needs to change existing code written by themselves, there is a unit test suite that can be run, providing immediate feedback if the change caused an error.

Software Engineering 3 Object-oriented Analysis and Design Test-Driven Development 2  The popular unit testing framework is the xUnit family m For Java, the popular version is JUnit. NUnit for.NET m JUnit is integrated into most of the Java IDEs, such as Eclipse.  Example fo Sale class, write a unit testing method in a SaleTest class m Create a Salethe thing to be tested (also known as the fixture). m Add some line items to it with the makeLineItem method (the makeLineItem method is the public method we wish to test). m Ask for the total, and verify that it is the expected value, using the assertTrue method. JUnit will indicate a failure if any assertTrue statement does not evaluate to true.  Each testing method follows this pattern: m Create the fixture. m Do something to it (some operation that you want to test). m Evaluate that the results are as expected.

Software Engineering 4 Object-oriented Analysis and Design Test-Driven Development 3  To use JUnit, to create a test class that extends the JUnit TestCase class; the test class inherits various unit testing behaviors. m create a separate testing method for each Sale method. m write unit testing methods for each public method of the Sale class.  To test method doFoo, it is an idiom to name the testing method testDoFoo. public class SaleTest extends TestCase { // … // test the Sale.makeLineItem method public void testMakeLineItem() { // STEP 1: CREATE THE FIXTURE // -this is the object to test // -it is an idiom to name it 'fixture' // -it is often defined as an instance field rather than // a local variable Sale fixture = new Sale(); // set up supporting objects for the test Money total = new Money( 7.5 ); Money price = new Money( 2.5 );

Software Engineering 5 Object-oriented Analysis and Design Test-Driven Development 4 ItemID id = new ItemID( 1 ); ProductDescription desc = new ProductDescription( id, price, "product 1" ); // STEP 2: EXECUTE THE METHOD TO TEST // NOTE: We write this code **imagining** there // is a makeLineItem method. This act of imagination // as we write the test tends to improve or clarify // our understanding of the detailed interface to // to the object. Thus TDD has the side-benefit of // clarifying the detailed object design. // test makeLineItem sale.makeLineItem( desc, 1 ); sale.makeLineItem( desc, 2 ); // STEP 3: EVALUATE THE RESULTS // there could be many assertTrue statements // for a complex evaluation // verify the total is 7.5 assertTrue( sale.getTotal().equals( total )); }

Software Engineering 6 Object-oriented Analysis and Design Refactoring 1  Refactoring m is a structured, disciplined method to rewrite or restructure existing code without changing its external behavior, applying small transformation steps combined with re- executing tests each step.  The essence of refactoring m applying small behavior preserving transformations (each called a 'refactoring'), one at a time. m After each transformation, the unit tests are re-executed to prove that the refactoring did not cause a regression (failure).

Software Engineering 7 Object-oriented Analysis and Design Refactoring 2  The activities and goals refactoring m remove duplicate code m improve clarity m make long methods shorter m remove the use of hard-coded literal constants  Some code smells include m duplicated code m big method m class with many instance variables m class with lots of code m strikingly similar subclasses m little or no use of interfaces in the design m high coupling between many objects

Software Engineering 8 Object-oriented Analysis and Design In Java, for example, replace using the new operator and constructor call with invoking a helper method that creates the object (hiding the details). Replace Constructor Call with Factory Method Put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose. Introduce Explaining Variable (specialization of Extract Local Variable) Replace a literal constant with a constant variable.Extract Constant Transform a long method into a shorter one by factoring out a portion into a private helper method. Extract Method DescriptionRefactoring Refactoring 3  Refactorings example

Software Engineering 9 Object-oriented Analysis and Design Refactoring 4  Before introducing an explaining variable. // good method name, but the logic of the body is not clear boolean isLeapYear( int year ) { return( ( ( year % 400 ) == 0 ) || ( ( ( year % 4 ) == 0 ) && ( ( year % 100 ) != 0 ) ) ); }  After introducing an explaining variable. // that's better! boolean isLeapYear( int year ) { boolean isFourthYear = ( ( year % 4 ) == 0 ); boolean isHundrethYear = ( ( year % 100 ) == 0); boolean is4HundrethYear = ( ( year % 400 ) == 0); return(is4HundrethYear||(isFourthYear&& !isHundrethYear)); }  IDE Support for Refactoring: Eclipse IDE