1. Trang chủ
  2. » Công Nghệ Thông Tin

the cucumber book

328 650 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 328
Dung lượng 6,93 MB

Nội dung

www.it-ebooks.info What Readers Are Saying About The Cucumber Book Few tools have managed to bridge the developer-customer divide as well as Cucumber has. Cucumber is not a tool for testing applications. Cucumber is a philosophy for communicating requirements. This book brings that philosophy to life. ➤ Robert C. Martin (Uncle Bob) I devoured the Cucumber book on a train ride from Grenoble to Brussels a few days after watching Matt’s presentation “BDD As It’s Meant to Be Done.” These two resources helped me understand in just a few hours how to avoid dozens of common mistakes writing scenarios in the Cucumber style. It’s as though I received an injection of perhaps two years of experience writing scenarios poorly so that I didn’t have to go through it all myself. What a gift. I recommend this book to everyone working with Cucumber. ➤ J. B. Rainsberger Author, JUnit Recipes www.it-ebooks.info Teams can use Cucumber to get a better understanding of what software to build for their customers. In this book, Aslak and Matt do a brilliant job explaining how you get started with Cucumber with plenty of easy-to-follow examples. ➤ Rachel Davies Author, Agile Coaching To those of you wondering how to use Cucumber effectively, The Cucumber Book is the answer. Not content to write just a testing book, Aslak and Matt have packed it with practical insights on many aspects of software development. Studying this book will make you a better software developer. ➤ Pat Maddox, B.D.D.M.F. RSpec Core team This is a much-needed book, providing not only an expanded description of how to use Cucumber but an opinionated one to suggest how to use it for the best effect. Reading this book is like having Aslak and Matt sitting next to you, patiently helping you through your first project with Cucumber. Not only will you learn effective use of Cucumber, but you’ll also be introduced to several other Ruby tools that can be used with Cucumber. ➤ George Dinwiddie Software development coach at iDIA Computing, LLC www.it-ebooks.info Matt and Aslak show you how Cucumber can save you from stale documentation, unclear requirements, and absentee tests. By the end of the book, your team’s programmers, testers, and product owners will be talking excitedly about the next great product you’re going to build together. ➤ Ian Dees Author, Scripted GUI Testing with Ruby This book had me at “Cucumber is designed to help build bridges between the technical and nontechnical members of a software team.” Wynne and Hellesøy understand the whole-team approach to specification by example, with diverse team members collaborating to deliver what the customer really wants. They use examples to teach us how to automate regression checks with Cucumber, use it to build a safety net to allow refactoring, and free testers to contribute their most valuable skills to the team. ➤ Lisa Crispin Author, Agile Testing: A Practical Guide for Testers (with Janet Gregory) This book is a tale of how to do effective acceptance testing, with Cucumber as the filling in the sandwich. The authors don’t just scratch the surface; they get right under the skin and show us how versatile Cucumber can be. ➤ Robert Chatley Principal, Devlogical www.it-ebooks.info Lots of great tips for Cucumber newbies and experts alike—Matt and Aslak have done a great job of explaining everything from getting started to how to get the most out of Cucumber. You’ll want to read this book cover to cover and keep it close as a reference! ➤ Gojko Adzic Author, Specification by Example and Bridging the Communication Gap The Cucumber Book is a must-read for anyone thinking about using Cucumber; it is scattered with treasures for even the most experienced Cucumber users. ➤ Antony Marcano RiverGlide www.it-ebooks.info The Cucumber Book Behaviour-Driven Development for Testers and Developers Matt Wynne Aslak Hellesøy The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina www.it-ebooks.info 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 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 Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. 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 Pragmatic titles, please visit us at http://pragprog.com . The team that produced this book includes: Jackie Carter (editor) Potomac Indexing, LLC (indexer) Kim Wimpsett (copyeditor) David J Kelly (typesetter) Janet Furlow (producer) Juliet Benda (rights) Ellie Callahan (support) Copyright © 2012 Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted , in any fo rm , or by any means, electro ni c, mechanical, p ho to cop yin g, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-934356-80-7 Printed on acid-free paper. Book version: P1.0—January 2012 www.it-ebooks.info Contents Foreword . . . . . . . . . . . . . xiii Acknowledgments . . . . . . . . . . . xv Preface . . . . . . . . . . . . . . xvii Part I — Cucumber Fundamentals 1. Why Cucumber? . . . . . . . . . . . . 3 Automated Acceptance Tests 41.1 1.2 Behaviour-Driven Development 4 1.3 Living Documentation 6 1.4 How Cucumber Works 7 1.5 What We Just Learned 8 2. First Taste . . . . . . . . . . . . . 11 Understanding Our Goal 112.1 2.2 Creating a Feature 12 2.3 Creating Step Definitions 14 2.4 Implementing Our First Step Definition 16 2.5 Running Our Program 17 2.6 Changing Formatters 18 2.7 Adding an Assertion 19 2.8 Making It Pass 20 2.9 What We Just Learned 23 3. Gherkin Basics . . . . . . . . . . . . 25 What’s Gherkin For? 253.1 3.2 Format and Syntax 28 3.3 Feature 29 3.4 Scenario 30 3.5 Comments 33 www.it-ebooks.info 3.6 Spoken languages 34 3.7 What We Just Learned 35 4. Step Definitions: From the Outside . . . . . . . 39 Steps and Step Definitions 404.1 4.2 Capturing Arguments 45 4.3 Multiple Captures 49 4.4 Flexibility 50 4.5 Returning Results 52 4.6 What We Just Learned 58 5. Expressive Scenarios . . . . . . . . . . 61 Background 615.1 5.2 Data Tables 64 5.3 Scenario Outline 70 5.4 Nesting Steps 75 5.5 Doc Strings 78 5.6 Staying Organized with Tags and Subfolders 79 5.7 What We Just Learned 82 6. When Cucumbers Go Bad . . . . . . . . . 85 Feeling the Pain 866.1 6.2 Working Together 89 6.3 Caring for Your Tests 97 6.4 Stop the Line and Defect Prevention 105 6.5 What We Just Learned 106 Part II — A Worked Example 7. Step Definitions: On the Inside . . . . . . . . 111 Sketching Out the Domain Model 1127.1 7.2 Removing Duplication with Transforms 117 7.3 Adding Custom Helper Methods to the World 120 7.4 Organizing the Code 128 7.5 What We Just Learned 131 8. Support Code . . . . . . . . . . . . 133 Fixing the Bug 1348.1 8.2 Bootstrapping the User Interface 141 8.3 Making the Switch 142 8.4 Using Hooks 147 Contents • ix www.it-ebooks.info 8.5 Building the User Interface 150 8.6 What We Just Learned 154 9. Dealing with Message Queues and Asynchronous Components . . . . . . . . . . 157 9.1 Our New Asynchronous Architecture 157 9.2 How to Synchronize 158 9.3 Implementing the New Architecture 161 9.4 Fixing the Flickering Scenario 165 9.5 What We Just Learned 171 10. Databases . . . . . . . . . . . . . 173 Introducing ActiveRecord 17410.1 10.2 Refactoring to Use a Database 175 10.3 Reading and Writing to the Database 178 10.4 Cleaning the Database with Transactions 181 10.5 Cleaning the Database with Truncation 185 10.6 What We Just Learned 186 Part III — Cucumber Applied 11. The Cucumber Command-Line Interface . . . . . 191 Cucumber’s Command-Line Options 19111.1 11.2 Running a Subset of Scenarios 192 11.3 Changing Cucumber’s Output 194 11.4 Specifying the Location of Step Definitions 196 11.5 Managing Your Work in Progress (WIP) 197 11.6 Using Profiles 198 11.7 Running Cucumber from Rake 198 11.8 Running Cucumber in Continuous Integration 199 11.9 What We Just Learned 200 12. Testing a REST Web Service . . . . . . . . 201 12.1 In-Process Testing of Rack-Based REST APIs 202 12.2 Out-of-Process Testing of Any REST API 213 12.3 What We Just Learned 220 13. Adding Tests to a Legacy Application . . . . . . 221 Characterization Tests 22213.1 13.2 Squashing Bugs 224 13.3 Adding New Behavior 225 Contents • x www.it-ebooks.info [...]... definition executes without error, Cucumber proceeds to the next step in the scenario If it gets to the end of the scenario without any of the steps raising an error, it marks the scenario as having passed If any of the steps in the scenario fail, however, Cucumber marks the scenario as having failed and moves on to the next one As the scenarios run, Cucumber prints out the results showing you exactly... compliment our book Running the Code Examples This book is full of practical examples, and we encourage you to follow along with them to get the most out of the book You’ll learn the most if you type them in by hand as you read along, but if you’d prefer, you can always download the code examples from http://pragprog.com/titles/hwcuc/ source_code To run the examples, you’ll need to install the Ruby language... You can find the full instructions in Appendix 2, Installing Cucumber, on page 299 Windows Users Most of the code examples work just the same on Windows and *nix operating systems On the rare occasions that they differ, you’ll find the Windows version in a sidebar nearby, with a note in the body of the text pointing you to the sidebar You’ll soon notice that we’ve used the $ symbol for the command prompt... and the chances of misunderstandings are greatly reduced Cucumber helps facilitate the discovery and use of a ubiquitous language within the team, by giving the two sides of the linguistic divide a place where they can meet Cucumber tests interact directly with the developers’ code, but they’re written in a medium and language that business stakeholders can understand By working together to write these... www.it-ebooks.info report erratum • discuss Acknowledgments The first people we want to thank are the hundreds of you who contribute to the Cucumber community Whether you’re sharing ideas, experiences, and opinions on the mailing list, helping people in the IRC channel, or contributing new features and bug fixes to the codebase, it all helps Without your contribution, there would be no Cucumber and therefore... automated acceptance tests When the acceptance tests are written as examples, they stimulate people’s imaginations and help them see other scenarios they hadn’t previously considered When the team write their acceptance tests collaboratively, they can develop their own ubiquitous language for talking about their problem domain This helps them avoid misunderstandings www.it-ebooks.info report erratum •... parts of the team no longer have their own personal versions of the truth 1.4 How Cucumber Works Before we dive into the meat of the book, let’s give you some context with a high-level overview of a typical Cucumber test suite Cucumber is a command-line tool When you run it, it reads in your specifications from plain-language text files called features, examines them for scenarios to test, and runs the. .. First Taste So, for example, if the input file looks like this: 2+2 then the output would be 4 Similarly, if the input file looks like this: 100/2 then the output would be 50 You get the idea 2.2 Creating a Feature Cucumber tests are grouped into features We use this name because we want them to describe the features that a user will be able to enjoy when using our program The first thing we need to do... numbers Given the input "2+2" When the calculator is run Then the output should be "4" This feature file contains the first scenario for our calculator program We’ve translated one of the examples we were given in the previous section into a Cucumber scenario that we can ask the computer to run over and over again You can probably see that Cucumber expects a little bit of structure in this file The keywords... When, and Then are the structure, and everything else is documentation Although some of the keywords are highlighted here in the book and they may be in your editor too—it’s just a plain-text file The structure is called Gherkin When you save this file and run cucumber, you should see a great deal more output than the last time: $ cucumber Feature: Adding Scenario: Add two numbers Given the input "2+2" . our book. Running the Code Examples This book is full of practical examples, and we encourage you to follow along with them to get the most out of the book. You’ll learn the most if you type them. Gregory) This book is a tale of how to do effective acceptance testing, with Cucumber as the filling in the sandwich. The authors don’t just scratch the surface; they get right under the skin and. discuss www.it-ebooks.info Acknowledgments The first people we want to thank are the hundreds of you who contribute to the Cucumber community. Whether you’re sharing ideas, experiences, and opinions on the

Ngày đăng: 24/04/2014, 16:09

Xem thêm

TỪ KHÓA LIÊN QUAN