Software Design Patterns in Test Automation
3
1. Objectives2. What are Design Patterns?3. Design Pattern and Design Principle - what's the difference?4. Principles KISS Don’t Repeat Yourself.5. Patterns Multi-layered test solution Facade Chaining Template Method
Table of Content
Objectives• Present the concepts of „design pattern” & „de-sign principle” • Show why these concepts are important• Describe chosen patterns & principles.• All presented patterns have examples in C# but can be used with most Object-Oriented Languages• Target Audience: Testers who are writing automated tests and want to dive deeper into programing world.
5
• General reusable solution to a commonly occuring problem within given context• Object-Oriented Desgin Patterns typically show relationships between classes and objects without specifing theirs final application.• (a pattern) „describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same ay twice„- Christopher Alexander
Software Design Pattern
6
• Software Design Principle - Set of Guidelines that helps to avoid bad design and unmaintainable code.• „Design principles are the desirable goals that one aims to achieve. Design patterns are tools one can use to realize those goals. ” – E Arimitage
Pattern vs Principle
Keep it simple stupid. (KISS)KISS
KISS – Example 8
9KISS – Example
Solution
Don’t Repeat yourself (DRY)
DRY
12DRY - Example
Solution
Time for Patterns
Multi Layered Test SolutionStrategy
Overview
Example
Pros & Cons• Low maintenance effort and time• Enhanced code reusability• Structured codebase which is easy to fix and extend• Improved communication• Complexity• Time invested during creation• Experience
FacadeFacade
20Facade – description
Facade provides an unified interface in subsystem. Defines a higher-level interface that makes the subsystem easier to use.
Facade – UML
Facade - Test Case
• Login as Normal User • Add product to cart • Go to checkout• Verify if PayPal is available payment method
Facade – Before
Facade –After
Facade – Front Facade Implementation
Pros & Cons
• Less time spent on writing new test cases • Easier maintenance.• Test is harder to debug if it fails in ‚unexpected’ way.• Test is unreadable – if treated as an Unit Test.
Methods Chaining
Methods Chaining- Example
Chaining – methods difference
Pros & Cons
Helps when writing testsClean Code & ReadabilityPerson can’t see what object is returnedChain breaks
Template Method
32Template Method– description
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Template Method– UML
Template Method -Test Cases
Buy Product Go To CheckoutValidate PayPal PaymentBuy Product Go To CheckoutValidate Payment On Delivery
Template Method - Example
Template Method - Example
Pros & Cons• Avoiding God Class• Modularization• Quite hard to understand• To understand what going on in test ‚little digging’ is neccesary
SummaryDesign patterns are more like guidelinesPatterns can be bad if used in wrong con-textAnybody can use themDo not force patterns into your code!!!Anti-Patterns
Bibliographyhttp://dofactory.com/net/design-patternsHead First Design Patterns – O’Reily MediaDesign Patterns: Elements of Reusable Object-Oriented Software
Questions?
Contact Us
[email protected]@gmail.com
Thank you for your attentionKrzysztof Wilczyński & Maciej Wyrodek