Discussion:
Practice: Test-First Programming
Kent Beck
2005-01-19 08:28:44 UTC
Permalink
Write a failing automated test before changing any code. Test-first
programming addresses many problems at once:
* Scope creep-It's easy to get carried away programming and put in code
"just in case." By stating explicitly and objectively what the program is
supposed to do, you give yourself a focus for your coding. If you really
want to put that other code in, write another test after you've made this
one work.
* Coupling and cohesion-If it's hard to write a test, it's a signal that
you have a design problem, not a testing problem. Loosely coupled, highly
cohesive code is easy to test.
* Trust-It's hard to trust the author of code that doesn't work. By
writing clean code that works and demonstrating your intentions with
automated tests, you give your teammates a reason to trust you.
* Rhythm-It's easy to get lost for hours when you are coding. When
programming test-first, it's clearer what to do next: either write another
test or make the broken test work. Soon this develops into a natural and
efficient rhythm-test, code, refactor, test, code, refactor.

The XP community hasn't done much exploration of alternatives to tests for
verifying the behavior of the system. Tools like static analysis and model
checking could be used test-first style. You start with a "test" that says,
for example, that there are no deadlocks in the system. After every change,
you verify again that there are no deadlocks. The static analysis tools I've
seen aren't intended to be used this way. They run too slowly to be part of
the minute-by-minute cycle of programming. However, this seems to be merely
a matter of focus, not a fundamental limitation.

Another refinement of test-first programming is continuous testing, first
reported by David Saff and Michael Ernst in "An Experimental Evaluation of
Continuous Testing During Development," and also explored in Erich Gamma's
and my book Contributing to Eclipse. In continuous testing the tests are run
on every program change, much as an incremental compiler is run on every
change to the source code. Test failures are reported in the same format as
compiler errors. Continuous testing reduces the time to fix errors by
reducing the time to discover them. The tests have to run quickly, however.

The tests you write while coding test-first have the limitation that they
take a microview of the program: do these two objects work well together? As
your experience grows, you'll be able to squeeze more and more reassurance
into these tests. Because of their limited scope, these tests tend to run
very fast. You can run thousands of them as part of the Ten-Minute Build.
Tim King
2005-01-19 16:44:47 UTC
Permalink
I'm a big fan of test-first, even though it often does require
discipline. How many times I've felt like whining, "Oh, I don't feel
like writing a test just for that little change!" But there are so many
advantages. Tests generate more value than they cost.

* Tests reduce debugging effort. Even though writing tests requires you
to write twice as much code, test-first cuts debugging at least in half.
This is especially true over the long term, when you have to maintain
that code. Legacy code without unit tests is often known for its
fragility. Make one change; generate three bugs.

* Related to this, tests make it possible to refactor fearlessly, which
is key to designing in flexibility.

* Unit tests show you how to use the code. They can actually serve as a
form of intra-team documentation. But even on a one-person project, if I
forget how I'm supposed to call f() and g() methods to accomplish
operation X, I can just look at the appropriate unit test for a refresher.

* Unit tests allow you to see your code actually running and working
quickly. This is a strong psychological component. In fact, once you do
a certain amount of test-driven development, you become addicted to
seeing your tests pass. It's a rush to tweak the code and see it run and
work the way it was designed, even if only evidenced by a simple green
bar on the screen. It's an experience only a programmer could understand.

-TimK
--
J. Timothy King team-oriented object-oriented Agile
www.jtse.com C++ Perl assembly embedded systems
Steve Hayes
2005-01-19 22:03:35 UTC
Permalink
Every time I revisit code that I didn't write test first (bad, bad
developer), I find that making it testable also improves the design.
That would be sufficient justification for me (though I subscribe to all
the other points you highlight as well, and see that some of them are
related).

