Professional Documents
Culture Documents
Software Design For Testability
Software Design For Testability
Software Design For Testability
Amr Medhat
13 - 4 - 2006 -
Outline
Testability: what, why, how and when? The Design Dilemma Testability Features Techniques for Developing QualityOriented Software
Testability .. What?
Controllability: the ability to apply inputs to s/w under test & place it in specified states Visibility: the ability to observe states and outputs [what we see is what we test]
Testability .. Why?
Improve software quality Ease the test process and more support for test automation Cost reduction
The average software company spends 60-80% of its development costs on supporting [Lidor Wyssocky]
Testability .. How?
Testability .. When?
From Day 1
Testers has to be engaged early in the product development cycle The product has to be open for design changes to meet the testing requirements
Using the perfect tools.... But in the wrong way ... !!! ?
modularity? scalability? reliability? reusability? maintainability?
Rigidity Fragility Immobility Viscosity Lack of Abstraction Violation of Encapsulation Interdependence {between Objects & Layers}
Separate interface from its implementation use overriding to support polymorphism When deriving a class, inherit interface not implementation Implementation is either Extended Or, Overridden Polymorphism
Encapsulation
Data are Private, Methods are Public, & Implementation Details are Hidden Encapsulating the data and the functions operating on this data.
myThing[] things = thingManager.getThingList(); for (int i = 0; i < things.length; i++) { myThing thing = things[i]; if (thing.getName().equals(thingName)) { return thingManager.delete(thing); } } return thingManager.deleteThingNamed(thingName);
Interdependence
(ADP)
3 ways of communication
Invoke Method Superiority Relation Raise Trigger Inferiority Relation Message Passing Peer Relation
OCP Example
Copy
Copy
Reader
Writer
Write Printer
Keyboard Reader
Printer Writer
Disk Writer
Read Keyboard
Write Printer
Write Disk
LSP Example
class Bird { public: virtual void fly(); }; class Parrot : public Bird { public: virtual void mimic(); };
class Penguin : public Bird { public: void fly() { error (Penguins dont fly!); } };
Does not model: Penguins cant fly It models Penguins may fly, but if they try it is error Run-time error if attempt to fly not desirable
Design for Testability
Testability Features
Logging events & verbose mode support Assertions {make assumptions explicit} Test points (fault injection hooks) Scriptable installation process
Detecting internal errors before they propagate Knowing the source and place of the problem easily Ability to reproduce bugs many times Time stamps may be useful in logging Well describe and identifying the logged event Agree on a standard format of logging all over the system [The use of a stand-alone Logger] Use variable levels of logging Throwing exceptions upon an assertions violation may be useful rather than just logging it as a warning
Notes
Development Techniques
Defensive Programming
Assertions everywhere
Design by Contract
Preconditions
what must be true when a method is invoked
Postconditions
what must be true after a method completes successfully.
Class invariants
what must be true about each instance of a class.
Test-Driven Development
Write tests first, then write the minimal code the makes this test pass Tests provide the required specification for the developer {A part of the documentation} Provides rapid feedback for the developer Emphasizes fast, incremental development Functionality is added in very small chunks Ensures 100% thoroughly unit tested code
Summary
Design Better
Use interfaces Dont violate encapsulation Avoid interdependence and cycles Class single responsibility Class open for extension closed for modification Class substitution (parent is more general than its child)
Logging Assertions Fault injection hooks
References
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11.
Bret Pettichord, Design for Testability Jeffery E. Payne, Roger T. Alexander, Charles D. Hutchinson, Design-for-Testability for Object-Oriented Software Robert Martin, OO Design Quality Metrics An Analysis of Dependencies Rahul Tyagi, Two principles to help create robust, reusable objectoriented design apps Robert C. Martin, Design Principles and Design Patterns Matt Weisfeld, "Object-Oriented Thought Process", Second Edition, Sams Publishing Wikipedia http://www.johndeacon.net/OOAandD/top25GoldenRules.asp http://www.artima.com/weblogs/viewpost.jsp?thread=132358 http://labs.cs.utt.ro/labs/ip2/html
http://www.mertner.com/confluence/display/MbUnit/Design+Standards