Friday, October 10, 2009

Free chm ebook download: Test-Driven Development By Example

Test-Driven Development By Example
By Kent Beck
    
Publisher : Addison Wesley
Pub Date : November 08, 2002
ISBN : 0-321-14653-0
Pages : 240

Clean code that works - now. This is the seeming contradiction that lies behind much of the pain of programming. Test-driven development replies to this contradiction with a paradox-test the program before you write it.

A new idea? Not at all. Since the dawn of computing, programmers have been specifying the inputs and outputs before programming precisely. Test-driven development takes this age-old idea, mixes it with modern languages and programming environments, and cooks up a tasty stew guaranteed to satisfy your appetite for clean code that works-now.

Developers face complex programming challenges every day, yet they are not always readily prepared to determine the best solution. More often than not, such difficult 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.

Readers will learn to:

Solve complicated tasks, beginning with the simple and proceeding to the more complex.

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.

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.

It is a predictable way to develop. You know when you are finished, without having to worry about a long bug trail.

It gives you a chance to learn all of the lessons that the code has to teach you. If you only slap together the first thing you think of, then you never have time to think of a second, better thing.

It improves the lives of the users of your software.

It lets your teammates count on you, and you on them.

It feels good to write it.

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

Write new code only if an automated test has failed

Eliminate duplication

These are two simple rules, but they generate complex individual and group behavior with technical implications such as the following.

We must design organically, with running code providing feedback between decisions.

We must write our own tests, because we can't wait 20 times per day for someone else to write a test.

Our development environment must provide rapid response to small changes.

Our designs must consist of many highly cohesive, loosely coupled components, just to make testing easy.

The two rules imply an order to the tasks of programming.

Red?/span> Write a little test that doesn't work, and perhaps doesn't even compile at first.

Green?/span> Make the test work quickly, committing whatever sins necessary in the process.

Refactor?/span> Eliminate all of the duplication created in merely getting the test to work.

Red/green/refactor梩he 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.

If the defect density can be reduced enough, then quality assurance (QA) can shift from reactive work to proactive work.

If the number of nasty surprises can be reduced enough, then project managers can estimate accurately enough to involve real customers in daily development.

If the topics of technical conversations can be made clear enough, then software engineers can work in minute-by-minute collaboration instead of daily or weekly collaboration.

Again, if the defect density can be reduced enough, then we can have shippable software with new functionality every day, leading to new business relationships with customers.

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.

Download the free chm ebook: Test-Driven Development By Example

Tags: test, development