Steve Hayes
Post by Tim King
I'm a big fan of test-first, even though it often does require
discipline. How many times I've felt like whining, "Oh, I don't feel
like writing a test just for that little change!" But there are so many
advantages. Tests generate more value than they cost.
* Tests reduce debugging effort. Even though writing tests requires you
to write twice as much code, test-first cuts debugging at least in half.
This is especially true over the long term, when you have to maintain
that code. Legacy code without unit tests is often known for its
fragility. Make one change; generate three bugs.
* Related to this, tests make it possible to refactor fearlessly, which
is key to designing in flexibility.
* Unit tests show you how to use the code. They can actually serve as a
form of intra-team documentation. But even on a one-person project, if I
forget how I'm supposed to call f() and g() methods to accomplish
operation X, I can just look at the appropriate unit test for a refresher.
* Unit tests allow you to see your code actually running and working
quickly. This is a strong psychological component. In fact, once you do
a certain amount of test-driven development, you become addicted to
seeing your tests pass. It's a rush to tweak the code and see it run and
work the way it was designed, even if only evidenced by a simple green
bar on the screen. It's an experience only a programmer could understand.
-TimK
--
J. Timothy King team-oriented object-oriented Agile
www.jtse.com C++ Perl assembly embedded systems
------------------------------------------------------------------------
*Yahoo! Groups Links*
http://groups.yahoo.com/group/xpbookdiscussiongroup/
* Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service <http://docs.yahoo.com/info/terms/>.
Markus Silpala
2005-01-20 03:22:39 UTC
Permalink
Kent,

I am very curious: why did you use the term "test-first programming" in
XPE2E instead of "test-driven development?" My understanding has been
that TDD is the newer term for test-first, so it seems likely that you
deliberately chose to use the old term instead. I just don't understand
why.

Regards,

Markus Silpala
Amir Kolsky
2005-01-21 09:01:32 UTC
Permalink
My spin on the final D in TDD is Design. I find that testability forces a
better design by virtue of forcing the developer to actually understand what
they are trying to accomplish.

Also, the question that I like to pose to the programmers is: "How are you
going to test that the thing you want to do works?" I like to pose this
question because it usually leads to them asking "What am I trying to do?"

Amir Kolsky
XP& Software



_____

From: Markus Silpala [mailto:msilpala-***@public.gmane.org]
Sent: Thursday, January 20, 2005 5:23 AM
To: xpbookdiscussiongroup-***@public.gmane.org
Subject: Re: [xpe2e] Practice: Test-First Programming


Kent,

I am very curious: why did you use the term "test-first programming" in
XPE2E instead of "test-driven development?" My understanding has been
that TDD is the newer term for test-first, so it seems likely that you
deliberately chose to use the old term instead. I just don't understand
why.

Regards,

Markus Silpala



_____

Yahoo! Groups Links


* To visit your group on the web, go to:
http://groups.yahoo.com/group/xpbookdiscussiongroup/


* To unsubscribe from this group, send an email to:
xpbookdiscussiongroup-unsubscribe-***@public.gmane.org
<mailto:xpbookdiscussiongroup-unsubscribe-***@public.gmane.org?subject=Unsubscrib
e>


* Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
<http://docs.yahoo.com/info/terms/> .
John Goodsen
2005-01-21 18:10:17 UTC
Permalink
I think of TDD as Test Driven *DEVELOPMENT* - which includes (at least)
writing
customer acceptance tests at the start of the iteration as well as
programmer tests
before writing code. Kent, are you trying to make a distinction
between writing
acceptance tests (Story Tests) as a distinct practice from writing unit
tests?

----
John Goodsen RADSoft / Better Software Faster
jgoodsen-***@public.gmane.org Extreme Programmer and Coach
http://www.radsoft.com Enterprise Java and .NET Solutions
Post by Amir Kolsky
My spin on the final D in TDD is Design. I find that testability
forces a better design by virtue of forcing the developer to actually
understand what they are trying to accomplish.
 
