Python Testing Cookbook Over 70 simple but incredibly effective recipes for taking control of automated testing using powerful Python testing tools Greg L. Turnquist BIRMINGHAM - MUMBAI Python Testing Cookbook Copyright © 2011 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: May 2011 Production Reference: 1100511 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849514-66-8 www.packtpub.com Cover Image by Asher Wishkerman ([email protected]) Credits Author Project Coordinator Greg L. Turnquist Srimoyee Ghoshal Reviewers Proofreader Matthew Closson Bernadette Watkins Chetan Giridhar Indexer Sylvain Hellegouarch Hemangini Bari Maurice HT Ling Production Coordinator Acquisition Editor Adline Swetha Jesuthas Tarun Singh Cover Work Development Editor Adline Swetha Jesuthas Hyacintha D'Souza Technical Editors Pallavi Kachare Shreerang Deshpande Copy Editor Laxmi Subramanian About the Author Greg L. Turnquist has worked in the software industry since 1997. He is an active participant in the open source community, and has contributed patches to several projects including MythTV, Spring Security, MediaWiki, and the TestNG Eclipse plugin. As a test-bitten script junky, he has always sought the right tool for the job. He is a firm believer in agile practices and automated testing. He has developed distributed systems, LAMP-based setups, and supported mission-critical systems hosted on various platforms. After graduating from Auburn University with a Master's in Computer Engineering, Greg started working with Harris Corporation. He worked on many contracts utilizing many types of technology. In 2006, he created the Spring Python project and went on to write Spring Python 1.1 in 2010. He joined SpringSource, a division of VMware in 2010, as part of their international software development team. I would like to extend my thanks to Sylvain Hellegouarch, Matt Closson, as well as my editors, for taking the time to technically review this book and provide valuable feedback. I thank my one-year-old daughter for pulling me away when I needed a break and my one-month-old son for giving me MANY opportunities in the middle of the night to work on this book. I especially thank my precious wife Sara for the support, encouragement, patience, and most importantly for saying "I think we should strike while the iron is hot" when I was offered this writing opportunity. About the Reviewers Matthew Closson is a creative technologist and entrepreneur at heart. He is currently employed as a software engineer by Philips Healthcare. He is passionate about software testing, systems integration, and web technologies. When not obsessing over Ruby and C# code, this elusive developer is likely to be found reading at the local bookstore or relaxing on the beach. Chetan Giridhar has more than five years experience of working in the software services industry, product companies, and research organizations. He has a string background of C/C++, Java (certified Java professional) and has a good command of Perl, Python scripting languages, using which he has developed useful tools and automation frameworks. His articles on Code Reviews, Software Automation, and Agile methodologies have been published in international magazines including TestingExperience and AgileRecord for which he has received appreciation from other industry experts on his website—TechnoBeans. Chetan has also co-authored a book on Design Patterns in Python that is listed at Python's Official Website. He has given lectures on Python Programming to software professionals and at educational institutes including the Indian Institute of Astrophysics, Bangalore. Chetan holds a B.E. in Electrical Engineering from the University of Mumbai and feels that the world is full of knowledge. I take this opportunity to thank Rahul Verma, who has guided and inspired me, Ashok Mallya and Rishi Ranjan, for their encouragement and for the confidence they have shown in me. Special thanks to my parents Jayant and Jyotsana Giridhar, and my wife Deepti, who have all been a constant support. Sylvain Hellegouarch is a senior software engineer with several years experience in development and performance testing in various companies, both in France and in the United Kingdom. Passionate about open-source software, he has written several Python projects around communication protocols such as HTTP, XMPP, and the Atom Publishing Protocol. He has been part of the CherryPy team since 2004 and has also authored the CherryPy Essentials book, published by Packt Publishing in 2007. Sylvain also reviewed Spring Python, published by Packt Publishing in 2010. His current interests are set on the open-data movement and the wave of innovation it brings to public services. When away from his computer, Sylvain plays the guitar and the drums or spends his time with friends and family. Maurice HT Ling completed his Ph.D. in Bioinformatics and B.Sc(Hons) in Molecular and Cell Biology from The University of Melbourne where he worked on microarray analysis and text mining for protein-protein interactions. He is currently a Senior Scientist (Bioinformatics) in Life Technologies and an Honorary Fellow in The University of Melbourne, Australia. Maurice holds several Chief Editorships including The Python Papers, Computational and Mathematical Biology, and Methods and Cases in Computational, Mathematical, and Statistical Biology. In Singapore, he co-founded the Python User Group (Singapore) and has been the co-chair of PyCon Asia-Pacific since 2010. In his free time, Maurice likes to train in the gym, read, and enjoy a good cup of coffee. He is also a Senior Fellow of the International Fitness Association, USA. His personal website is: http://maurice.vodien.com. www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. 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 [email protected] 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. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On demand and accessible via 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 nine entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface 1 Chapter 1: Using Unittest To Develop Basic Tests 5 Introduction 5 Asserting the basics 7 Setting up and tearing down a test harness 11 Running test cases from the command line with increased verbosity 14 Running a subset of test case methods 16 Chaining together a suite of tests 18 Defining test suites inside the test module 21 Retooling old test code to run inside unittest 25 Breaking down obscure tests into simple ones 29 Testing the edges 35 Testing corner cases by iteration 39 Chapter 2: Running Automated Test Suites with Nose 45 Introduction 45 Getting nosy with testing 46 Embedding nose inside Python 49 Writing a nose extension to pick tests based on regular expressions 52 Writing a nose extension to generate a CSV report 59 Writing a project-level script that lets you run different test suites 66 Chapter 3: Creating Testable Documentation with doctest 77 Introduction 77 Documenting the basics 78 Catching stack traces 82 Running doctests from the command line 85 Coding a test harness for doctest 88 Filtering out test noise 92