Top Banner
Simplifying test automation with design patterns Ivan Pashko
26

Ivan Pashko - Simplifying test automation with design patterns

Jan 21, 2018

Download

Engineering

Ievgenii Katsan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ivan Pashko - Simplifying test automation with design patterns

Simplifying test automation with design patterns

Ivan Pashko

Page 2: Ivan Pashko - Simplifying test automation with design patterns

Speaker info

Ivan Pashko, UkraineScrum Master in Betsson, Ciklum

• 8+ years in the IT

• Software Automation Developer

/in/ivan-pashko-33208042/

/groups/1180099188730673

Page 3: Ivan Pashko - Simplifying test automation with design patterns

Code smell

Page 4: Ivan Pashko - Simplifying test automation with design patterns

Clean code

Code is clean if it can be understood

easily – by everyone on the team, going

over a long time

Page 5: Ivan Pashko - Simplifying test automation with design patterns
Page 6: Ivan Pashko - Simplifying test automation with design patterns

Why Design Patterns?

General repeatable solution to a commonly occurring

problem in software design.

Page 7: Ivan Pashko - Simplifying test automation with design patterns

Test smells

• Test Code Duplication• Conditional Test Logic• Obscure Test• Fragile Test

Page 8: Ivan Pashko - Simplifying test automation with design patterns

Test smells

• copy/paste• if-else • inheritance /overrides• data sensivity (hardcode, magic values)

Page 9: Ivan Pashko - Simplifying test automation with design patterns

Test smell. Copy/Paste

• Duplication increasesthe cost of maintenance

Page 10: Ivan Pashko - Simplifying test automation with design patterns

Template Method

• Defines the skeleton of an algorithm in an operation,

• Deferring some steps to subclasses

Page 11: Ivan Pashko - Simplifying test automation with design patterns

Template Method

public class MonitoringNotificationBaseTest<T> where T : BaseNotificationData

/// <summary>

/// Method perform next steps:

/// 1. Create New Notification

/// 2. Update Notification

/// 3. Check Enable/Disable Button

/// 4. Remove Notification

/// </summary>

/// <param name="defaultNotification">Used in 1 step</param>

/// <param name="updatedNotification">Used in 2 step</param>

public void CRUDNotificationTest(T defaultNotification, T updatedNotification)

{

CreateNewNotification(defaultNotification);

UpdateNotification(updatedNotification);

CheckEnableDisableButton();

RemoveNotification();

}

Page 12: Ivan Pashko - Simplifying test automation with design patterns

Test smell. Conditional Test Logic

Makes tests more complicated than

they really should be.

Page 13: Ivan Pashko - Simplifying test automation with design patterns

if (element.Exist()){

AssertElementContext(element);}else{

AssertErrorMessageAppeared();}

if (element != null)

{

element.Click();

}

• What condition is passed?

• What if condition failed, but test is green?

• What is test fail?

Test smell. Conditional Test Logic

Page 14: Ivan Pashko - Simplifying test automation with design patterns

Strategy Pattern

Enables an algorithm's behavior to be selected at

runtime

Page 15: Ivan Pashko - Simplifying test automation with design patterns

Factory Pattern

• Hides the logic of initializing an object inside the factory.

• Refers to the object using a common interface instead of the concrete class.

Page 16: Ivan Pashko - Simplifying test automation with design patterns

Test smell. Inheritance /overrides

Blind inheritance and overrides leads to the creation of «Monster» objects

Page 17: Ivan Pashko - Simplifying test automation with design patterns

Composite Pattern

• The composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object.

Page 18: Ivan Pashko - Simplifying test automation with design patterns

Decorator Pattern

• Attach additional responsibilities to an object dynamically.

Page 19: Ivan Pashko - Simplifying test automation with design patterns

• Revealing Naming

• Replace Magic Numbers with constants

• Use builder for defaults & complex object creation

//Revealing namingList<string> list1List<string> userNames

//Magic numbers as constantsFindByAge(33);

const int OLD_USER_AGE = 33;FindByAge(OLD_USER_AGE);

Test smell. Data sensitivity

Page 20: Ivan Pashko - Simplifying test automation with design patterns

Builder Pattern

Separates the construction of a complex object from its representation

Page 21: Ivan Pashko - Simplifying test automation with design patterns

Builder Pattern

public class User{

//Required parameters:public string Email { get; set; }public string Login { get; set; }

//Additional parameters:public string Name { get; set; }public int Age { get; set; }public string Address { get; set; }

}

• Typical test user object:• Required fields• Additional fields

Page 22: Ivan Pashko - Simplifying test automation with design patterns

Builder Pattern

//Telescoping constructornew User(email, login);new User(email, login, "test name", 18, "test address");

//JavaBeansnew User("[email protected]", "test_123"){

Name = "Name",Age = 18,Address = "test address"

};

• Copy / paste

• Constructor overrides

• Helpers methods

As a result:

• A lot of code

• Duplicates

• Unclear defaults

Page 23: Ivan Pashko - Simplifying test automation with design patterns

Builder Patternnew UserBuilder("[email protected]", "user_123")

.Name("Name")

.Age(18)

.Address("test address")

.Build();

public UserBuilder(string email, string login){

_user = new User(email, login){

//Default values:Name = "Default Name",Age = 18,Address = "Default address"

};}

• Easy to read

• Simple to extend

• Safe defaults

Page 24: Ivan Pashko - Simplifying test automation with design patterns

Your test smells if

• contains if’s

• has a ‘twin brother’

• depends on the data

• doesn’t feet in one screen

• bypass your framework

Page 25: Ivan Pashko - Simplifying test automation with design patterns

Good testis like a

good joke

- it needs no explanation

Page 26: Ivan Pashko - Simplifying test automation with design patterns

Thank you

Useful links:

http://blog.bbv.ch/wp-content/uploads/2013/06/Clean-Code-V2.1.pdf

http://xunitpatterns.com

https://sourcemaking.com

Questions?

You can also ask questions for me in the lounge zone