Also, the question that I like to pose to the programmers is: "How are
you going to test that the thing you want to do works?" I like to pose
this question because it usually leads to them asking "What am I
trying to do?"
 
 Amir Kolsky
XP& Software
 
Sent: Thursday, January 20, 2005 5:23 AM
Subject: Re: [xpe2e] Practice: Test-First Programming
Kent,
I am very curious: why did you use the term "test-first programming" in
XPE2E instead of "test-driven development?" My understanding has been
that TDD is the newer term for test-first, so it seems likely that you
deliberately chose to use the old term instead. I just don't understand
why.
Regards,
Markus Silpala
Yahoo! Groups Links
http://groups.yahoo.com/group/xpbookdiscussiongroup/
 
 
• Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service.
Keith Ray
2005-01-21 18:44:06 UTC
Permalink
The distinction between test-first acceptance testing and test-first
programmer-tests is useful, if only to remind people that are leaving
something out if they don't do both of them.

Conversely, people could find having the two concepts separate as
giving them permission to do one but not the other.
Post by John Goodsen
I think of TDD as Test Driven *DEVELOPMENT* - which includes (at least)
writing
customer acceptance tests at the start of the iteration as well as
programmer tests
before writing code. Kent, are you trying to make a distinction
between writing
acceptance tests (Story Tests) as a distinct practice from writing unit
tests?
----
John Goodsen RADSoft / Better Software Faster
http://www.radsoft.com Enterprise Java and .NET Solutions
Post by Amir Kolsky
My spin on the final D in TDD is Design. I find that testability
forces a better design by virtue of forcing the developer to actually
understand what they are trying to accomplish.
Also, the question that I like to pose to the programmers is: "How are
you going to test that the thing you want to do works?" I like to pose
this question because it usually leads to them asking "What am I
trying to do?"
Amir Kolsky
XP& Software
Sent: Thursday, January 20, 2005 5:23 AM
Subject: Re: [xpe2e] Practice: Test-First Programming
Kent,
I am very curious: why did you use the term "test-first programming" in
XPE2E instead of "test-driven development?" My understanding has been
that TDD is the newer term for test-first, so it seems likely that you
deliberately chose to use the old term instead. I just don't understand
why.
Regards,
Markus Silpala
Yahoo! Groups Links
http://groups.yahoo.com/group/xpbookdiscussiongroup/
• Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service.
Yahoo! Groups Links
--
C. Keith Ray
<http://homepage.mac.com/keithray/blog/index.html>
<http://homepage.mac.com/keithray/xpminifaq.html>
<http://homepage.mac.com/keithray/resume2.html>
Kent Beck
2005-01-26 07:36:09 UTC
Permalink
John,

I wasn't trying to make that distinction. I believe writing
customer-oriented tests (just) in advance of implementation is valuable as
is writing programmer-oriented tests (just) in advance of implementation. I
intended to leave the level(s) at which to write the tests in test-first
programming to the discretion of the team. I see Test-Driven Development to
include both test-first programming AND incremental design.

Kent Beck
Three Rivers Institute

-----Original Message-----
From: John Goodsen [mailto:jgoodsen-***@public.gmane.org]
Sent: Friday, January 21, 2005 10:10 AM
To: xpbookdiscussiongroup-***@public.gmane.org
Subject: Re: [xpe2e] Practice: Test-First Programming


I think of TDD as Test Driven *DEVELOPMENT* - which includes (at least)
writing
customer acceptance tests at the start of the iteration as well as
programmer tests
before writing code. Kent, are you trying to make a distinction
between writing
acceptance tests (Story Tests) as a distinct practice from writing unit
tests?

----
John Goodsen RADSoft / Better Software Faster
jgoodsen-***@public.gmane.org Extreme Programmer and Coach
http://www.radsoft.com Enterprise Java and .NET Solutions
Post by Amir Kolsky
My spin on the final D in TDD is Design. I find that testability
forces a better design by virtue of forcing the developer to actually
understand what they are trying to accomplish.
 
