45
INTRODUCTION TO TEST DRIVEN DEVELOPMENT tw: @MaciejGrajcarek

#2 Backend Meetup - Introduction to TDD/BDD

Embed Size (px)

DESCRIPTION

TDD

Citation preview

Page 1: #2 Backend Meetup - Introduction to TDD/BDD

INTRODUCTION TOTEST DRIVEN DEVELOPMENT

tw: @MaciejGrajcarek

Page 2: #2 Backend Meetup - Introduction to TDD/BDD

Check List

1. History of TDD2. TDD Cycles3. BDD4. TDD is dead?5. PHPSpec

Page 3: #2 Backend Meetup - Introduction to TDD/BDD

Who needs tests anyway?

Page 4: #2 Backend Meetup - Introduction to TDD/BDD

Who needs tests anyway? Bugs killers!

Page 5: #2 Backend Meetup - Introduction to TDD/BDD

Technical Debt

Page 6: #2 Backend Meetup - Introduction to TDD/BDD

Technical Debt

Page 7: #2 Backend Meetup - Introduction to TDD/BDD

Where it all beganWaterfall Model

Winston W. Royce (1970)

Page 8: #2 Backend Meetup - Introduction to TDD/BDD

Where it all beganAgile – best friend of TDD

- Adaptive vs Predictive- Iterative vs Waterfall

2001 – Agile Manifesto

Page 9: #2 Backend Meetup - Introduction to TDD/BDD

Where it all beganScrum Cicle

Analysis

Programming

Testing

Acceptance

Page 10: #2 Backend Meetup - Introduction to TDD/BDD

Where it all began

„Test Driven Development: By Example” – Kent Beck (2003)

Page 11: #2 Backend Meetup - Introduction to TDD/BDD

Check List

1. History of TDD2. TDD Cycle of life3. BDD4. TDD is dead?5. PHPSpec

Page 12: #2 Backend Meetup - Introduction to TDD/BDD

TDD Cycle of life

Failing Test

Pass testRefactoring

Page 13: #2 Backend Meetup - Introduction to TDD/BDD

TDD Cycle of life

Failing Test

•Make test compile•Make sure it doesn’t pass

Page 14: #2 Backend Meetup - Introduction to TDD/BDD

First simple failing test

function checkSumTest()

{

$a = 5;

$b = 4;

$calculator = new Calculator();

$sum = $calculator->sum($a, $b);

$this->assertEqual(9, $sum);

}

Page 15: #2 Backend Meetup - Introduction to TDD/BDD

TDD Cycle of life

Pass test

•Fake It (Till You Make It)•Type The Obvious Implementation•Triangulation

Page 16: #2 Backend Meetup - Introduction to TDD/BDD

Fake It (Till You Make It)

class Calculator

{

function sum($a, $b)

{

return 9;

}

}

Page 17: #2 Backend Meetup - Introduction to TDD/BDD

Type The Obvious Implementation

class Calculator

{

function sum($a, $b)

{

$sum = $a + $b;

return $sum;

}

}

Page 18: #2 Backend Meetup - Introduction to TDD/BDD

Triangulation

At least two points needed to find a location.

Page 19: #2 Backend Meetup - Introduction to TDD/BDD

Triangulation

//Test 1$sum = $calculator->sum($a, $b);$this->assertEqual(9, $sum);

class Calculator{function sum($a, $b)

{ $sum = $a + $b; return $sum;}

}

Page 20: #2 Backend Meetup - Introduction to TDD/BDD

Triangulation

//Test 1$sum = $calculator->sum(4, 5);$this->assertEqual(9, $sum);

//Test 2$sum = $calculator->sum(4, 5, 7);$this->assertEqual(16, $sum);

class Calculator{

function sum($a, $b, $c = 0) {

$sum = $a + $b + $c; return $sum;}

}

Page 21: #2 Backend Meetup - Introduction to TDD/BDD

Triangulation

//Test 1$sum = $calculator->sum(4, 5);$this->assertEqual(9, $sum);

//Test 2$sum = $calculator->sum(4, 5, 7);$this->assertEqual(16, $sum);

//Test 3$sum = $calculator->sum(4, 5, 7, 1);$this->assertEqual(17, $sum);

Page 22: #2 Backend Meetup - Introduction to TDD/BDD

Triangulation

class Calculator{function sum()

{$elements = func_get_args();$sum = 0;foreach ($elements as $el) {

$sum += $el;}return $sum;

}}

Page 23: #2 Backend Meetup - Introduction to TDD/BDD

Triangulation

//Test 1$sum = $calculator->sum(4, 5);$this->assertEqual(9, $sum);

//Test 2$sum = $calculator->sum(4, 5, 7);$this->assertEqual(16, $sum);

//Test 3$sum = $calculator->sum(4, 5, 7, 1);$this->assertEqual(17, $sum);

Page 24: #2 Backend Meetup - Introduction to TDD/BDD

Triangulation

//Final test$sum = $calculator->sum(4, 5, 7, 1);

$this->assertEqual(17, $sum);

Page 25: #2 Backend Meetup - Introduction to TDD/BDD

