[1] Test-Driven Python Development Develop high-quality and maintainable Python applications using the principles of test-driven development Siddharta Govindaraj BIRMINGHAM - MUMBAI Test-Driven Python Development Copyright © 2015 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information First published: April 2015 Production reference: 1210415 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78398-792-4 www.packtpub.com Cover image by Jayanand Govindaraj (jayanand@gmail.com) Credits Author Siddharta Govindaraj Copy Editors Vikrant Phadke Sameen Siddiqui Reviewers Sharad Bagri Kevin Davenport Vivek Vidyasagaran Alpha Singh Project Coordinator Danuta Jones Devesh Chanchlani Dorai Thodla Kiran Gangadharan Cees de Groot David Simick Fernando Ramos Christopher Humphries Commissioning Editor Julian Ursell Acquisition Editor Nikhil Karkal Content Development Editor Sumeet Sawant Technical Editor Ruchi Desai Proofreaders Simran Bhogal Stephen Copestake Safis Editing Paul Hindle Indexer Mariammal Chettiyar Graphics Jason Monteiro Production Coordinator Arvindkumar Gupta Cover Work Arvindkumar Gupta About the Author Siddharta Govindaraj first encountered Python in 2002, and it has remained his favorite programming language ever since He was an early participant in BangPypers, India's first Python community, and a speaker at InPycon He is the founder of Silver Stripe Software, an Indian product start-up that develops products based on Python A vocal proponent of the language, Siddharta extols the virtues of Python to anyone who will listen—and also to those who won't His blog is at http://www.siddharta.me Acknowledgments Abhaya Shenoy, my colleague at Innvo Systems, introduced me to Python back in 2002 I spent a month working with Python, and then I was hooked to it for life I was also introduced to test-driven development in 2002 Thanks to Moses Hohman, who worked for ThoughtWorks at that time I would like to thank all the reviewers for their participation: Sharad Bagri, Kevin Davenport, Vivek Vidyasagaran, Devesh Chanchlani, Dorai Thodla, Kiran Gangadharan, Cees de Groot, David Simick, Fernando Ramos, and Christopher Humphries Devesh Chanchlani and Dorai Thodla spent a number of hours reviewing the book with me and offering valuable suggestions Sharad Bagri, Kevin Davenport, and Vivek Vidyasagaran also provided a ton of feedback that helped the book become what it is Mohammad Fahad and Ruchi Desai from Packt Publishing did a great job coordinating with a number of people and getting this book in shape A special shout-out to the Python community! One of the reasons I love Python is not only the language but also the awesome community around it Finally, I would like to thank my family This book could not have been completed without their constant encouragement, motivation, and support About the Reviewers Sharad Bagri is a passionate software developer, adventurer, and an inquisitive learner His first date with Python happened during the New Year break of 2013, and he fell in love with it Ever since then, he can be seen cajoling his fellow programmers to try Python He is a seasoned programmer and has worked with various languages and technologies Sharad secured his MS degree in computer engineering from Virginia Tech, USA His research focused on verification of electronic circuits, which resulted in several papers published in peer-reviewed international conferences Before going to Virginia Tech, he worked at Qualcomm and ABB in the embedded software and hardware development departments He completed his bachelor's degree in electronics and communication engineering (ECE) from National Institute of Technology (NIT), Nagpur, India, where he was involved in diverse projects related to robotics, programming, and embedded systems development His interests also include reading, running, cycling, and filmmaking Kevin Davenport is a senior technical program manager at Amazon.com, working on scalable solutions for offer recommendations and comparisons He has 10 years of experience in statistical and machine learning implementations, systems engineering, and product management Other than work, Kevin enjoys sharing his ideas on his blog (http://kldavenport.com/) and volunteering for Washington's Evergreen Mountain Bike Alliance I would like to thank the author of this book for articulating a compelling case for test-driven development and the use of Python Many thanks to Packt Publishing for the opportunity to contribute to this publication and for their contributions to the open source world Vivek Vidyasagaran is pursuing his master's degree at Carnegie Mellon University's Entertainment Technology Centre, where he works on developing the next generation of digital entertainment He completed his bachelor's degree in computer science and engineering from VIT University, Vellore, India Currently, he works on game design and development Python has been an important tool in his development toolbox and he has been using it to create games and apps since 2010 Vivek is also very interested in the core technologies behind modern entertainment, such as computer graphics, GPU computing, and game engines www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy Get in touch with us at service@packtpub.com for more details At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library Here, you can search, access, and read Packt's entire library of books Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view entirely free books Simply use your login credentials for immediate access Table of Contents Preface vii Chapter 1: Getting Started with Test-Driven Development Prerequisites 2 Understanding test-driven development TDD versus unit testing versus integration testing Using TDD to build a stock alert application Writing our first test Analyzing the test output 10 Test errors versus test failures 11 Making the test pass 11 Reorganizing the test code 12 Running the tests after the reorganization 14 Summary 16 Chapter 2: Red-Green-Refactor – The TDD Cycle 17 Tests are executable requirements 17 Arrange-Act-Assert 18 Documenting our tests 19 Testing for exceptions 20 Exploring assert methods 21 Specific asserts versus generic asserts 24 Setup and teardown 25 Brittle tests 28 Refactoring the design 30 Refactoring tests 32 Exploring the Rule classes 34 Exercise 38 Summary 39 [i] .. .Test-Driven Python Development Develop high-quality and maintainable Python applications using the principles of test-driven development Siddharta Govindaraj BIRMINGHAM - MUMBAI Test-Driven Python. .. Innvo Systems, introduced me to Python back in 2002 I spent a month working with Python, and then I was hooked to it for life I was also introduced to test-driven development in 2002 Thanks to... Table of Contents Preface vii Chapter 1: Getting Started with Test-Driven Development Prerequisites 2 Understanding test-driven development TDD versus unit testing versus integration testing