Also, the question that I like to pose to the programmers is: "How are
you going to test that the thing you want to do works?" I like to pose
this question because it usually leads to them asking "What am I
trying to do?"
 
 Amir Kolsky
XP& Software
 
Sent: Thursday, January 20, 2005 5:23 AM
Subject: Re: [xpe2e] Practice: Test-First Programming
Kent,
I am very curious: why did you use the term "test-first programming" in
XPE2E instead of "test-driven development?" My understanding has been
that TDD is the newer term for test-first, so it seems likely that you
deliberately chose to use the old term instead. I just don't understand
why.
Regards,
Markus Silpala
Yahoo! Groups Links
http://groups.yahoo.com/group/xpbookdiscussiongroup/
 
 
• Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service.
Yahoo! Groups Links
Jeff Nielsen
2005-01-21 16:30:02 UTC
Permalink
Here's my take (until Kent responds with an authoritative answer).

Original term: Test-First Development (TFD)

Became: Test-Driven Development (TDD) in "TDD by Example". Kent wanted to
emphasize the facts that the tests /drive/ the code by being written first.

Then people started using TDD to mean all sorts of things--are we talking
about the customer tests or the programmer tests? Does "development" mean
coding or does it mean all of the SW development activities?

At some point Kent decides that TDD is actually a "conflation of two
concepts": test-first programming, and incremental design. In XPE2E, there
is a desire to separate the two again, because you can do "test-first
programming" without doing incremental design and vice versa. Also, the
term "programming" seems to make it more clear to people that we are talking
about the programmer tests.

So now: Test-First Programming is the "official" term for the practice.

How'd I do?

Jeff Nielsen
Digital Focus
www.digitalfocus.com

----- Original Message -----
From: "Markus Silpala" <msilpala-***@public.gmane.org>
To: <xpbookdiscussiongroup-***@public.gmane.org>
Sent: Wednesday, January 19, 2005 10:22 PM
Subject: Re: [xpe2e] Practice: Test-First Programming
Post by Markus Silpala
Kent,
I am very curious: why did you use the term "test-first programming" in
XPE2E instead of "test-driven development?" My understanding has been
that TDD is the newer term for test-first, so it seems likely that you
deliberately chose to use the old term instead. I just don't understand
why.
Regards,
Markus Silpala
Amir Kolsky
2005-01-21 17:48:19 UTC
Permalink
Plausible...

However, whereas test first can drive programming, well executed test first
drives your design...

The weak point of TDS (S = something) is that you need to know how to write
the tests. You can write tests that are simple and incrementally get you
nowhere, or you can write tests that are so big that they are in effect
system tests. So, testing first is not enough. Having the tests drive the
programming is also not enough (IMHO). You want your tests to also drive the
design.

As for the separating the design (a-la Incremental Design) and the
programming (a-la Test Driven Programming) -- well, you can't. The design
evolves through the programming. The programming evolve through the tests.
Hence - the design evolves through the tests.
QED :-)

Amir Kolsky
XP& Software



_____

From: Jeff Nielsen [mailto:jeff.nielsen-***@public.gmane.org]
Sent: Friday, January 21, 2005 6:30 PM
To: xpbookdiscussiongroup-***@public.gmane.org
Subject: Re: [xpe2e] Practice: Test-First Programming


Here's my take (until Kent responds with an authoritative answer).

Original term: Test-First Development (TFD)

Became: Test-Driven Development (TDD) in "TDD by Example". Kent wanted to
emphasize the facts that the tests /drive/ the code by being written first.

Then people started using TDD to mean all sorts of things--are we talking
about the customer tests or the programmer tests? Does "development" mean
coding or does it mean all of the SW development activities?

