Test Driven Development: By Example / Edition 1 available in Paperback, eBook

- ISBN-10:
- 0321146530
- ISBN-13:
- 9780321146533
- Pub. Date:
- 11/08/2002
- Publisher:
- Pearson Education
- ISBN-10:
- 0321146530
- ISBN-13:
- 9780321146533
- Pub. Date:
- 11/08/2002
- Publisher:
- Pearson Education

Buy New
$66.49Buy Used
$16.46
-
SHIP THIS ITEMIn stock. Ships in 1-2 days.PICK UP IN STORE
Your local store may have stock of this item.
Available within 2 business hours
$16.46-
SHIP THIS ITEM
Temporarily Out of Stock Online
Please check back later for updated availability.
Overview
Product Details
ISBN-13: | 9780321146533 |
---|---|
Publisher: | Pearson Education |
Publication date: | 11/08/2002 |
Series: | Addison-Wesley Signature Series (Beck) |
Edition description: | New Edition |
Pages: | 240 |
Product dimensions: | 7.30(w) x 9.10(h) x 0.60(d) |
About the Author
Read an Excerpt
Clean code that works, in Ron Jeffries' pithy phrase, is the goal of Test-Driven Development (TDD). Clean code that works is a worthwhile goal for a whole bunch of reasons.
But how do we get to clean code that works? Many forces drive us away from clean code, and even from code that works. Without taking too much counsel of our fears, here's what we do: we drive development with automated tests, a style of development called Test-Driven Development (TDD). In Test-Driven Development, we
These are two simple rules, but they generate complex individual and group behavior with technical implications such as the following.
The two rules imply an order to the tasks of programming.
Red/green/refactorthe TDD mantra.
Assuming for the moment that such a programming style is possible, it further might be possible to dramatically reduce the defect density of code and make the subject of work crystal clear to all involved. If so, then writing only that code which is demanded by failing tests also has social implications.
So the concept is simple, but what's my motivation? Why would a software engineer take on the additional work of writing automated tests? Why would a software engineer work in tiny little steps when his or her mind is capable of great soaring swoops of design? Courage.
Courage
Test-driven development is a way of managing fear during programming. I don't mean fear in a bad waypow widdle prwogwammew needs a pacifiew-but fear in the legitimate, this-is-a-hard-problem-and-I-can't-see-the-end-from-the-beginning sense. If pain is nature's way of saying "Stop!" then fear is nature's way of saying "Be careful." Being careful is good, but fear has a host of other effects.
None of these effects are helpful when programming, especially when programming something hard. So the question becomes how we face a difficult situation and,
Imagine programming as turning a crank to pull a bucket of water from a well. When the bucket is small, a free-spinning crank is fine. When the bucket is big and full of water, you're going to get tired before the bucket is all the way up. You need a ratchet mechanism to enable you to rest between bouts of cranking. The heavier the bucket, the closer the teeth need to be on the ratchet.
The tests in test-driven development are the teeth of the ratchet. Once we get one test working, we know it is working, now and forever. We are one step closer to having everything working than we were when the test was broken. Now we get the next one working, and the next, and the next. By analogy, the tougher the programming problem, the less ground that each test should cover.
Readers of my book Extreme Programming Explained will notice a difference in tone between Extreme Programming (XP) and TDD. TDD isn't an absolute the way that XP is. XP says, "Here are things you must be able to do to be prepared to evolve further." TDD is a little fuzzier. TDD is an awareness of the gap between decision and feedback during programming, and techniques to control that gap. "What if I do a paper design for a week, then test-drive the code? Is that TDD?" Sure, it's TDD. You were aware of the gap between decision and feedback, and you controlled the gap deliberately.
That said, most people who learn TDD find that their programming practice changed for good. Test Infected is the phrase Erich Gamma coined to describe this shift. You might find yourself writing more tests earlier, and working in smaller steps than you ever dreamed would be sensible. On the other hand, some software engineers learn TDD and then revert to their earlier practices, reserving TDD for special occasions when ordinary programming isn't making progress.
There certainly are programming tasks that can't be driven solely by tests (or at least, not yet). Security software and concurrency, for example, are two topics where TDD is insufficient to mechanically demonstrate that the goals of the software have been met. Although it's true that security relies on essentially defect-free code, it also relies on human judgment about the methods used to secure the software. Subtle concurrency problems can't be reliably duplicated by running the code.
Once you are finished reading this book, you should be ready to
This book is organized in three parts.
I wrote the examples imagining a pair programming session. If you like looking at the map before wandering around, then you may want to go straight to the patterns in Part III and use the examples as illustrations. If you prefer just wandering around and then looking at the map to see where you've been, then try reading through the examples, referring to the patterns when you want more detail about a technique, and using the patterns as a reference. Several reviewers of this book commented they got the most out of the examples when they started up a programming environment, entered the code, and ran the tests as they read.
A note about the examples. Both of the examples, multi-currency calculation and a testing framework, appear simple. There are (and I have seen) complicated, ugly, messy ways of solving the same problems. I could have chosen one of those complicated, ugly, messy solutions, to give the book an air of "reality." However, my goal, and I hope your goal, is to write clean code that works. Before teeing off on the examples as being too simple, spend 15 seconds imagining a programming world in which all code was this clear and direct, where there were no complicated solutions, only apparently complicated problems begging for careful thought. TDD can help you to lead yourself to exactly that careful thought.
0321146530P10172002
Table of Contents
Preface.Acknowledgments.
Introduction.
I. THE MONEY EXAMPLE.
1. Multi-Currency Money.
2. Degenerate Objects.
3. Equality for All.
4. Privacy.
5. Franc-ly Speaking.
6. Equality for All, Redux.
7. Apples and Oranges.
8. Makin' Objects.
9. Times We're Livin' In.
10. Interesting Times.
11. The Root of All Evil.
12. Addition, Finally.
13. Make It.
14. Change.
15. Mixed Currencies.
16. Abstraction, Finally.
17. Money Retrospective.
II. The xUnit Example.
18. First Steps to xUnit.
19. Set the Table.
20. Cleaning Up After.
21. Counting.
22. Dealing with Failure.
23. How Suite It Is.
24. xUnit Retrospective.
III. Patterns for Test-Driven Development.
25. Test-Driven Development Patterns.
26. Red Bar Patterns.
27. Testing Patterns.
28. Green Bar Patterns.
29. xUnit Patterns.
30. Design Patterns.
31. Refactoring.
32. Mastering TDD.
Appendix I: Influence Diagrams.
Appendix II: Fibonacci.
Afterword.
Index. 0321146530T10172002
Preface
Clean code that works, in Ron Jeffries' pithy phrase, is the goal of Test-Driven Development (TDD). Clean code that works is a worthwhile goal for a whole bunch of reasons.
But how do we get to clean code that works? Many forces drive us away from clean code, and even from code that works. Without taking too much counsel of our fears, here's what we do: we drive development with automated tests, a style of development called Test-Driven Development (TDD). In Test-Driven Development, we
These are two simple rules, but they generate complex individual and group behavior with technical implications such as the following.
The two rules imply an order to the tasks of programming.
Red/green/refactorthe TDD mantra.
Assuming for the moment that such a programming style is possible, it further might be possible to dramatically reduce the defect density of code and make the subject of work crystal clear to all involved. If so, then writing only that code which is demanded by failing tests also has social implications.
So the concept is simple, but what's my motivation? Why would a software engineer take on the additional work of writing automated tests? Why would a software engineer work in tiny little steps when his or her mind is capable of great soaring swoops of design? Courage.
CourageTest-driven development is a way of managing fear during programming. I don't mean fear in a bad waypow widdle prwogwammew needs a pacifiew-but fear in the legitimate, this-is-a-hard-problem-and-I-can't-see-the-end-from-the-beginning sense. If pain is nature's way of saying 'Stop!' then fear is nature's way of saying 'Be careful.' Being careful is good, but fear has a host of other effects.
None of these effects are helpful when programming, especially when programming something hard. So the question becomes how we face a difficult situation and,
Imagine programming as turning a crank to pull a bucket of water from a well. When the bucket is small, a free-spinning crank is fine. When the bucket is big and full of water, you're going to get tired before the bucket is all the way up. You need a ratchet mechanism to enable you to rest between bouts of cranking. The heavier the bucket, the closer the teeth need to be on the ratchet.
The tests in test-driven development are the teeth of the ratchet. Once we get one test working, we know it is working, now and forever. We are one step closer to having everything working than we were when the test was broken. Now we get the next one working, and the next, and the next. By analogy, the tougher the programming problem, the less ground that each test should cover.
Readers of my book Extreme Programming Explained will notice a difference in tone between Extreme Programming (XP) and TDD. TDD isn't an absolute the way that XP is. XP says, 'Here are things you must be able to do to be prepared to evolve further.' TDD is a little fuzzier. TDD is an awareness of the gap between decision and feedback during programming, and techniques to control that gap. 'What if I do a paper design for a week, then test-drive the code? Is that TDD?' Sure, it's TDD. You were aware of the gap between decision and feedback, and you controlled the gap deliberately.
That said, most people who learn TDD find that their programming practice changed for good. Test Infected is the phrase Erich Gamma coined to describe this shift. You might find yourself writing more tests earlier, and working in smaller steps than you ever dreamed would be sensible. On the other hand, some software engineers learn TDD and then revert to their earlier practices, reserving TDD for special occasions when ordinary programming isn't making progress.
There certainly are programming tasks that can't be driven solely by tests (or at least, not yet). Security software and concurrency, for example, are two topics where TDD is insufficient to mechanically demonstrate that the goals of the software have been met. Although it's true that security relies on essentially defect-free code, it also relies on human judgment about the methods used to secure the software. Subtle concurrency problems can't be reliably duplicated by running the code.
Once you are finished reading this book, you should be ready to
This book is organized in three parts.
I wrote the examples imagining a pair programming session. If you like looking at the map before wandering around, then you may want to go straight to the patterns in Part III and use the examples as illustrations. If you prefer just wandering around and then looking at the map to see where you've been, then try reading through the examples, referring to the patterns when you want more detail about a technique, and using the patterns as a reference. Several reviewers of this book commented they got the most out of the examples when they started up a programming environment, entered the code, and ran the tests as they read.
A note about the examples. Both of the examples, multi-currency calculation and a testing framework, appear simple. There are (and I have seen) complicated, ugly, messy ways of solving the same problems. I could have chosen one of those complicated, ugly, messy solutions, to give the book an air of 'reality.' However, my goal, and I hope your goal, is to write clean code that works. Before teeing off on the examples as being too simple, spend 15 seconds imagining a programming world in which all code was this clear and direct, where there were no complicated solutions, only apparently complicated problems begging for careful thought. TDD can help you to lead yourself to exactly that careful thought.