Test Driven Development: By Example
When faced with a complex programming challenge, rare is the developer who can readily see how to begin or produce its solution. More often than not, such projects generate a great deal of stress and bad code. To garner the strength and courage needed to surmount seemingly Herculean tasks, programmers should look to test-driven development (TDD), a proven set of techniques that encourage simple designs and test suites that inspire confidence.
By driving development with automated tests and then eliminating duplication, any developer can write reliable, bug-free code no matter what its level of complexity. Moreover, TDD encourages programmers to learn quickly, communicate more clearly, and seek out constructive feedback.
In Test-Driven Development, readers will learn to:
- Start complicated tasks simply.
- Write automated tests before coding.
- Grow a design organically by refactoring to add design decisions one at a time.
- Create tests for more complicated logic, including reflection and exceptions.
- Use patterns to decide what tests to write.
- Create tests using xUnit, the architecture at the heart of many programmer-oriented testing tools.
This book follows two TDD projects from start to finish; illustrating techniques programmers can use to easily and dramatically increase the quality of their work. The examples are followed by references to the featured TDD patterns and refactorings. With its emphasis on agile methods and fast development strategies, Test-Driven Development is sure to inspire readers to embrace these under-utilized but powerful techniques.
Review By: Rick Martin
07/08/2010This book is a "how to" for the application of the TDD methodology derived from XP. TDD is a powerful and adaptive methodology that improves the chances of producing "clean code that works." In TDD, a programmer begins each development task with one or with a small group of tests that indicate success when they all pass. It is powerful because it puts the focus on quality and testability of software products right where it belongs, in the hands of the programmer.
TDD is adaptive and can be tailored to match the task to which it is being applied and the skill level of the practitioner. A programmer can take big or small steps, increasing or decreasing the step size to match their comfort level. Beck walks you through two implementations using the Red/Green/Re-factor recipe showing at every opportunity how the programmer receives nearly continuous positive feedback.
Instead of waiting for integration points that require long compilation and test cycles, progress is visible many times during the programming session, providing increased confidence in the project. With each new "task" the programmer is reassured regressions have not been introduced because of the continually growing set of regression tests that result from adopting the TDD methodology.
Following this methodology, the programmer naturally creates a test library for future use. TDD is particularly adept at guiding the developer when an exhaustive set of requirements is not available, or the programmer knows that the product will evolve in the future.
TDD helps promote the "Keep It Simple" goal by prompting the programmer to write the minimum code required to satisfy the test. No longer will the "product be complete except for testing." In TDD testing becomes the initiating task for all development and the resulting code is not only functional, but clean.
I am admittedly an XP skeptic having been educated in the old school of the design-implement-validate waterfall methodology. However, TDD is a methodology that I feel makes sense, particularly in projects where the complete requirements are unknown, which is the default for most projects. I felt this is an excellent treatise of TDD, occasionally funny and often very insightful. Beck demonstrates not only his understanding of the methodology, but also the programmer psyche. The two examples used for illustration were complex enough to not be dismissed as concocted, but simple enough so that the reader can focus on the subject matter and not the example problem.
It is clear Beck understands what motivates programmers is making progress and all programmers want to write clean code. Beck presents the material in a very readable, plain English narrative that focuses on the application of TDD as a tool and not dogma.
With occasional anecdotes used to reinforce his message, the author reassures the reader that TDD will not only improve the product, but it will also make the development process less stressful. Test Driven Development can be an important force in the software industry if we can embrace it. This book is a quick read and answers many questions.
Though in fact, the concept is not really new, unit tests have been a "best practice" since the early days of software development. Using two examples, the author demonstrates concretely the benefits of TDD to both the product and the practitioner. I would recommend this book to anyone who wants to improve the code they produce without incurring the cost of adopting heavyweight design-develop-test cycles that were once prescribed as Software Engineering.