At some point Kent decides that TDD is actually a "conflation of two
concepts": test-first programming, and incremental design. In XPE2E, there
is a desire to separate the two again, because you can do "test-first
programming" without doing incremental design and vice versa. Also, the
term "programming" seems to make it more clear to people that we are talking

about the programmer tests.

So now: Test-First Programming is the "official" term for the practice.

How'd I do?

Jeff Nielsen
Digital Focus
www.digitalfocus.com

----- Original Message -----
From: "Markus Silpala" <msilpala-***@public.gmane.org>
To: <xpbookdiscussiongroup-***@public.gmane.org>
Sent: Wednesday, January 19, 2005 10:22 PM
Subject: Re: [xpe2e] Practice: Test-First Programming
Post by Markus Silpala
Kent,
I am very curious: why did you use the term "test-first programming" in
XPE2E instead of "test-driven development?" My understanding has been
that TDD is the newer term for test-first, so it seems likely that you
deliberately chose to use the old term instead. I just don't understand
why.
Regards,
Markus Silpala
_____

Yahoo! Groups Links


* To visit your group on the web, go to:
http://groups.yahoo.com/group/xpbookdiscussiongroup/


* To unsubscribe from this group, send an email to:
xpbookdiscussiongroup-unsubscribe-***@public.gmane.org
<mailto:xpbookdiscussiongroup-unsubscribe-***@public.gmane.org?subject=Unsubscrib
e>


* Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
<http://docs.yahoo.com/info/terms/> .
Kent Beck
2005-01-23 20:00:20 UTC
Permalink
Amir,

I think we agree that the combination of test-first programming and
incremental design is powerful. However, they can be separated. I can do
incremental design without tests. I did it for years in Smalltalk. Someone
can also make many design decisions before writing the first test. I
disagree that it is impossible to separate design and programming. I just
think it is more effective to put them together in a tight loop.

Kent Beck
Three Rivers Institute

_____

From: Amir Kolsky [mailto:amir-***@public.gmane.org]
Sent: Friday, January 21, 2005 9:48 AM
To: xpbookdiscussiongroup-***@public.gmane.org
Subject: RE: [xpe2e] Practice: Test-First Programming


Plausible...

However, whereas test first can drive programming, well executed test first
drives your design...

The weak point of TDS (S = something) is that you need to know how to write
the tests. You can write tests that are simple and incrementally get you
nowhere, or you can write tests that are so big that they are in effect
system tests. So, testing first is not enough. Having the tests drive the
programming is also not enough (IMHO). You want your tests to also drive the
design.

As for the separating the design (a-la Incremental Design) and the
programming (a-la Test Driven Programming) -- well, you can't. The design
evolves through the programming. The programming evolve through the tests.
Hence - the design evolves through the tests.
QED :-)

Amir Kolsky
XP& Software



_____

From: Jeff Nielsen [mailto:jeff.nielsen-***@public.gmane.org]
Sent: Friday, January 21, 2005 6:30 PM
To: xpbookdiscussiongroup-***@public.gmane.org
Subject: Re: [xpe2e] Practice: Test-First Programming


Here's my take (until Kent responds with an authoritative answer).

Original term: Test-First Development (TFD)

Became: Test-Driven Development (TDD) in "TDD by Example". Kent wanted to
emphasize the facts that the tests /drive/ the code by being written first.

Then people started using TDD to mean all sorts of things--are we talking
about the customer tests or the programmer tests? Does "development" mean
coding or does it mean all of the SW development activities?

At some point Kent decides that TDD is actually a "conflation of two
concepts": test-first programming, and incremental design. In XPE2E, there
is a desire to separate the two again, because you can do "test-first
programming" without doing incremental design and vice versa. Also, the
term "programming" seems to make it more clear to people that we are talking

about the programmer tests.

So now: Test-First Programming is the "official" term for the practice.

How'd I do?

Jeff Nielsen
Digital Focus
www.digitalfocus.com

