Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject:...
Transcript of Test Driven Development A Tutorial · Title: TDD Tutorial.ppt Author: Erik Doernenburg Subject:...
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
3© Copyright ThoughtWorks, Inc.® 2004-2006
The four XP values
Simplicity
Communication
Feedback
Courage
Iteration 0Tools for test-driven development
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
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
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]
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]
Iteration 2Interaction-based testing
14© Copyright ThoughtWorks, Inc.® 2004-2006
Collaborating Objects
• An object-oriented program is organised as a web of collaborating objects
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
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
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
21© Copyright ThoughtWorks, Inc.® 2004-2006
[Switch to development environment]
Iteration 3Lightweight containers
23© Copyright ThoughtWorks, Inc.® 2004-2006
Iteration 3
Where is my application?
24© Copyright ThoughtWorks, Inc.® 2004-2006
PicoContainer
pico containerclasses objects
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?
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]
29© Copyright ThoughtWorks, Inc.® 2004-2006
Iteration 3
But we’re using the Spring framework!
Odds and endsObject Mother
31© Copyright ThoughtWorks, Inc.® 2004-2006
Odds and ends
My tests are ugly!
32© Copyright ThoughtWorks, Inc.® 2004-2006
[Switch to development environment]
The end