Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject:...

17
Test Driven Development A Tutorial Erik Dörnenburg ThoughtWorks 2 © Copyright ThoughtWorks, Inc.® 2004-2006 Agenda Iteration 0 Tools for test-driven development Iteration 1 Test-driven development Iteration 2 Interaction-based testing Iteration 3 Lightweight containers

Transcript of Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject:...

Page 1: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

Test Driven Development

A Tutorial

Erik DörnenburgThoughtWorks

2© Copyright ThoughtWorks, Inc.® 2004-2006

Agenda

• Iteration 0 Tools for test-driven development

• Iteration 1

Test-driven development

• Iteration 2

Interaction-based testing

• Iteration 3

Lightweight containers

Page 2: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

3© Copyright ThoughtWorks, Inc.® 2004-2006

The four XP values

Simplicity

Communication

Feedback

Courage

Iteration 0Tools for test-driven development

Page 3: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

5© Copyright ThoughtWorks, Inc.® 2004-2006

Core tools for TDD

an IDE a unit testing frameworka build automation tool a continuous integration system

Iteration 1 Test-driven development

Page 4: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

7© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 1

As a wallet owner I would

like to be able to put money

into my wallet so that I can

take it out later.

#1

8© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 1

As a wallet owner I would

like to be able to see how

much money is in my wallet.

#2

Page 5: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

9© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 1

As a wallet owner I would

like to be able to take a

specified amount of money

out of the wallet.

#3

10© Copyright ThoughtWorks, Inc.® 2004-2006

[Switch to development environment]

Page 6: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

11© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 1

As a wallet owner I would

like to be able to take out

more money than I have, so

that I can buy things I

can't afford.

account#3b

12© Copyright ThoughtWorks, Inc.® 2004-2006

[Switch to development environment]

Page 7: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

Iteration 2Interaction-based testing

14© Copyright ThoughtWorks, Inc.® 2004-2006

Collaborating Objects

• An object-oriented program is organised as a web of collaborating objects

Page 8: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

15© Copyright ThoughtWorks, Inc.® 2004-2006

Using Mocks

• To isolate the tested object we replace its neighbours with mock objects

Object under test

Test case

Mock

Mock

16© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 2

As an auditor I would like

to be notified whenever an

account is overdrawn.

#4

Page 9: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

17© Copyright ThoughtWorks, Inc.® 2004-2006

As an auditor I would like

to be notified whenever an

account is overdrawn.

#4

Iteration 2

Task 1:

Introduce AccountService

#4.1

18© Copyright ThoughtWorks, Inc.® 2004-2006

As an auditor I would like

to be notified whenever an

account is overdrawn.

#4

Iteration 2

Task 2:

Add audit service and logic

#4.2

Page 10: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

19© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 2

As an account owner I

would like be notified by

email when I overdraw my

account.

#5

20© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 2

As an auditor I would like

to be notified whenever

more than £10,000 are

withdrawn from an account

in one transaction.

#6

Page 11: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

21© Copyright ThoughtWorks, Inc.® 2004-2006

[Switch to development environment]

Iteration 3Lightweight containers

Page 12: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

23© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 3

Where is my application?

24© Copyright ThoughtWorks, Inc.® 2004-2006

PicoContainer

pico containerclasses objects

Page 13: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

25© Copyright ThoughtWorks, Inc.® 2004-2006

[Switch to development environment]

26© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 3

What’s this ‘Inversion of Control’thing people are talking about?

Page 14: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

27© Copyright ThoughtWorks, Inc.® 2004-2006

Dependency resolution

• Service Locator

• Dependency Injection

Service Locator

Dependency Injector

28© Copyright ThoughtWorks, Inc.® 2004-2006

[Switch to development environment]

Page 15: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

29© Copyright ThoughtWorks, Inc.® 2004-2006

Iteration 3

But we’re using the Spring framework!

Odds and endsObject Mother

Page 16: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

31© Copyright ThoughtWorks, Inc.® 2004-2006

Odds and ends

My tests are ugly!

32© Copyright ThoughtWorks, Inc.® 2004-2006

[Switch to development environment]

Page 17: Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject: Test-Driven Development Keywords: TDD mock picocontainer spring Created Date: 9/13/2006

The end