93
Making Friends with TDD John Cleary Lead Web Developer, DocNet @TheRealBifter

Making friends with TDD

Embed Size (px)

Citation preview

Page 1: Making friends with TDD

Making Friends with TDD

John ClearyLead Web Developer, DocNet

@TheRealBifter

Page 2: Making friends with TDD

So, What is TDD?

Page 3: Making friends with TDD

10 Write one test20 Run tests (they fail)30 Write code to make test pass40 Run tests again (they pass)50 Refactor (code AND tests)60 Run test (they still pass)70 Goto 10

Page 4: Making friends with TDD

Red

GreenRefactor

Page 5: Making friends with TDD

Red

GreenRefactor

Write����������� ������������������  a����������� ������������������  failing����������� ������������������  test

Page 6: Making friends with TDD

Red

GreenRefactor

Write����������� ������������������  a����������� ������������������  failing����������� ������������������  test

Fix����������� ������������������  the����������� ������������������  test

Page 7: Making friends with TDD

Red

GreenRefactor

Write����������� ������������������  a����������� ������������������  failing����������� ������������������  test

Fix����������� ������������������  the����������� ������������������  test

Make����������� ������������������  it����������� ������������������  pretty

Page 8: Making friends with TDD

Why do it?

Page 9: Making friends with TDD

1. Improved Code Design

Page 10: Making friends with TDD

Classes that are easy to test are by definition decoupled.

1. Improved Code Design

Page 11: Making friends with TDD

Classes that are easy to test are by definition decoupled.

Encourages refactoring

1. Improved Code Design

Page 12: Making friends with TDD

2. Verification

Page 13: Making friends with TDD

Fewer bugs

2. Verification

Page 14: Making friends with TDD

Fewer bugs

Easier to maintain

2. Verification

Page 15: Making friends with TDD

Awesome - let’s do it, right?

Page 16: Making friends with TDD

Awesome - let’s do it, right?

Wait!

Page 17: Making friends with TDD

Does it take longer?

Page 18: Making friends with TDD

Does it take longer?

Maybe, maybe not.

Page 19: Making friends with TDD

Is it harder?

Page 20: Making friends with TDD

Is it harder?

For sure!

Page 21: Making friends with TDD

But it wrecks my creativity, dude?

Page 22: Making friends with TDD

But it wrecks my creativity, dude?

Really? Do some prototypes man!

Page 23: Making friends with TDD

Could we get these benefits without TDD?

Page 24: Making friends with TDD

Could we get these benefits without TDD?

No, but yeah, but no...

Page 25: Making friends with TDD

Awesome, let's do it!(for real this time)

Page 26: Making friends with TDD

Recently Used List Class

Page 27: Making friends with TDD

Recently Used List ClassDevelop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.

Page 28: Making friends with TDD

Recently Used List Class

A recently-used-list is initially empty.

Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.

Page 29: Making friends with TDD

Recently Used List Class

A recently-used-list is initially empty.The most recently added item is first, the least recently added item is last.

Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.

Page 30: Making friends with TDD

Recently Used List Class

A recently-used-list is initially empty.The most recently added item is first, the least recently added item is last.Items can be looked up by index, which counts from zero.

Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.

Page 31: Making friends with TDD

Recently Used List Class

A recently-used-list is initially empty.The most recently added item is first, the least recently added item is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are moved rather than added.

Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.

Page 32: Making friends with TDD

Recently Used List Class

A recently-used-list is initially empty.The most recently added item is first, the least recently added item is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are moved rather than added.

Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.

Page 33: Making friends with TDD
Page 34: Making friends with TDD
Page 35: Making friends with TDD
Page 36: Making friends with TDD
Page 37: Making friends with TDD
Page 38: Making friends with TDD
Page 39: Making friends with TDD
Page 40: Making friends with TDD
Page 41: Making friends with TDD
Page 42: Making friends with TDD
Page 43: Making friends with TDD

Recently Used List Class

A recently-used-list is initially empty.The most recently added item is first, the least recently added item is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are moved rather than added.

Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.

Page 44: Making friends with TDD
Page 45: Making friends with TDD
Page 46: Making friends with TDD
Page 47: Making friends with TDD
Page 48: Making friends with TDD
Page 49: Making friends with TDD
Page 50: Making friends with TDD
Page 51: Making friends with TDD
Page 52: Making friends with TDD

Recently Used List Class

A recently-used-list is initially empty.The most recently added item is first, the least recently added item is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are moved rather than added.

Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.

Page 53: Making friends with TDD
Page 54: Making friends with TDD
Page 55: Making friends with TDD

Recently Used List Class

A recently-used-list is initially empty.The most recently added item is first, the least recently added item is last.Items can be looked up by index, which counts from zero.Items in the list are unique, so duplicate insertions are moved rather than added.

Develop a recently-used-list class to hold strings uniquely in Last-In-First-Out order.

