Test Driven Development (TDD) Basics

20
Test Driven Development

Transcript of Test Driven Development (TDD) Basics

Page 1: Test Driven Development (TDD) Basics

Test Driven Development

Page 2: Test Driven Development (TDD) Basics

Test Driven Developme

nt

Technique to design and develop software.

Practice part of the Extreme Programming methodology — XP

Based on three fundamental pillars.

Page 3: Test Driven Development (TDD) Basics

The Three Pillars

Implement the exact functionality of the software.

Reduce the number of defects on the software – good quality software

Produce modular and highly reusable software.

Page 4: Test Driven Development (TDD) Basics

When Doing TDD

It’s not about just writing tests.

Translate use cases to examples. Enough to describe the functionality without ambiguity.

Architecture will eventually emerge.

Page 5: Test Driven Development (TDD) Basics

The TDD Algorithm

Red

GreenRefactor

Page 6: Test Driven Development (TDD) Basics

The TDD Algorithm

Red

GreenRefactor

Page 7: Test Driven Development (TDD) Basics

TDD In ActionWith an example ;-)

Page 8: Test Driven Development (TDD) Basics

The Problem

Email validation mechanism

Page 9: Test Driven Development (TDD) Basics

The Problem

Email validation mechanism

?

Page 10: Test Driven Development (TDD) Basics

The Problem

Email validation mechanism

Neither empty nor null.

Has to contain an @ symbol

Has to belong to the .com, .net and .edu TDLs

Domain part can’t be 1 character long

All lowercase

Not contain numbers

Only _ . - symbols

Page 11: Test Driven Development (TDD) Basics

Before we begin

Writing Tests

Testing framework Part of the language

Ruby Rust Go Python

External tools and libraries

Page 12: Test Driven Development (TDD) Basics

Neither empty nor null

Red@Testpublic void shouldReturnFalseIfEmailIsNull() { String email = null; EmailValidator validator = new EmailValidator();

boolean isValid = validator.isValid(email);

assertThat(isValid, equalTo(false));}

Page 13: Test Driven Development (TDD) Basics

Neither empty nor null

Green public boolean isValid(String email) { return false;}

Page 14: Test Driven Development (TDD) Basics

Neither empty nor null

Red

@Testpublic void shouldReturnTrueIfEmailLengthIsGreaterThanZero() { String email = "[email protected]"; EmailValidator validator = new EmailValidator();

boolean isValid = validator.isValid(email);

assertThat(isValid, equalTo(true));}

Page 15: Test Driven Development (TDD) Basics

Neither empty nor null

Greenpublic boolean isValid(String email) { if (email == null) { return false; } return email.length() > 0;}

Page 16: Test Driven Development (TDD) Basics

Contains an @ symbol

Red

@Testpublic void shouldReturnFalseIfEmailDoesntContainAtSymbol () { String email = "someemail.com"; EmailValidator validator = new EmailValidator();

boolean isValid = validator.isValid(email);

assertThat(isValid, equalTo(false));}

Page 17: Test Driven Development (TDD) Basics

Neither empty nor null

Greenpublic boolean isValid(String email) { if (email == null) { return false; } if (!email.contains("@")) { return false; } return email.length() > 0;}

Page 18: Test Driven Development (TDD) Basics

Refactorprivate EmailValidator validator;

@Beforepublic void setUp () { validator = new EmailValidator();}

Page 19: Test Driven Development (TDD) Basics

In the end… Components will behave exactly as we want.

Tests will be the documentation of our system.

Tests will break if changes compromise the system.

Page 20: Test Driven Development (TDD) Basics

Thank you!Carlos Andrés Oquendo

[email protected]@andres19_05