Python Testing Beginner's Guide An easy and convenient approach to testing your Python projects Daniel Arbuckle BIRMINGHAM - MUMBAI This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 Python Testing Beginner's Guide Copyright © 2010 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: January 2010 Production Reference: 1120110 Published by Packt Publishing Ltd 32 Lincoln Road Olton Birmingham, B27 6PA, UK ISBN 978-1-847198-84-6 www.packtpub.com Cover Image by Vinayak Chittar (vinayak.chittar@gmail.com) This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 Credits Author Daniel Arbuckle Reviewers Róman Joost Production Editorial Manager Abhijeet Deobhakta Editorial Team Leader Aanchal Kumar Andrew Nicholson Herjend Teny Project Team Leader Lata Basantani Acquisition Editor Douglas Paterson Project Coordinator Srimoyee Ghoshal Development Editor Ved Prakash Jha Graphics Geetanjali Sawant Technical Editors Aditya Belpathak Charumathi Sankaran Indexer Monica Ajmera Mehta Production Coordinator Shantanu Zagade Cover Work Shantanu Zagade Proofreader Lesley Harrison This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 About the Author Daniel Arbuckle received his Ph D in computer science from the University of Southern California in 2007 He is an active member of the Python community and an avid unit tester I would like to thank Grig, Titus, and my family for their companionship and encouragement along the way This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 About the Reviewers Róman Joost is a professional Python software developer and a free software enthusiast, currently living in Australia Since 2003, he has been contributing to the GNU Image Manipulation Program (GIMP) by writing documentation and contributing to the source code He uses testing frameworks and test-driven methodologies extensively, when writing new components for the Z Object Publishing Environment (Zope) in Python Andrew Nicholson is a software engineer with over 12 years of professional commercial experience in a broad range of technologies He is passionate about free and open source software (FOSS) and has actively participated in contributing code, ideas, and passion in the open source community since 1999 Nicholson's biography can be read at http://infiniterecursion.com.au/people/ Herjend Teny is an electrical engineering graduate from Melbourne who has come to love programming in Python after years of programming in mainline programming languages, such as C, Java, and Pascal He is currently involved in designing web application using Django for an Article Repository project on http://www.havingfunwithlinux.com/ The project would allow users to post their article for public view and bookmark it onto their favorite blog This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 Table of Contents Preface Chapter 1: Testing for Fun and Profit How can testing help? Types of testing Unit testing Integration testing System testing You've got Python, right? Summary Chapter 2: Doctest: The Easiest Testing Tool Basic doctest Time for action – creating and running your first doctest The syntax of doctests Time for action – writing a more complex test Expecting exceptions Time for action – expecting an exception Expecting blank lines in the output Using directives to control doctest Ignoring part of the result Time for action – using ellipsis in tests Ignoring whitespace Time for action – normalizing whitespace 9 9 10 10 11 11 12 13 14 15 16 17 17 17 17 18 19 Skipping an example entirely 19 Time for action – skipping tests 20 Other doctest directives Execution scope Embedding doctests in Python docstrings Time for action – embedding a doctest in a docstring Doctest directives 21 21 24 24 25 This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 Table of Contents Execution scope Putting it in practice: an AVL tree English specification Node data Constructor Recalculate height Make deletable Rotation Locating a node Testing the rest of the specification Summary 26 26 27 28 30 30 32 33 34 34 35 Chapter 3: Unit Testing with Doctest 37 What is Unit testing and what it is not? Time for action – identifying units Unit testing throughout the development process Design phase Time for action – unit testing during design Development phase Time for action – unit testing during development Feedback phase Time for action – unit testing during feedback Back to the development phase Time for action – unit testing during development again Maintenance phase Time for action – unit testing during maintenance Reuse phase Time for action – unit testing during reuse Summary Chapter 4: Breaking Tight Coupling by using Mock Objects Installing Python Mocker Time for action – installing Python Mocker The idea of a mock object Python Mocker Time for action – exploring the basics of Mocker Mocking functions Mocking containers Parameter matching ANY ARGS KWARGS IS 37 38 40 41 41 44 44 47 47 51 51 53 53 55 55 59 61 61 62 62 63 63 67 68 69 69 70 70 71 [ ii ] This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 Table of Contents IN CONTAINS MATCH 71 72 72 Mocking complex expressions Returning iterators Raising exceptions Calling functions via a mock Specifying that an expectation should occur multiple times Replacing library objects with mocks Mocking self Time for action – passing a mock object as self Summary Chapter 5: When Doctest isn't Enough: Unittest to the Rescue Basic unittest Time for action – testing PID with unittest Assertions assertTrue assertFalse assertEqual assertNotEqual assertAlmostEqual assertNotAlmostEqual assertRaises fail 73 73 74 74 75 77 80 80 82 83 83 84 89 89 90 90 90 90 92 92 93 Test fixtures Time for action – testing database-backed units Integrating with Python Mocker Summary Chapter 6: Running Your Tests: Follow Your Nose What is Nose? Installing Nose Organizing tests Time for action – organizing tests from previous chapters Finding doctests Customizing Nose's search Nose and doctest Time for action – creating a fixture for a doctest Nose and unittest Time for action – creating a module fixture Time for action – creating a package fixture Nose's own testing framework 94 95 100 100 101 101 102 103 104 108 109 110 111 112 113 114 116 [ iii ] This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 Table of Contents Time for action – using Nose-specific tests Summary Chapter 7: Developing a Test-Driven Project Writing the specification Time for action – what are you going to do? Writing initial unit tests Time for action – nailing down the specification with unit tests Coding planner.data Using the tests to get the code right Fixing the code Time for action – writing and debugging code Writing persistence tests Writing persistence code Finishing up Summary Chapter 8: Testing Web Application Frontends using Twill Installing Twill Exploring the Twill language Time for action – browsing the web with Twill Time for action – Twill scripting Twill commands help setglobal setlocal add_auth add_extra_header clear_extra_headers show_extra_headers agent back clear_cookies code config debug echo exit extend_with find notfind follow formaction formclear formfile 116 118 119 119 125 125 139 139 143 143 146 147 148 151 153 155 155 156 156 159 160 160 160 161 161 161 162 162 162 162 162 162 163 163 163 163 164 164 164 164 164 165 165 [ iv ] This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st February 2010 2601 S Broadway St, Unit 29, La Porte, , 77571 ... Chapter 1: Testing for Fun and Profit How can testing help? Types of testing Unit testing Integration testing System testing You've got Python, right? Summary Chapter 2: Doctest: The Easiest Testing. .. 6PA, UK ISBN 97 8-1 -8 4719 8-8 4-6 www.packtpub.com Cover Image by Vinayak Chittar (vinayak.chittar@gmail.com) This material is copyright and is licensed for the sole use by Betty Vaughan-Pope on 1st... What this book covers Chapter 1: Testing for Fun and Profit introduces Python test-driven development and various testing methods Chapter 2: Doctest: The Easiest Testing Tool covers the doctest