Software Design Patterns in Test Automation
Table of Content Objectives What are Design Patterns? Design Pattern and Design Principle - what's the difference? Principles KISS Don’t Repeat Yourself. 5. Patterns Multi-layered test solution Facade Chaining Template Method
Objectives Present the concepts of „design pattern” & „design 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.
Software Design Pattern 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
Pattern vs Principle 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
KISS Keep it simple stupid. (KISS)
KISS – Example
KISS – Example
Solution
DRY Don’t Repeat yourself (DRY)
DRY - Example
Solution
Time for Patterns
Multi Layered Test Solution Strategy Multi Layered Test Solution
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
Facade Facade
Facade provides an unified interface in subsystem. Facade – description Facade provides an unified interface in subsystem. Defines a higher-level interface that makes the subsystem easier to use.
Facade – UML
Verify if PayPal is available payment method 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
Less time spent on writing new test cases Easier maintenance. 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 tests Clean Code & Readability Person can’t see what object is returned Chain breaks
Template Method
Template 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 Checkout Validate PayPal Payment Buy Product Go To Checkout Validate Payment On Delivery
Template Method - Example
Template Method - Example
Quite hard to understand To understand what going on in test Pros & Cons Avoiding God Class Modularization Quite hard to understand To understand what going on in test ‚little digging’ is neccesary
Design patterns are more like guidelines Summary Design patterns are more like guidelines Patterns can be bad if used in wrong context Anybody can use them Do not force patterns into your code!!! Anti-Patterns
Head First Design Patterns – O’Reily Media Bibliography http://dofactory.com/net/design-patterns Head First Design Patterns – O’Reily Media Design Patterns: Elements of Reusable Object-Oriented Software
Questions?
Contact Us wyrodek.maciej@gmail.com wilczynskikrzysztofq@gmail.com
Thank you for your attention Krzysztof Wilczyński & Maciej Wyrodek