1 / 59

Testing Object-Oriented Software – Part One Object-Oriented Principles from a testing perspective Test early, test oft

Testing Object-Oriented Software – Part One Object-Oriented Principles from a testing perspective Test early, test often, test enough. Software Engineering of Standalone Programs University of Colorado. Primary reference. A Practical Guide to Testing Object-Oriented Software

nasnan
Download Presentation

Testing Object-Oriented Software – Part One Object-Oriented Principles from a testing perspective Test early, test oft

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Testing Object-Oriented Software – Part OneObject-Oriented Principles from a testing perspectiveTest early, test often, test enough. Software Engineering of Standalone Programs University of Colorado ECEN5033 - OO Testing - University of Colorado

  2. Primary reference • A Practical Guide to Testing Object-Oriented Software • John McGregor and David A. Sykes • Addison Wesley – Object Technology Series, 2001 • ISBN 0-201-32564-0 ECEN5033 - OO Testing - University of Colorado

  3. What is software testing? • The evaluation of the work products created during a software development effort • Done throughout development effort • Applied to all development products (models) before as well as after code is written • More specifically • The process of uncovering evidence of defects • Since a defect can be introduced at any phase, testing efforts find defects in all phases • Testing is not the debugging, isolation, or repair of bugs ECEN5033 - OO Testing - University of Colorado

  4. What is software? • The instruction codes and data necessary to accomplish some task on a computer or microprocessor • All representations of those instructions and data • Analogy • Architects and builders can examine blueprints to spot problems • We can examine analysis and design models before the codeis written with a form of “execution.” ECEN5033 - OO Testing - University of Colorado

  5. Assumptions • Development process is incremental with iterations within each increment • Models are expressed in UML • Software design in accordance with good design principles • inheritance • data hiding • abstraction • low coupling • high cohesion ECEN5033 - OO Testing - University of Colorado

  6. Testing vs. Quality Assurance • Quality Assurance • Responsible for test plans and system testing • Monitor testing during development • Keep statistics • Testing is a necessary but insufficient part of a QA process • QA addresses activities designed to • prevent defects • remove defects • Testing helps in identifying problems and failures • Testing helps QA by identifying them early in dev. ECEN5033 - OO Testing - University of Colorado

  7. What’s special about testing OO software? • Features such as class inheritance and interfaces support polymorphism in which code manipulates objects without their exact class being known • Testers must ensure the code works no matter what the exact class of such objects might be. • Features that support data hiding complicate testing because operations must be added to a class interface (by the developer) just to support testing ECEN5033 - OO Testing - University of Colorado

  8. OO Testing Is Still Testing • We still do • unit testing but we change the definition of unit • integration testing to make sure subsystems work correctly together • system testing to verify that requirements are met • regression testing to make sure previous functionality still works after new functionality is added ECEN5033 - OO Testing - University of Colorado

  9. OO Testing Is Not Just Old Style Testing • Fundamental aspect of OO software • OO Software is designed as a set of objects that essentially model a problem and then collaborate to effect a solution • While the solution may change over time, the structure and componentsof the problem do not change as frequently • a program structured from the problem is more adaptable to changes later • components derived from the problem can be reused in development of other programs to solve related problems ECEN5033 - OO Testing - University of Colorado

  10. Benefit • Many analysis models map straightforwardly to design models which, in turn, map to code • Start testing during analysis • Refine the same tests for design • Refine those tests for code ECEN5033 - OO Testing - University of Colorado

  11. Advantages of testing analysis and design models • Test cases can be identified earlier in the process, even while determining requirements • Early test cases help analysts and designers to • better understand and express requirements • ensure that specified requirements are testable • Bugs can be found early – saving time and money • Test cases can be reviewed for correctness early in the project • If test cases are applied to models early, misunderstandings of requirements on the part of testers can be corrected early ECEN5033 - OO Testing - University of Colorado

  12. Avoid the “bugging phase” • In other words, model testing helps to ensure that testers and developers have a consistent understanding of the system requirements early in the project. • However, code testing is still important ECEN5033 - OO Testing - University of Colorado

  13. Categories of OO Testing • Model testing • Class testing instead of unit testing • Class interaction testing instead of integration testing • System and subsystem testing • Acceptance testing • Self-testing • Should you try to apply all of these? Probably not if you want to be taken seriously and be employed. • You should learn to recognize approaches and techniques that will apply to your project in a useful and affordable way. ECEN5033 - OO Testing - University of Colorado

  14. Testing perspective • Skeptical, objective, thorough, systematic • Look at any development product and question its validity • Attitude that should be held by a developer as well as a full-time tester • To ensure • a. the software will do what it is supposed to do • b. the software will not do what it is not supposed to do • Ensuring “a.” does not automatically ensure “b.” ECEN5033 - OO Testing - University of Colorado

  15. Object • An operational entity that encapsulates both specific data values and the code that manipulates those values. • Provides the mechanisms needed to • receive messages • dispatch methods • return results • associates instance attributes with methods ECEN5033 - OO Testing - University of Colorado

  16. Objects from a testing perspective • Encapsulates – the complete definition of the object is easy to identify, easy to pass around, easy to manipulate • Hides information – can make changes to the object hard to observe which makes checking test results difficult • Has a state that persists for its life. This state can become inconsistent and can be the source of incorrect behavior • Has a lifetime – can be examined during its lifetime to check if it is in the right state based on its lifetime. • Common source of failures – construction of an object too late or destruction of it too early ECEN5033 - OO Testing - University of Colorado

  17. Message • Message – a request that an operation be performed by some object. • can include actual parameters used to perform that operation • receiver can return a value • OO program is a community of objects that collaborate to solve a problem. • This is achieved by sending messages to one another • Can result in a return value • Can result in an exception from receiver to sender ECEN5033 - OO Testing - University of Colorado

  18. Messages from a testing perspective • A message • has a sender who determines when to send and may make an incorrect decision about this • has a receiver • may not be ready for the specific msg it receives • may not take the correct action if msg is unexpected • may include actual parameters • used by or updated by the receiver • objects passed as parameters must • be in correct states before and after the message is processed • implement the interfaces expected by the receiver ECEN5033 - OO Testing - University of Colorado

  19. Those issues are the primary focus of interaction testing ECEN5033 - OO Testing - University of Colorado

  20. Interface • Aggregation of behavioral declarations • Example: a set of behaviors related to being a moving item on a screen such as the ball in (the old game of) Pong • Building block for specifications • A specification is the total set of public behaviors for a class. --------------- • Java: has a syntactic construct interface; doesn’t allow declaration of any state variables • C++: declare an abstract base class with only public, pure virtual methods ECEN5033 - OO Testing - University of Colorado

  21. Interfaces from a testing perspective • Interface encapsulates operation specifications which • build the specifications of larger groupings such as classes • If it contains behaviors that do not belong with the other behaviors, implementations of the interface will have unsatisfactory designs • Interface has relationships with other interfaces and classes. • may be specified as the parameter type for a behavior to allow any implementer of that interface to be passed as a parameter • Interface describes a set of behavior declarations whether or not we use the interface syntax ECEN5033 - OO Testing - University of Colorado

  22. Class • Set ofobjects that share a common conceptual basis. • Class definition says what members (objects) of the set look like, what they have in common. • Objects form the basic elements for executing OO programs • Classes are the basic elements for defining OO programs • Any concept to be represented in a program must first be defined in a class. • Then objects defined by that class are created (instantiation) and are called instances. object = instance ECEN5033 - OO Testing - University of Colorado

  23. Class as object Note: • OO languages usually allow a class to be an object itself and can have operations and attributes defined for it • In C++ and Java, operations and data values associated with a class are identified by the keyword static and these operations are called static operations • Public static operations in a class specification mean • the class itself is an object that can be messaged • we must treat the class as an object and create tests for the class as well as for its instances • Scary thought: non-constant static data associated with a class can affect the behavior of the instances (yikes!) ECEN5033 - OO Testing - University of Colorado

  24. ClassA SubClassA1 SubClassA2 ECEN5033 - OO Testing - University of Colorado

  25. Operations • A class specification includes a specification for each of the operations that can be performed by each of its instances • An operation is an action that can be applied to an object to obtain a certain effect. • Accessor (inspector) operations – provide information about the object but do not change the object • Modifier (mutator) operations – change the state of the object by setting one or more attributes to have new values (perhaps not every time) • Accessors are tested differently than modifiers. ECEN5033 - OO Testing - University of Colorado

  26. Two special operations • Constructor – a class object operation used to create a new object • includes initializing a new instance when it comes into existence • Destructor – an instance object operation used to perform any processing needed just prior to the end of the object’s lifetime • Differ from accessors & modifiers • invoked implicitly as a result of the birth and death of objects ECEN5033 - OO Testing - University of Colorado

  27. What do we expect of a class specification? • A description of what a class represents. • It’s either a concept in the problem being solved or • in the solution to that problem • Some meaning and constraints to be associated with each of the operations defined in the class specification • So ... each operation should have a specification that describes what it does, including its preconditions and invariants ECEN5033 - OO Testing - University of Colorado

  28. Reminder • Preconditions • Conditions that must hold before the operation can be performed. • Post conditions • Conditions that must hold after the operation is performed. • Invariants • Conditions that must always hold within the lifetime of the object • An operation’s method may violate invariants during execution but it must “hold” again by completion. ECEN5033 - OO Testing - University of Colorado

  29. Preconditions • Usually stated in terms of one or more of the following: • attributes of the objectcontaining the operation • attributes of any actual parameters in the message requesting that an operation be performed ECEN5033 - OO Testing - University of Colorado

  30. Post conditions • Usually stated in terms of one or more of the following: • attributes of the object containing the operation • attributes of any actual parameters in the message requesting that the operation be performed • the value of any reply • the exceptions that might be raised ECEN5033 - OO Testing - University of Colorado

  31. Invariants • A class invariant describes a set of operating boundaries for an instance of a class • It is possible to define interface invariants and operational invariants • A class invariant can be treated as an implied post condition for EACH operation in the class • Usually stated in terms of • attributes of an object • states of an object ECEN5033 - OO Testing - University of Colorado

  32. Behavior of the instances of a class • The aggregate of the specificationsof all of the operations in a class provides • part of the description of the behavior of its instances • Behavior • difficult to infer from operation specifications alone • typically designed and represented at a higher form of abstraction • defining a set of states for an instance • describing how various operations effect transitions from state to state ECEN5033 - OO Testing - University of Colorado

  33. To write a specification for an operation • To define the interface between the receiver and the sender • Contract approach – emphasizes preconditions but has simpler post conditions • Defensive programming approach -- emphasizes post conditions but has simpler preconditions ECEN5033 - OO Testing - University of Colorado

  34. Contract approach to class design from a testing perspective • Preconditions specify obligation of the sender • If met, receiver is obligated to meet the requirements set form in the post conditions and class invariant • Care must be taken in the design of the class interface to ensure that • the preconditions are sufficient to allow a receiver to meet the post conditions • a sender can determine whether all preconditions are met before sending a message • post conditions address all possible outcomes of an operation, assuming preconditions were met ECEN5033 - OO Testing - University of Colorado

  35. Defensive approach to class design from a testing perspective • Interface defined primarily in terms of the receiver and any assumptions it makes on its own state and the values of any inputs (arguments or global data) at the time of the request. • Operation typically returns some indication re status of the result of the request (success or failure), • traditionally as a return code associating a value with each possible outcome • can provide to sender an object that encapsulates the status of the request • Identify “garbage in” and eliminate “garbage out” by checking for improper values coming in and reporting the status of processing the request to the sender ECEN5033 - OO Testing - University of Colorado

  36. What does this mean for tester? • The approach used in an interface determines the types of testing that need to be done. • Contract approach • simplifies class testing • complicates interaction testing – must ensure any sender meets the preconditions • Defensive approach • complicates class testing – test cases must address all possible outcomes • complicates interaction testing – must ensure all possible outcomes are produced and that they are properly handled by the sender ECEN5033 - OO Testing - University of Colorado

  37. During design and design inspections of a class – how maintain testing perspective? • Review the preconditions and post conditions for testability • Are the constraints clearly stated? • Does the specification include the means by which one can check preconditions? (the sender does not want to be an expert on the receiver; receiver should explain how to check for the preconditions) ECEN5033 - OO Testing - University of Colorado

  38. Class implementation • Describes how an object represents its attributes and carries out its operations. It is made of several components: • A set ofdata values stored in data members (aka instance variables or variables) – some or all of the values associated with the attributes of an object. • A set of methods (aka member functions) – code used to implement an algorithm to accomplish an operation declared in the public or private class specification. • A set of constructors to initialize a new instance. • A destructor to handle any processing associated with destruction of an instance • A set of private operations in a private interface – provide support for the implementation of public operations. ECEN5033 - OO Testing - University of Colorado

  39. Importance of class testing • Classes define the building blocks for OO programs • A class is an abstraction of the commonalities among its instances – therefore, the testing process must ensure that a representative sample of members are selected for testing. ECEN5033 - OO Testing - University of Colorado

  40. Classes from a testing perspective • A class specification contains operations to construct instances. They may not properly initialize the attributes of new instances. • Class relies on collaboration to define its behaviors and attributes. The other classes may be implemented incorrectly and contribute to failure of the class that relies on them. • A class’ implementation “satisfies” its specification – does not mean the specification is correct. • Might not support all required operations; may perform them incorrectly. • Might not provide a way for a precondition to be checked by a sender before sending a message ECEN5033 - OO Testing - University of Colorado

  41. Inheritance • Relationship between classes that allows the definition of a new class based on the definition of an existing class. • allows reuse of both specification & implementation • important advantage: the preexisting class does not have to be modified or made aware of the new class • New class is called subclass or derived class • Parent class is called superclass or base class • Each class (except the root) has one or more ancestors; the chain of ancestors up to the root is called inheritance hierarchy ECEN5033 - OO Testing - University of Colorado

  42. Good OO Design Use of Inheritance • Used only to implement an is-a or is-a-kind-of relationship • Best use: with respect to specifications and not implementation – inclusion polymorphism, for example (more on that in a moment) ECEN5033 - OO Testing - University of Colorado

  43. Inheritance from a testing perspective • Provides a mechanism by which bugs can be propagated from a class to each of its descendants  • Important reason to test classes as they are developed to eliminate fault propagation • Provides a mechanism by which we can reuse test cases. • subclass inherits part of its specification and implementation from its superclass, potentially can reuse test cases from superclass to subclass • Models an is a kind of relationship • Use of inheritance solely for code reuse will probably lead to maintenance difficulties • Common mistake in OO development ECEN5033 - OO Testing - University of Colorado

  44. Inheritance models is-a-kind-of relationship • If D is a subclass of C, then D is a kind of C • If so, an instance of D can be used whenever an instance of C is expected • To work, the behavior of D must somehow conform to that which is associated with C • Behaviorof a class • observable states of an instance • the semantics associated with the operations defined for an instance of that class • Behavior of a subclass – incremental changes to the observable states and operations defined by its superclass ECEN5033 - OO Testing - University of Colorado

  45. Substitution Principle • Only the following changes are allowed in defining the behavior associated with a new subclass: • Preconditions for each operation must be the same or weaker – less constraining – than those of the superclass • Post conditions for each operation must be the same or stronger – do at least as much as defined by the superclass • Class invariant – must be the same or stronger; add more constraints ECEN5033 - OO Testing - University of Colorado

  46. Substitution Principle of Inheritancefrom a testing perspective • Developers must enforce (in inspections, if not before) the constraints of this principle on behavior changes • Observable states and all transitions between them associated with the superclass must be preserved by the subclass • The subclass may addtransitions between these states • The subclass may add observable states as long as each is either concurrent or a substate of an existing state • In other words, don’t use inheritance because you are too lazy to specify a class that is similar but is-not-a-kind-of ECEN5033 - OO Testing - University of Colorado

  47. Polymorphism • Ability to treat an object as belonging to more than one type. • Not necessarily the safest approach to programming • Supports designs that are flexible • Inclusion polymorphism is the occurrence of different forms in the same class • Can substitute an object whose specification matches another object’s specification for the latter object in a request for an operation • i.e., a sender can use an object as a parameter based on its implementation of an interface rather than its full class ECEN5033 - OO Testing - University of Colorado

  48. A class is a set of objects that share a common conceptual basis. • This definition is influenced primarily by associating inheritance and inclusion polymorphism • The class at the root (top of tree graph) establishes a common conceptual basis for all objects in the set. • A descendant refines the behavior established by the root class and intermediate ancestors • Objects in the descendant class are still in the set of objects in the root class – a subset of each of the sets defined by its ancestors ECEN5033 - OO Testing - University of Colorado

  49. Two perspectives of sets representing classes – both are useful during testing • Class’ perspective – each set contains all instances, maybe an infinite number; most easily represented with Venn diagrams • Executing program’s perspective – each set is drawn with one element per instance in existence. ECEN5033 - OO Testing - University of Colorado

  50. A Matter of Perspective • When a class is to be tested • outside context of any application program, we test it by selecting arbitrary instances using the class perspective • in context of an executing application program or in context of object persistence, we use the other perspective • ensure size of set is correct • ensure elements correspond to appropriate objects in the problem or solution ECEN5033 - OO Testing - University of Colorado

More Related