Download from Wow! eBook <www.wowebook.com> What People Are Saying About Test-Driven Development for Embedded C In this much-needed book, Agile methods expert James Grenning con- cisely demonstrates why and how to apply Test-Driven Development in embedded software development. Coming from a purely embedded background, I was myself skeptical about TDD initially. But with this book by my side, I’m ready to plunge right in and certain I can apply TDD even to device drivers and other challenging low-level code. Michael Barr Author of Programming Embedded Systems:WithCandGNU DevelopmentTools and Embedded CCodingStandard, Netrino, Inc. “Test-Driven Development cannot work for us! We work in C, and Test-Driven Development requires an object-oriented language such as Java!” I frequently hear statements such as these when coaching teams in TDD in C. I’ve always pointed them to the work of James Grenning, such as the article “Embedded TDD Cycle.” James is a true pioneer in applying Agile development techniques to embedded prod- uct development. I was really excited when he told me he was going to write this book because I felt it would definitively help the embed- ded Agile community forward. It took James more than two years, but the result, this book, was worth waiting for. This is a good and useful book that every embedded developer should read. Bas Vodde Author of ScalingLeanandAgileDevelopment and Practices forScalingLeanandAgileDevelopment, Odd-e, Singapore I have been preaching and teaching TDD in C for years, and finally there is a book I can recommend to fellow C programmers who want to learn more about modern programming techniques. Olve Maudal C programmer, Cisco Systems Download from Wow! eBook <www.wowebook.com> This book is a practical guide that sheds light on how to apply Agile development practices in the world of embedded software. You’ll soon be writing tests that help you pinpoint problems early and avoid hours tearing your hair out trying to figure out what’s going on. From my experience writing code for robotics, telemetry, and telecommunica- tions products, I can heartily recommend reading this book; it’s a great way to learn how you can apply Test-Driven Development for embedded C. Rachel Davies Author of AgileCoaching, Agile Experience Limited This is a long-awaited book. It guides the reader through the unique challenges of applying Test-Driven Development to developing embed- ded software in C. It explains the principles and techniques of TDD using code examples, creating a clear path from start to finish. I rec- ommend this book to anyone involved in embedded software develop- ment who is interested in doing it better. Timo Punkka Software Development Manager, Schneider Electric This book is targeting the embedded-programmer-on-the-street and hits its target. It is neither spoon-fed baby talk nor useless theory- spin. In clear and simple prose, James shows working geeks each of the TDD concepts and their C implementations. Any C programmer can benefit from working through this book. Michael “GeePaw” Hill Senior TDD coach, Anarchy Creek Software Test-Driven DevelopmentforEmbedded C is the first book I would rec- ommend to both C and C++ developers wanting to learn TDD, whether or not their target is an embedded platform. It’s just that good. C. Keith Ray Agile coach/trainer, Industrial Logic, Inc. Download from Wow! eBook <www.wowebook.com> Download from Wow! eBook <www.wowebook.com> Test-Driven Development for Embedded C James W. Grenning The Pragmatic Bookshelf Raleigh,NorthCarolina Dallas,Texas Download from Wow! eBook <www.wowebook.com> Many of the designations used by manufacturers and sellers to distinguish their prod- uctsareclaimedastrademarks.Wherethosedesignationsappearinthisbook,andThe Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The PragmaticProgrammer,PragmaticProgramming,PragmaticBookshelfandthelinkingg devicearetrademarksofThePragmaticProgrammers,LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibilityfor errorsor omissions, or for damages that may result from theuseofinformation(includingprogramlistings)containedherein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatictitles,pleasevisitusathttp://www.pragprog.com. Theteamthatproducedthisbookincludes: Editor: JacquelynCarter Indexing: PotomacIndexing,LLC Copyedit: KimWimpsett Production: JanetFurlow Customersupport: EllieCallahan International: JulietBenda Copyright©2011JamesW.Grenning. Allrightsreserved. Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise,withoutthepriorconsentofthepublisher. PrintedintheUnitedStatesofAmerica. ISBN-10:1-934356-62-X ISBN-13:978-1-934356-62-3 Printedonacid-freepaper. P1.0printing,April,2011 Version:2011-4-6 Download from Wow! eBook <www.wowebook.com> Indedicationtomydad, forgivingmeagoodcompass, andmylovingwifeMarilee forhelpingmenotloseit. Download from Wow! eBook <www.wowebook.com> Contents Foreword by Jack Ganssle 13 Foreword by Robert C. Martin 15 Acknowledgments 18 Preface 20 Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . 21 How to Read This Book . . . . . . . . . . . . . . . . . . . . . 21 The Code in This Book . . . . . . . . . . . . . . . . . . . . . 22 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . 23 1 Test-Driven Development 24 1.1 Why Do We Need TDD? . . . . . . . . . . . . . . . . . 25 1.2 What Is Test-Driven Development? . . . . . . . . . . 27 1.3 Physics of TDD. . . . . . . . . . . . . . . . . . . . . . 28 1.4 The TDD Microcycle . . . . . . . . . . . . . . . . . . . 29 1.5 TDD Benefits . . . . . . . . . . . . . . . . . . . . . . . 32 1.6 Benefits for Embedded . . . . . . . . . . . . . . . . . 33 I Getting Started 35 2 Test-Driving Tools and Conventions 36 2.1 What Is a Unit Test Harness? . . . . . . . . . . . . . 36 2.2 Unity: A C-Only Test Harness . . . . . . . . . . . . . 38 2.3 CppUTest: A C++ Unit Test Harness . . . . . . . . . 44 2.4 Unit Tests Can Crash . . . . . . . . . . . . . . . . . . 48 2.5 The Four-Phase Test Pattern . . . . . . . . . . . . . 49 2.6 Where Are We? . . . . . . . . . . . . . . . . . . . . . . 49 Download from Wow! eBook <www.wowebook.com> CONTENTS 9 3 Starting a C Module 51 3.1 Elements of a Testable C Module . . . . . . . . . . . 51 3.2 What Does an LED Driver Do? . . . . . . . . . . . . 53 3.3 Write a Test List . . . . . . . . . . . . . . . . . . . . . 54 3.4 Writing the First Test . . . . . . . . . . . . . . . . . . 55 3.5 Test-Drive the Interface Before the Internals . . . . 61 3.6 Incremental Progress . . . . . . . . . . . . . . . . . . 68 3.7 Test-Driven Developer State Machine . . . . . . . . 70 3.8 Tests Are FIRST . . . . . . . . . . . . . . . . . . . . . 72 3.9 Where Are We? . . . . . . . . . . . . . . . . . . . . . . 72 4 Testing Your Way to Done 75 4.1 Grow the Solution from Simple Beginnings . . . . . 75 4.2 Keep the Code Clean—Refactor as You Go . . . . . . 91 4.3 Repeat Until Done . . . . . . . . . . . . . . . . . . . . 94 4.4 Take a Step Back Before Claiming Done . . . . . . . 101 4.5 Where Are We? . . . . . . . . . . . . . . . . . . . . . . 101 5 Embedded TDD Strategy 104 5.1 The Target Hardware Bottleneck . . . . . . . . . . . 104 5.2 Benefits of Dual-Targeting . . . . . . . . . . . . . . . 106 5.3 Risks of Dual-Target Testing . . . . . . . . . . . . . . 107 5.4 The Embedded TDD Cycle . . . . . . . . . . . . . . . 108 5.5 Dual-Target Incompatibilities . . . . . . . . . . . . . 111 5.6 Testing with Hardware . . . . . . . . . . . . . . . . . 116 5.7 Slow Down to Go Fast . . . . . . . . . . . . . . . . . 120 5.8 Where Are We? . . . . . . . . . . . . . . . . . . . . . . 120 6 Yeah, but... 122 6.1 We Don’t Have Time. . . . . . . . . . . . . . . . . . . 122 6.2 Why Not Write Tests After the Code? . . . . . . . . . 126 6.3 We’ll Have to Maintain the Tests . . . . . . . . . . . 127 6.4 Unit Tests Don’t Find All the Bugs . . . . . . . . . . 127 6.5 We Have a Long Build Time . . . . . . . . . . . . . . 128 6.6 We Have Existing Code . . . . . . . . . . . . . . . . . 128 6.7 We Have Constrained Memory . . . . . . . . . . . . . 129 6.8 We Have to Interact with Hardware . . . . . . . . . . 130 6.9 Why a C++ Test Harness for Testing C? . . . . . . . 131 6.10 Where Are We? . . . . . . . . . . . . . . . . . . . . . . 132 Reporterratum Download from Wow! eBook <www.wowebook.com> thiscopyis(P1.0printing,April,2011) CONTENTS 10 II Testing Modules with Collaborators 133 7 Introducing Test Doubles 134 7.1 Collaborators. . . . . . . . . . . . . . . . . . . . . . . 134 7.2 Breaking Dependencies. . . . . . . . . . . . . . . . . 135 7.3 When to Use a Test Double . . . . . . . . . . . . . . 139 7.4 Faking It in C, What’s Next . . . . . . . . . . . . . . 140 7.5 Where Are We? . . . . . . . . . . . . . . . . . . . . . . 144 8 Spying on the Production Code 145 8.1 Light Scheduler Test List . . . . . . . . . . . . . . . . 147 8.2 Dependencies on Hardware and OS . . . . . . . . . 147 8.3 Link-Time Substitution . . . . . . . . . . . . . . . . . 148 8.4 Spying on the Code Under Test . . . . . . . . . . . . 149 8.5 Controlling the Clock . . . . . . . . . . . . . . . . . . 154 8.6 Make It Work for None, Then One . . . . . . . . . . . 155 8.7 Make It Work for Many . . . . . . . . . . . . . . . . . 170 8.8 Where Are We? . . . . . . . . . . . . . . . . . . . . . . 175 9 Runtime-Bound Test Doubles 177 9.1 Testing Randomness . . . . . . . . . . . . . . . . . . 177 9.2 Faking with a Function Pointer . . . . . . . . . . . . 179 9.3 Surgically Inserted Spy . . . . . . . . . . . . . . . . . 182 9.4 Verifying Output with a Spy . . . . . . . . . . . . . . 186 9.5 Where Are We? . . . . . . . . . . . . . . . . . . . . . . 191 10 The Mock Object 193 10.1 Flash Driver . . . . . . . . . . . . . . . . . . . . . . . 194 10.2 MockIO . . . . . . . . . . . . . . . . . . . . . . . . . . 202 10.3 Test-Driving the Driver . . . . . . . . . . . . . . . . . 205 10.4 Simulating a Device Timeout . . . . . . . . . . . . . 208 10.5 Is It Worth It? . . . . . . . . . . . . . . . . . . . . . . 211 10.6 Mocking with CppUMock . . . . . . . . . . . . . . . . 212 10.7 Generating Mocks . . . . . . . . . . . . . . . . . . . . 214 10.8 Where Are We? . . . . . . . . . . . . . . . . . . . . . . 216 Reporterratum Download from Wow! eBook <www.wowebook.com> thiscopyis(P1.0printing,April,2011)
Description: