Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Inheritance Reserved word protected Reserved word super
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object-oriented Programming Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object-Oriented Software Testing. C-S 5462 Object-Oriented Software Testing Research confirms that testing methods proposed for procedural approach are.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Object-Oriented Testing
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
SNPL1 Woochang Lim What (Variable) + How (Function) = Object Objects are the physical and conceptual things we find in the universe around us. Object-Oriented.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Information System Design (IT60105) Lecture 26 Object-Oriented System Testing.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Introduction to OOP CPS235: Introduction.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Object-Oriented Programming Concepts
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Object-Oriented Analysis and Design
Inheritance and Polymorphism
The Object-Oriented Thought Process Chapter 1
Types of Programming Languages
Testing with OO OO has several key concepts:
Object-Oriented PHP (1)
Final and Abstract Classes
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology. –Find faults and fixing them before releasing –Show that the software works (at least in chosen areas) In OO we are especially interested in testing because we want to “reuse” the tested objects. Does our approach have to change because of OO? –Not really but we do have to focus on a few characteristics

Basic Levels of Testing in OO Unit Testing Unit A Integration test Unit X Integration test System Test What is a “unit” In OO? Same as before; testing “related” units as a group Same as before; testing the whole system

What is a “unit” in OO ? A “unit” of code for unit testing before was left with a loose definition of a module, –a sequence of code that performs something, –a compiled program, –something assigned to 1 person In OO we have the same problem with the definition of a “unit” in unit testing. Is it: –A method? –A set of code that performs something? –A Class? Considering Class as a “unit” has a lot of support among testers., but it is not a fixed law or something

Class and Object instantiation There are at least 2 concerns with viewing class as a “unit”: 1.We should view Class as a fundamental unit and test it as an independent entity by itself. What and how much “scaffolding” code do we need to write to invoke and test this Class. –e.g. Class1 x1; –You may need to write the public static void “main” method to test the class (possible scaffolding code) 2.If there is a large method inside the Class and we know (via white-box testing approach) that the method invokes other methods in other Classes, should we follow (test) the thread of methods across different Classes? – the module-module (mm-path) approach may be employed for testing the “thread” of methods, that may go beyond one Class scaffolding integration

3 important concepts in OO 1.Encapsulation: –In OO, the notion of self contained and only know/operate within its own domain is very important –Good encapsulation gives rise to good reusable classes that can be composed with other classes to deliver more service. –We are also after highly cohesive and loosely coupled units with encapsulation. 2.Inheritance: –A way to gain on the concept of reuse through a class taking (inheriting) portions from a super class. 3.Overload/Polymorphism –Another way to gain on the reuse concept through using parts of class or method for different purposes based on the passed parameter or based on run time usage (late binding). How do these concepts affect us in testing?

Encapsulation Both data and methods may be encapsulated and protected. Depending on the programming language used there may be slight differences in implementing the encapsulation mechanism: –Public (visible) pretty much same in all languages –Private (hidden) only methods in the class can access –Protected (secret)- - - subclass methods can access Test the access of encapsulated data Test the access of encapsulated methods Read the discussion on the positions of “dial’ and “lever” of the windshield wiper example on page 287. The exchange of “positions” of these two may be different depends on the design and how the data is encapsulated.