----- Original Message -----
From: "Markus Silpala" <msilpala-***@public.gmane.org>
To: <xpbookdiscussiongroup-***@public.gmane.org>
Sent: Wednesday, January 19, 2005 10:22 PM
Subject: Re: [xpe2e] Practice: Test-First Programming
Post by Markus Silpala
Kent,
I am very curious: why did you use the term "test-first programming" in
XPE2E instead of "test-driven development?" My understanding has been
that TDD is the newer term for test-first, so it seems likely that you
deliberately chose to use the old term instead. I just don't understand
why.
Regards,
Markus Silpala
_____

Yahoo! Groups Links


* To visit your group on the web, go to:
http://groups.yahoo.com/group/xpbookdiscussiongroup/


* To unsubscribe from this group, send an email to:
xpbookdiscussiongroup-unsubscribe-***@public.gmane.org
<mailto:xpbookdiscussiongroup-unsubscribe-***@public.gmane.org?subject=Unsubscrib
e>


* Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
<http://docs.yahoo.com/info/terms/> .
Kent Beck
2005-01-22 00:27:10 UTC
Permalink
Markus,

I had two reasons for the change. First, TDD as described in my book
combines TFP and incremental design. By splitting out the two elements as
separate practices people could think about how to apply them individually.
For example, a team could look at TFP and apply it by writing their tests
soon after writing the code instead of months later. Second, I'm looking
towards the day when renaming XP will be the right thing to do. My favorite
alternative name is Test-Driven Development. Not using TDD to describe a
technical practice any more clears the name for wider use later.

Kent Beck
Three Rivers Institute

-----Original Message-----
From: Markus Silpala [mailto:msilpala-***@public.gmane.org]
Sent: Wednesday, January 19, 2005 7:23 PM
To: xpbookdiscussiongroup-***@public.gmane.org
Subject: Re: [xpe2e] Practice: Test-First Programming


Kent,

I am very curious: why did you use the term "test-first programming" in
XPE2E instead of "test-driven development?" My understanding has been
that TDD is the newer term for test-first, so it seems likely that you
deliberately chose to use the old term instead. I just don't understand
why.

Regards,

Markus Silpala




Yahoo! Groups Links
Steven Gordon
2005-01-21 18:38:18 UTC
Permalink
In the context of XP, I agree that you really cannot separate design from programming. However, in the context of a waterfall-style development approach, Test First Programming could be applied to implementing designs created earlier in the design phase.

-----Original Message-----
From: Amir Kolsky [mailto:amir-***@public.gmane.org]
Sent: Friday, January 21, 2005 10:48 AM
To: xpbookdiscussiongroup-***@public.gmane.org
Subject: RE: [xpe2e] Practice: Test-First Programming


[CLIP]

As for the separating the design (a-la Incremental Design) and the programming (a-la Test Driven Programming) -- well, you can't. The design evolves through the programming. The programming evolve through the tests. Hence - the design evolves through the tests.
QED :-)

Amir Kolsky
XP& Software
geektank
2005-01-29 01:35:31 UTC
Permalink
Post by Kent Beck
The XP community hasn't done much exploration of alternatives to
tests for
Post by Kent Beck
verifying the behavior of the system. Tools like static analysis and
model
Post by Kent Beck
checking could be used test-first style. You start with a "test"
that says,
Post by Kent Beck
for example, that there are no deadlocks in the system. After every
change,
Post by Kent Beck
you verify again that there are no deadlocks. The static analysis
tools I've
Post by Kent Beck
seen aren't intended to be used this way. They run too slowly to be
part of
Post by Kent Beck
the minute-by-minute cycle of programming. However, this seems to be
merely
Post by Kent Beck
a matter of focus, not a fundamental limitation.
Continuing with the extreme I think this is a valuable part of test
first, if anything can be proven via static analysis of the code,
let's gets those "Must be secure", "Must be safe" story tests in as
soon as possible. I am all for making the world safe from programmers.
Loading...