TDD Cycle of life

Refactoring•KISS•DRY

Page 26: #2 Backend Meetup - Introduction to TDD/BDD

Maintainability, Flexibility, Portability, Reusability, Readability, Scalability, Testability, Understandability

Page 27: #2 Backend Meetup - Introduction to TDD/BDD

Don’t Repeat Yourself

1) Extract Shared Concept

2) Give it a name

3) Reuse it

If impossible to do 1-2, there is no repetition

Page 28: #2 Backend Meetup - Introduction to TDD/BDD

TDD Cycle of life

Failing Test

Pass testRefactoring

Page 29: #2 Backend Meetup - Introduction to TDD/BDD

Check List

1. History of TDD2. TDD Cycle of life

1. Red phase2. Green phase3. Refactoring

3. Tests measurement4. TDD Tips5. BDD6. TDD is dead?7. PHPSpec

Page 30: #2 Backend Meetup - Introduction to TDD/BDD

Tests measurement

1. Code Coverage2. Underlay Defects

- 100 % code coverage != Error Free- Vendor code should have 100% code

coverage, but yours not necessarily

Page 31: #2 Backend Meetup - Introduction to TDD/BDD

TDD Tips

1. Create a check list of tests

Page 32: #2 Backend Meetup - Introduction to TDD/BDD

TDD Tips

1. Create a check list of tests2. Small step approach during

refactorization (or larger when fealing comfortable)

Page 33: #2 Backend Meetup - Introduction to TDD/BDD

TDD Tips

1. Create a check list of tests2. Small step approach during

refactorization (or larger when fealing comfortable)

3. Add problems to the list, instead of resolving them immediately

Page 34: #2 Backend Meetup - Introduction to TDD/BDD

TDD Tips

1. Create a check list of tests2. Small step approach during

refactorization (or larger when fealing comfortable)

3. Add problems to the list, instead of resolving them immediately

4. Never create new test if previous one is failing

Page 35: #2 Backend Meetup - Introduction to TDD/BDD

TDD Tips

1. Create a check list of tests2. Small step approach during

refactorization (or larger when fealing comfortable)

3. Add problems to the list, instead of resolving them immediately

4. Never create new test if previous one is failing

5. Remove repetition (between code and test too!)

Page 36: #2 Backend Meetup - Introduction to TDD/BDD

TDD Tips

1. Create a check list of tests2. Small step approach during refactorization

(or larger when fealing comfortable)3. Add problems to the list, instead of

resolving them immediately4. Never create new test if previous one is

failing5. Remove repetition (between code and test

too!)6. Tests should be fast and isolated

Page 37: #2 Backend Meetup - Introduction to TDD/BDD

TDD Tips

1. Create a check list of tests2. Small step approach during refactorization (or

larger when fealing comfortable)3. Add problems to the list, instead of resolving

them immediately4. Never create new test if previous one is failing5. Remove repetition (between code and test

too!)6. Tests should be fast and isolated7. Test only public

Page 38: #2 Backend Meetup - Introduction to TDD/BDD

TDD Tips

1. Create a check list of tests2. Small step approach during refactorization (or

larger when fealing comfortable)3. Add problems to the list, instead of resolving

them immediately4. Never create new test if previous one is failing5. Remove repetition (between code and test too!)6. Tests should be fast and isolated7. Test only public8. Write your own tests, don’t wait for a

teammate

Page 39: #2 Backend Meetup - Introduction to TDD/BDD

London vs Brooklyn School of TDD

Is it all about Mocks?

Page 40: #2 Backend Meetup - Introduction to TDD/BDD

Check List

1. History of TDD2. TDD Cycle of life

1. Red phase2. Green phase3. Refactoring

3. Tests measurement4. TDD Tips5. London vs Brooklyn6. BDD7. TDD is dead?8. PHPSpec

Page 41: #2 Backend Meetup - Introduction to TDD/BDD

Behavior Driven Development- Based on TDD- Test behavior, not code!- Domain Driven Desing- DSL in test tools- „it_should”- Gherkin [Given, When, Then]- Tests as specification

Page 42: #2 Backend Meetup - Introduction to TDD/BDD

Check List

1. History of TDD2. TDD Cycle of life

1. Red phase2. Green phase3. Refactoring

3. Tests measurement4. TDD Tips5. London vs Brooklyn6. BDD7. TDD is dead?8. PHPSpec

Page 43: #2 Backend Meetup - Introduction to TDD/BDD

TDD is Dead?

Who said that ?! David Heinemeier Hansson (RoR author and Bacecamp founder)

http://martinfowler.com/articles/is-tdd-dead/

http://www.infoq.com/news/2014/06/tdd-dead-controversy

Page 44: #2 Backend Meetup - Introduction to TDD/BDD

Check List

1. History of TDD2. TDD Cycle of life

1. Red phase2. Green phase3. Refactoring

3. Tests measurement4. TDD Tips5. London vs Brooklyn6. BDD7. TDD is dead?8. PHPSpec

Page 45: #2 Backend Meetup - Introduction to TDD/BDD

PHPSpec

Let’s do some live coding!