ptg From the Library of Lee Bogdanoff www.it-ebooks.info ptg Praise for Growing Object-Oriented Software, Guided by Tests “The authors of this book have led a revolution in the craft of programming by controlling the environment in which software grows. Their Petri dish is the mock object, and their microscope is the unit test. This book can show you how these tools introduce a repeatability to your work that would be the envy of any scientist.” —Ward Cunningham “At last a book, suffused with code, that exposes the deep symbiosis between TDD and OOD. The authors, pioneers in test-driven development, have packed it with principles, practices, heuristics, and (best of all) anecdotes drawn from their decades of professional experience. Every software craftsman will want to pore over the chapters of worked examples and study the advanced testing and design principles. This one’s a keeper.” —Robert C. Martin “Design is often discussed in depth, but without empiricism. Testing is often promoted, but within the narrow definition of quality that relates only to the presence or absence of defects. Both of these perspectives are valuable, but each on its own offers little more than the sound of one hand clapping. Steve and Nat bring the two hands together in what deserves—and can best be described as—applause. With clarity, reason, and humour, their tour de force reveals a view of design, testing, code, objects, practice, and process that is compelling, practical, and overflowing with insight.” —Kevlin Henney, co-author of Pattern-Oriented Software Architecture and 97 Things Every Programmer Should Know “Steve and Nat have written a wonderful book that shares their software craftsmanship with the rest of the world. This is a book that should be studied rather than read, and those who invest sufficient time and energy into this effort will be rewarded with superior development skills.” —David Vydra, publisher, testdriven.com “This book presents a unique vision of test-driven development. It describes the mature form of an alternative strain of TDD that sprang up in London in the early 2000s, characterized by a totally end-to-end approach and a deep emphasis on the messaging aspect of objects. If you want to be an expert in the state of the art in TDD, you need to understand the ideas in this book.” —Michael Feathers “With this book you’ll learn the rhythms, nuances in thinking, and effective programming practices for growing tested, well-designed object-oriented applications from the masters.” —Rebecca Wirfs-Brock From the Library of Lee Bogdanoff www.it-ebooks.info ptg This page intentionally left blank From the Library of Lee Bogdanoff www.it-ebooks.info ptg Growing Object-Oriented Software, Guided by Tests From the Library of Lee Bogdanoff www.it-ebooks.info ptg T he Addison-Wesley Signature Series provides readers with practical and authoritative information on the latest trends in modern technology for computer professionals. The series is based on one simple premise: Great books come from great authors. Books in the series are personally chosen by expert advisors, world-class authors in their own right. These experts are proud to put their signatures on the covers, and their signatures ensure that these thought leaders have worked closely with authors to define topic coverage, book scope, critical content, and overall uniqueness. The expert signatures also symbolize a promise to our readers: You are reading a future classic. Visit informit.com/awss for a complete list of available products. The Addison-Wesley Signature Series Kent Beck, Mike Cohn, and Martin Fowler, Consulting Editors From the Library of Lee Bogdanoff www.it-ebooks.info ptg Growing Object-Oriented Software, Guided by Tests Steve Freeman and Nat Pryce Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City From the Library of Lee Bogdanoff www.it-ebooks.info ptg Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382–3419 corpsales@pearsontechgroup.com For sales outside the United States please contact: International Sales international@pearson.com Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data: Freeman, Steve, 1958- Growing object-oriented software, guided by tests / Steve Freeman and Nat Pryce. p. cm. ISBN 978-0-321-50362-6 (pbk. : alk. paper) 1. Object-oriented programming (Computer science) 2. Computer software Testing. I. Pryce, Nat. II. Title. QA76.64.F747 2010 005.1'17 dc22 2009035239 Copyright © 2010 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax (617) 671 3447 ISBN-13: 978–0–321–50362–6 ISBN-10: 0–321–50362–7 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing October 2009 From the Library of Lee Bogdanoff www.it-ebooks.info ptg To Paola, for all her support; to Philip, who sometimes missed out —Steve To Lamaan who put up with me spending time writing this book, and Oliver Tarek who did not —Nat From the Library of Lee Bogdanoff www.it-ebooks.info ptg This page intentionally left blank From the Library of Lee Bogdanoff www.it-ebooks.info ptg Contents xvForeword xviiPreface xxiAcknowledgments xxiiiAbout the Authors 1Part I: Introduction 3Chapter 1: What Is the Point of Test-Driven Development? 3Software Development as a Learning Process 4Feedback Is the Fundamental Tool 5Practices That Support Change 6Test-Driven Development in a Nutshell 7The Bigger Picture 8Testing End-to-End 9Levels of Testing 10External and Internal Quality 13Chapter 2: Test-Driven Development with Objects 13A Web of Objects 13Values and Objects 14Follow the Messages 17Tell, Don’t Ask 17But Sometimes Ask 18Unit-Testing the Collaborating Objects 19Support for TDD with Mock Objects ix From the Library of Lee Bogdanoff www.it-ebooks.info [...]... diversity of dialects has given me a new source of ideas to further refine my own development Growing Object-Oriented Software, Guided by Tests, presents a coherent, consistent system of development, where different techniques support each other I invite you to read Growing Object-Oriented Software, Guided by Tests, to follow along with the examples, to learn how the authors think about programming and... Library of Lee Bogdanoff Foreword Kent Beck One of the dilemmas posed by the move to shorter and shorter release cycles is how to release more software in less time—and continue releasing indefinitely A new perspective is necessary to resolve this dilemma More than a shift in techniques is needed Growing Object-Oriented Software, Guided by Tests presents such a new perspective What if software wasn’t “made,”... works.” Growing also hints at the biological quality we see in good software, the sense of coherence at every level of structure It ties into our approach to object xvii www.it-ebooks.info From the Library of Lee Bogdanoff xviii Preface orientation which follows Alan Kay’s1 concept of objects being similar to biological cells that send each other messages Why Guided by Tests? We write tests first... of discussion in the TDD world over the terminology for what we’re calling acceptance tests: “functional tests, ” “customer tests, ” “system tests. ” Worse, our definitions are often not the same as those used by professional software testers The important thing is to be clear about our intentions We use “acceptance tests to help us, with the domain experts, understand and agree on what we are going to... is a measure of demonstrable progress, and the growing suite of tests protects us against regression failures when we change the system Acceptance tests often take a while to make pass, certainly more than one check-in episode, so we usually distinguish between acceptance tests we’re working on (which are not yet included in the build) and acceptance tests for the features that have been finished (which... about the same time (and even teams that write the tests first) where the code is a mess and the tests just raise the cost of maintenance They’d made a start but hadn’t yet learned that the trick, as the title of the book suggests, is to let the tests guide development Use the contents of the tests to stay focused on making progress and feedback from the tests to raise the quality of the system What about... [Fowler99] The Bigger Picture It is tempting to start the TDD process by writing unit tests for classes in the application This is better than having no tests at all and can catch those basic programming errors that we all know but find so hard to avoid: fencepost errors, incorrect boolean expressions, and the like But a project with only unit tests is missing out on critical benefits of the TDD process We’ve... tests give us a safety net of regression coverage We use the term guided because the technique still requires skill and experience We found test-driven development to be an effective design support tool—once we’d learned how to develop incrementally and to “listen to the tests. ” Like any serious design activity, TDD requires understanding and sustained effort to work We’ve seen teams that write tests. .. to understand The programmer makes sure that the system is still working after each refactoring step, minimizing the risk of getting stranded by a change; in test-driven code, we can do that by running the tests Refactoring is a “microtechnique” that is driven by finding small-scale improvements Our experience is that, applied rigorously and consistently, its many small steps can lead to significant structural... preferred implementation of the “role” of acceptance testing is to write end-to-end tests which, as we just noted, should be as end-to-end as possible; our bias often leads us to use these terms interchangeably although, in some cases, acceptance tests might not be end-to-end We use the term integration tests to refer to the tests that check how some of our code works with code from outside the team that . Bogdanoff www.it-ebooks.info ptg Praise for Growing Object-Oriented Software, Guided by Tests “The authors of this book have led a revolution in the craft of programming by controlling the. 1958- Growing object-oriented software, guided by tests / Steve Freeman and Nat Pryce. p. cm. ISBN 978-0-321-50362-6 (pbk. : alk. paper) 1. Object-oriented