Académique Documents
Professionnel Documents
Culture Documents
TEST-DRIVEN DEVELOPMENT
2009-04-29
Brian Rasmussen
http://www.linkedin.com/in/brianrasmussen
AGENDA
Where to begin
The Turn
the Twist
What is TDD
WHAT IS TDD
WHAT IS TDD
TEST-DRIVEN DEVELOPMENT
Refactor
ORIGIN
Make it Fail
Make it Work
Make it Better
Refactor
TDD CYCLE
Make it Better
Make it Fail
Make it Work
TDD CYCLE
Refactor
Why TDD
WHY / BENEFITS
Confidence in change
Document requirements
80%
8000
7000
60%
6000
5000
% defects created
40%
4000
3000
20%
Thousand $s
2000
1000
0%
% of Defects Introduced
(MICROSOFT RESEARCH)
160%
140%
120%
100%
80%
60%
40%
20%
0%
How to
An out-of-process call
UI
GUI interaction
HOW TO DO TDD
on my component A?
Unit Test A, but what about B, C, D?
HOW TO DO TDD
on my component A?
This, and the previous two slides,
all thoughts
on reference
implementing tests on existing
are
If my
component
Aand
component
with exceptional
behavior (Exception)
Circular dependency
Testing
TDD.
Do not
write the code in your
head
before you write the test
Where to begin
The simplest.
The essence.
Answers:
If you need to write code that is untested, choose a
simpler test.
Answers:
In the beginning, focus on the test you are writing,
and do not think of the other tests.
Unbounded stack
example
Push null onto the Stack and verify that IsEmpty returns false.
Push null onto the Stack, Pop the Stack, and verify that the value
returned is null.
Push null onto the Stack, call Top, and verify that the value returned is
null.
The Turn
Interfaces
Dependency Injection
Test Doubles (Mock Objects)
Mocking Framework.
The builder and fluent interface patterns.
ASP.NET MVC
I wrote mock code like that before. Can you believe my mocking code
even contained bugs? I had to fix those as well.
The "free" products are not called free, they are open-source projects.
They are built in most cases by a single person, and maintained by the
community if they are successful.
There's a lot of effort and love put into that. What's the return? There's the
great satisfaction someone finds your software useful (Hey, that also goes
for commercial products).
Ayende, creator of Rhino Mocks and Daniel, creator of Moq, put a lot of
hours into feature requests and support, but there's a limit to what they
can do.
Take a look at Ayende's slogan on his blog: "Send me a patch for
this".
Don't be a whiner. Take responsibility. Compare the good, bad and ugly.
And pick what is right for you, not because it's what you can afford, but for
what it will save you, give you back or help you outperform your
competitors.
Of course you get what you pay for (Nothing + More nothing = Nothing).
TYPEMOCK
TYPEMOCK.NET HISTORY
Reflective Mocks
Natural Mocks
Typemock Isolator
Arrange Act Assert (AAA) Syntax (Latest API version - you are encouraged
to use this).
the Twist
Typemock Isolator
Demo
Excuses
"We're in a middle of a project right now. We'll wait two months until it
completes, then we'll have time".
If I came to you in two months and asked what has changed since last
time, you'll give me the same answer.
Poor Encapsulation
The builder and fluent interface patterns are very useful here
Bloated SetUp
Classicists
The classical TDD style is to use real objects if possible and a double if it's
The Classicists does White-box Integration
awkward to use the real thing. So a classical TDDer would use a real
testing
warehouse and a double for the
mail service. The kind of double doesn't
really matter that much.
First generation TDD
Mockist
A mockist TDD practitioner, however, will always use a mock for any
The Mockist does White-box Unit testing
object with interesting behavior. In this case for both the warehouse and
the mail service.
Sencond generation TDD
An isolator
TDD practitioner,
willdoes
alwaysBlack-box
isolate or fake
anytesting
object with
The
Isolator
and Fake
Unit
interesting behavior. In this case for both the warehouse and the mail
Third generation TDD
service.
INTRODUCING BDD
By Dan North
BDD is another variation on TDD that tends to use mockist testing
This sentence template The class should do something means you can only
define a test for the current class. This keeps you focused. If you find yourself
writing a test whose name doesnt fit this template, it suggests the behaviour
may belong elsewhere.
LINKS
Books
Blog
http://www.typemock.com/
http://research.microsoft.com/en-us/projects/esm/nagappan_tdd.pdf
Typemock
http://www.codeplex.com/CThru
Other links:
http://en.wikipedia.org/wiki/Test-driven_development
http://www.testdriven.com/
http://www.mockobjects.com/ - Online TDD book: http://www.mockobjects.com/book/index.html
http://www.martinfowler.com/articles/mocksArentStubs.html
http://dannorth.net/introducing-bdd
http://behaviour-driven.org/Introduction
Q&A