Ivan Pashko - Simplifying test automation with design patterns

Post on 21-Jan-2018

57 views 1 download

Transcript of Ivan Pashko - Simplifying test automation with design patterns

Simplifying test automation with design patterns

Ivan Pashko

Speaker info

Ivan Pashko, UkraineScrum Master in Betsson, Ciklum

• 8+ years in the IT

• Software Automation Developer

/in/ivan-pashko-33208042/

/groups/1180099188730673

Code smell

Clean code

Code is clean if it can be understood

easily – by everyone on the team, going

over a long time

Why Design Patterns?

General repeatable solution to a commonly occurring

problem in software design.

Test smells

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

Test smells

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

Test smell. Copy/Paste

• Duplication increasesthe cost of maintenance

Template Method

• Defines the skeleton of an algorithm in an operation,

• Deferring some steps to subclasses

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();

}

Test smell. Conditional Test Logic

Makes tests more complicated than

they really should be.

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

Strategy Pattern

Enables an algorithm's behavior to be selected at

runtime

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.

Test smell. Inheritance /overrides

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

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.

Decorator Pattern

• Attach additional responsibilities to an object dynamically.

• 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

Builder Pattern

Separates the construction of a complex object from its representation

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

Builder Pattern

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

//JavaBeansnew User("test@gmail.com", "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

Builder Patternnew UserBuilder("test@gmail.com", "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

Your test smells if

• contains if’s

• has a ‘twin brother’

• depends on the data

• doesn’t feet in one screen

• bypass your framework

Good testis like a

good joke

- it needs no explanation

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