Test-Driven Development with Django Develop powerful, fully-featured Django applications by writing tests first Kevin Harvey BIRMINGHAM - MUMBAI Test-Driven Development with Django 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: July 2015 Production reference: 1230715 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78528-116-7 www.packtpub.com Credits Author Copy Editor Kevin Harvey Yesha Gangani Reviewers Project Coordinator Ian Cordasco Milton Dsouza Anamta Farook Jason Myers Proofreader Safis Editing Vimal Atreya Ramaka Yogendra Sharma Indexer Rekha Nair Commissioning Editor Ashwin Nair Production Coordinator Melwyn D'sa Acquisition Editor Shaon Basu Cover Work Melwyn D'sa Content Development Editor Susmita Sabat Technical Editors Prajakta Mhatre Manan Patel About the Author Kevin Harvey first fell in love with Django while living in Quelimane, Mozambique, in 2007. His professional interests include software quality, open source, and teaching. He continues to be amazed at the Python community's ability to turn a history major into a software engineer, a feat for which he will forever be indebted. When not writing unit tests, Kevin enjoys playing the bass (both electric and stand up), and cooking with entirely too much butter. He lives in Nashville, Tennessee, with his wife and their two sons. There are many without whom this book would not have been possible. My thanks go to: Michael Trachtman, for helping me hash out my thoughts on the subject over the last 3 years; Julie Barnick, for helping me apply theory to the real world; Nate Aune, for his continued guidance; John Roth, for his enthusiasm; Medora Willmore, for teaching me how to write; Joe Killian, for encouraging me to write; Eli Bortz, for his industry advice; Jason Myers, for his unlimited willingness to help others; and finally, my wife Lara, for providing useful feedback on a topic that she knows nothing about, and dealing with the twins while daddy worked on his book. About the Reviewers Ian Cordasco is a core developer of requests, Flake8, Twine, Betamax, Chardet, and github3.py, as well as a core reviewer and driver for multiple OpenStack projects. As a polyglot, Ian frequently borrows testing ideas from other languages to improve the quality of Python's testing ecosystem. Anamta Farook is a software developer with a unique background in education and design. She graduated from Brown University in 2014 and has been programming professionally for the past year. Anamta enjoys working at trailblazing startups; she is currently employed at Maxwell Health, and she previously worked at Coursolve. She has 10 years of experience in teaching and developing educational content. She is particularly passionate about opportunities at the nexus of education and computer science, such as reviewing this book! I'd like to thank my parents, who encouraged me to take on this challenge. It's been a fun experience and I've really enjoyed being the voice of the learner in this book. I am confident that our readers will find this book an easy, valuable guide to Test-Driven Development. Jason Myers works at Cisco as a software engineer, working on OpenStack. Prior to switching to development a few years ago, he spent 15 years as a systems architect, and has build data centers and cloud architectures for several of the largest tech companies, hospitals, stadiums, and telecomm providers. He's a passionate developer who regularly speaks at local and national events about technology. He's also the chair of the PyTennessee conference. He loves solving human problems, and has a side project, Sucratrend, which is devoted to helping diabetics manage their condition and improve their quality of life. He has used SQLAlchemy in web, data warehouse, and analytics applications, and is currently writing Essential SQLAlchemy, 2nd Edition. Vimal Atreya Ramaka has graduated from the University of Prince Edward Island in computer science and business administration, and after graduation, she has started working as the tech lead for Climate Research Lab at the University of Prince Edward Island. Her domain is located at http://vimal.ramaka.com. Yogendra Sharma works at Siemens Industry Software Pvt. Ltd as a product developer and lives in Pune. He is a gold medalist in post graduation diploma. The course that he has graduated in is advance computing from CDAC, and he has also completed his bachelor's of technology in computer science. He is basically an engineer by heart and a technical enthusiast by nature. He has vast experience in the fields of C++, Python, Django, Java, J2EE technologies, and Web App Security. Yogendra was also the technical reviewer of Mastering Python Design Patterns, published by Packt Publishing. You can find him on LinkedIn at http://in.linkedin.com/in/yogendra0sharma. You can also visit http://PyLabs.in. I would like to thank my father for allowing me to learn all that I did. I would also like to thank my friends for their support and encouragement. 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 [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. 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 9 entirely free books. Simply use your login credentials for immediate access. This book is dedicated to Dr. Eric-Jan Manders. Eric, thanks for showing me the path.