Page 56: Making friends with TDD
Page 57: Making friends with TDD
Page 58: Making friends with TDD
Page 59: Making friends with TDD
Page 60: Making friends with TDD
Page 61: Making friends with TDD

Refactor����������� ������������������  to����������� ������������������  this

Page 62: Making friends with TDD

That wasn’t so hard...

Page 63: Making friends with TDD

That wasn’t so hard...

...but what about testing the persistence layer?

Page 64: Making friends with TDD

You’ve got options

Page 65: Making friends with TDD

Mock the database layer

DBUnit

Codeception

You’ve got options

Page 66: Making friends with TDD

Mock the database layer

DBUnit

Codeception

You’ve got options

The����������� ������������������  purist����������� ������������������  way

Page 67: Making friends with TDD

Mock the database layer

DBUnit

Codeception

You’ve got options

The����������� ������������������  purist����������� ������������������  way

The����������� ������������������  easy����������� ������������������  way

Page 68: Making friends with TDD

Mock the database layer

DBUnit

Codeception

You’ve got options

The����������� ������������������  purist����������� ������������������  way

The����������� ������������������  easy����������� ������������������  way

The����������� ������������������  even����������� ������������������  easier����������� ������������������  way

Page 69: Making friends with TDD

Mock Examplefunction testInsertDatabase(){ $insertArray = array( 'name' => 'Miles', );

// create a mock database $mock = $this->getMockBuilder('Zend_Db_Adapter_Pdo_Mysql', array('insert')) ->disableOriginalConstructor() ->getMock();

// mock out the insert method $mock ->expects($this->once()) ->method('insert') ->with($insertArray);

// pass the database to the container $container = new DIContainer(): $container->set('database', $mock);

// run the test $user = new User(); $user->setName('Miles'); $user->save();}

Not great because• Requires Dependency Injection• Can be fragile• Complicated

But good because• Super fast• Not tied to database structure

Page 70: Making friends with TDD

DBUnit Examplepublic function testCanCreateNewUser(){ $user = new User(); $user->setName('Miles'); $user->save();

$queryTable = $this->getConnection()->createTableQuery( 'users', 'SELECT * from users' );

$expectedTable = $this->createFlatXmlDataSet("users-expected.xml") ->getTable('users');

$this->assertTableEquals($expectedTable, $queryTable);}

Not great because• Slow• Can be fragile• Tied to database structure• Requires xml setup of expected results

But good because• Tests are easy to write and read• It’s obvious whats being tested

Page 71: Making friends with TDD

Codeception Example<?phpfunction testSavingUser(){ $user = new User(); $user->setName('Miles'); $user->save(); $this->codeGuy->seeInDatabase('users',array('name' => 'Miles'));}?> Not great because

• Slow• Tied to database structure

But good because• Less fragile than DBUnit (no xml to maintain)• Tests are easy to write and read• It’s obvious whats being tested

Page 72: Making friends with TDD

When testing the DB...

Page 73: Making friends with TDD

When testing the DB...

Give each developer their own test database (preferably on their own VM)

Page 74: Making friends with TDD

When testing the DB...

Give each developer their own test database (preferably on their own VM)

DB tests are slow - move them outside the main suite or use memory tables

Page 75: Making friends with TDD

When testing the DB...

Give each developer their own test database (preferably on their own VM)

DB tests are slow - move them outside the main suite or use memory tables

Use a tool like “Migrations” to keep databases in sync

Page 76: Making friends with TDD

UI Testing

Page 77: Making friends with TDD

UI Testing

PHPUnit with Selenium

Page 78: Making friends with TDD

UI Testing

PHPUnit with Selenium

Codeception

Page 79: Making friends with TDD

So, where do we go from here?

Page 80: Making friends with TDD

1. Have a go

www.cyber-dojo.com

Page 81: Making friends with TDD

2. Build up slowly

Page 82: Making friends with TDD

2. Build up slowly

Start with one afternoon per week

Page 83: Making friends with TDD

2. Build up slowly

Start with one afternoon per week

Try pair programming

Page 84: Making friends with TDD

3. Motivate your team

Page 85: Making friends with TDD

3. Motivate your team

Start a Coding Club / Kata

Page 86: Making friends with TDD

3. Motivate your team

Start a Coding Club / Kata

[Get your boss to] buy some books

Page 87: Making friends with TDD

3. Motivate your team

Start a Coding Club / Kata

[Get your boss to] buy some books

See����������� ������������������  me����������� ������������������  after����������� ������������������  this����������� ������������������  talk

Page 88: Making friends with TDD

4. Get a CI server

Jenkins

Travis

CruiseControl

Page 89: Making friends with TDD

5. Share

Page 90: Making friends with TDD

5. Share

Attend public Katas

Page 91: Making friends with TDD

5. Share

Attend public Katas

Forums & IRC

Page 92: Making friends with TDD

Questions ?

Page 93: Making friends with TDD

Thank YouJohn Cleary

Lead Web Developer, DocNet@TheRealBifter