Beruflich Dokumente
Kultur Dokumente
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
John McGregor and David A. Sykes
Addison Wesley Object Technology Series, 2001
ISBN 0-201-32564-0
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 code is written with a form of execution.
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
January 20, 2002
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
10
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
January 20, 2002
11
12
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.
January 20, 2002
13
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.
January 20, 2002
14
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
15
16
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
January 20, 2002
17
18
19
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; doesnt allow
declaration of any state variables
C++: declare an abstract base class with only public, pure
virtual methods
January 20, 2002
20
21
Class
Set of objects 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
January 20, 2002
22
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!)
January 20, 2002
23
ClassA
SubClassA2
SubClassA1
24
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.
January 20, 2002
25
26
27
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 operations method may violate invariants during
execution but it must hold again by completion.
January 20, 2002
28
Preconditions
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 an operation be performed
29
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
30
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
31
32
33
34
35
36
37
Class implementation
Describes how an object represents its attributes and carries
out its operations. It is made of several components:
A set of data 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.
January 20, 2002
38
39
40
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
January 20, 2002
41
42
43
44
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
45
46
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 objects 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
January 20, 2002
47
48
49
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
50
51
52
53
54
B
doIt( ) {...}
C
doIt( ) {...}
D
doIt( ) {...}
55
Parametric Polymorphism
The capability to define a type in terms of one or more
parameters rather like a macro
C++ provides this with the concept of templates
a compile-time ability to instantiate a new class
new because an actual parameter is provided for the
formal parameter (at compile-time) in the definition
Instances of the new class can then also be created
Used a lot in the C++ Standard Template Library
Almost looks like a kind of inheritance but it isnt
If the template works for one instantiation, no
guarantee it will work for another
January 20, 2002
56
Parametric Polymorphism
from a testing perspective
Need to inspect details of the template code to
understand what it will do with various parameters
It is possible to write templated drivers for testing
many parts of templates
57
Abstraction
The process of removing detail from a representation.
Allows us to look at a problem in various levels of
detail.
leave out details that are irrelevant for a given
consideration
OO technologies use abstraction extensively
inheritance hierarchy, for example
system models whose detail increases during
development
58
Layers of abstraction
from a testing perspective
Layers of abstraction in the development process are
paralleled by layers of testing analysis
If we begin testing analysis with the highest levels of
abstraction of development models,
we provide a more thorough examination of the
development product
and, therefore, a more effective and accurate set of
tests
59