#2 Backend Meetup - Introduction to TDD/BDD

Preview:

DESCRIPTION

TDD

Citation preview

INTRODUCTION TOTEST DRIVEN DEVELOPMENT

tw: @MaciejGrajcarek

Check List

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

Who needs tests anyway?

Who needs tests anyway? Bugs killers!

Technical Debt

Technical Debt

Where it all beganWaterfall Model

Winston W. Royce (1970)

Where it all beganAgile – best friend of TDD

- Adaptive vs Predictive- Iterative vs Waterfall

2001 – Agile Manifesto

Where it all beganScrum Cicle

Analysis

Programming

Testing

Acceptance

Where it all began

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

Check List

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

TDD Cycle of life

Failing Test

Pass testRefactoring

TDD Cycle of life

Failing Test

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

First simple failing test

function checkSumTest()

{

$a = 5;

$b = 4;

$calculator = new Calculator();

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

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

}

TDD Cycle of life

Pass test

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

Fake It (Till You Make It)

class Calculator

{

function sum($a, $b)

{

return 9;

}

}

Type The Obvious Implementation

class Calculator

{

function sum($a, $b)

{

$sum = $a + $b;

return $sum;

}

}

Triangulation

At least two points needed to find a location.

Triangulation

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

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

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

}

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;}

}

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

Triangulation

class Calculator{function sum()

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

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

}}

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

Triangulation

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

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

TDD Cycle of life

Refactoring•KISS•DRY

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

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

TDD Cycle of life

Failing Test

Pass testRefactoring

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

Tests measurement

1. Code Coverage2. Underlay Defects

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

coverage, but yours not necessarily

TDD Tips

1. Create a check list of tests

TDD Tips

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

refactorization (or larger when fealing comfortable)

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

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

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!)

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

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

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

London vs Brooklyn School of TDD

Is it all about Mocks?

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

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

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

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

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

PHPSpec

Let’s do some live coding!