Inheritance Example (JAVA) One may want to test the methods in the inherited class which were previously tested in the super- class. An example is a method, m1, that only returns positive integers and is used in another method, m2, as a divisor. An inherited subclass modifies (over rides) m1 to allow all integers, including zero. (We will need to ensure that the inherited and previously tested m2 is retested). public class Example { int y; public int m1 { - y = (test * test) +1; return y; } public void m2 { - z = w / y ; } import Example ; public class Example2 extends Example { - public int m1 { - y = test * test ; return y; } - }

Similar Inheritance Problem Example (C++) Class Test1 { int some_variable;. int method1 ( ) { return 1; } int method2 ( ) { return 5 / method1( ); } } Class Test_child : Public Test1 { int mehtod1 ( ) { return 0; } } Original CLASS Inherited CLASS over-riding method1

Inheritance Do we need to test multiple inheritance, where there may be conflicts in inheritance? –Two subclasses, Sub1 and Sub2, are inherited from a super- class, S1, and they both modify the method, method1, but differently. If we further inherit from both Sub1 and Sub2, what should we expect method1 to be like? Varx : int method1 ( ) method2 ( ) Mother Sibling 1Sibling 2 Grand_Sibling 1 (method1) Note: JAVA does not allow multiple inheritance overriddenOverridden differently

Inheritance Inheritance provides us with the ability to reuse (and save both implementation and testing expenses) what is there and make incremental changes by creating a subclass by either adding to or modifying the : –Instance variable –Methods Using “Flattened Class” concept: 1.One may want to test subclass access of the “encapsulated” data or methods in the super-class. 2.One may want to test all the methods in the inherited class which were previously tested in the super-class. An example is a method, m1, that only returns positive integers and is used in another method, m2, as a divisor. An inherited subclass modifies m1 to allow all integers, including zero. (We will need to ensure that the inherited and previously tested m2 is retested). 3.Do we need to test multiple inheritance, where there may be conflicts in inheritance? Two subclasses, Sub1 and Sub2, are inherited from a super-class, S1, and they both modify the method, m1, but differently. If we further inherit from both Sub1 and Sub2, what should we expect m1 to be like? Testing flattened Classes of all inherited subclasses will cause duplication of testing the same (non-overridden) methods that appear in all the inherited subclasses

Polymorphism and Late Binding (in C++) Polymorphism and late binding is a special feature that allows us to have multiple behavior based on which class a statement is bound to. e.g. in C++ Class Cube{ protected: float length; public: virtual float area( ) {return (length* length);} // area of square void set_length(float z) {length = z} // set passed parameter, length void volume( ) {cout<< “volume is” << area( ) * length;} // the area( ) method is not bound }; Class Cylinder: public Cube { virtual float area( ) {return (3.14* length**2);} // area of circle. }.. Cube c1, Cylinder cyl1;. c1.volume( ); // give us the volume of cube cyl1.volume( ); // give us the volume of circle Both classes must be tested, especially “if” one of them is a division by length.

Polymorphism via dynamic instantiation (in JAVA) class Shape { } - class Rectangle extends Shape { } class Triangle extends Shape { } class Circle extends Shape { } - import Rectangle; import Triangle; import Circle; public class Example { public static void main ( String[ ] args) - String S = get_shape_type ( ) ; // some method that reads and returns a string to S Shape myFigure; myFigure = (Shape) java.lang.class.forName(S).newinstance( ); /* myFigure is instantiated to what the string read in --- hopefully it is rectangle, circle or triangle */ /* Note that it is “casted’ as type Shape, which is the parent Class */ - } Is equivalence class testing of valid vs invalid shape good enough? Do you need to test everyone of the valid shape?

Polymorphism via “interface” class (in JAVA) public interface Speaker { public void speak( ); } public class cat implements Speaker { public void speak ( ) { system.out.println(“meow”); } } public class dog implements Speaker { public void speak ( ) { system.out.println(“woof woof”);} } Import cat; Import dog; Public class AnimalTalk { public static void main ( string[ ] arg) { Speaker current; system.out.println(“enter 1 or 2”); /* 1 = cat and 2 = dog */ int j = keyboard.readInt ( ); if (j == 1) current = new cat ( ); else current = new dog ( ); - current.speak ( ); - } All Branch Test will take care of both cat and dog ?-----

OO concepts and testing Assume a “unit” is a CLASS. –Inheritance can be dealt with at the subclass level through unit testing the “flattened” subclass as the basic “unit”. Flattened class is the class expanded to include all the inherited methods (operations) and variables/constants (attributes) –Encapsulation can be dealt with by both unit testing the individual class and integration testing the related classes. (More burden is placed on the integration test especially if the design is “tightly” coupled classes.) –Overload and polymorphism can be dealt with unit testing the overload or the polymorphic function individually and then use integration testing to further test the overriding and the polymorphism mechanisms

Basic Levels of Testing in OO Unit Testing Unit A Integration test Unit X Integration test System Test Test class and class Inheritance Test relations and Inheritance; Encapsulation; Polymorphism Same as before; testing the whole system

More Considerations of Unit Testing 1.Each method as a “unit” –Once a method of interest is designed and written one still needs to develop “scaffolding” test code such as stubs for the yet-to-complete other methods and driver for the “main” program to test the finished method. 2.The completed class as a “unit” –All the methods are and the data are available for testing. –We may view the class in 3 ways: –Source code level (reviewing the code – possibly with “flattened” class) –Compiled code level ( code inheritance takes place and all the classes in the hierarchy above the class must be available) –Execution code level (code is constructed and the interactions among the methods can be tested much like an integration test within a class) 3.Also, we need to deal with abstract method and abstract class which are generic concept. (It is almost like a stub itself).

Integration Testing Assumes the unit or units are unit-tested. Use the collaboration diagram and the sequence diagram from UML to design the integration test cases Collaboration diagram where boxes are classes and numbered arrows indicate sequenced messages Sequence diagram where the boxes are classes and the arrows indicate the sequence of messages