Karen M. Tracey
Benjamin A. Slavin
About the Author
Karen has a PhD in Electrical/Computer Engineering from the University of Notre Dame. Her research there focused on distributed operating systems, which led to work in an industry centered on communications protocols and middleware. Outside of work she has an interest in puzzles, which led her to take up crossword construction. She has published nearly 100 puzzles in the New York Times, the Los Angeles Times syndicate, the New York Sun, and USA Today. She amassed a database of thousands of puzzles to aid in constructing and cluing her own puzzles. The desire to put a web frontend on this database is what led her to Django. She was impressed by the framework and its community, and became an active core framework contributor. Karen is one of the most prolific posters on the django-users mailing list. Her experience in helping hundreds of people there guided her in choosing the best and most useful material to include in this book.
Many thanks to Steven Wilding and the entire Packt Publishing team for making this book possible.
Id also like to thank the Django community. The community is too large to name everyone individually, but Jacob Kaplan-Moss, Adrian Holovaty, Malcolm Tredinnick, and Russell Keith-Magee deserve special mention. I very much appreciate the tremendous amount of work you all have done to create an excellent framework and foster a helpful and welcoming community.
Finally thanks to my parents, brothers, and many friends who supported me throughout the writing process. Your encouraging words have been very helpful and much appreciated.
About the Reviewer
BEN SLAVIN is an entrepreneur, technology strategist, and developer, focused on high performance web applications. He has been using Django to build scalable, reliable websites and applications since 2006. As a Director of Technology and a CTO, Ben has successfully integrated Django into multiple businesses operations, reducing technology costs and improving productivity.
Residing in Washington, DC, Ben has built and operates the Heliograph Network, designed to improve the performance and reliability of web applications. You can find him online at http://benslavin.net.
In memory of Mello and Haley
Preface
Bugs are a time consuming burden during software development. Django's built-in test framework and debugging support help lessen this burden. This book will teach you quick and efficient techniques for using Django and Python tools to eradicate bugs and ensure your Django application works correctly.
This book will walk you step-by-step through the development of a complete sample Django application. You will learn how best to test and debug models, views, URL configuration, templates, and template tags. This book will help you integrate with and make use of the rich external environment of testing and debugging tools for Python and Django applications.
This book starts with a basic overview of testing. It will highlight areas to look out for while testing. You will learn about the different kinds of tests available, the pros and cons of each, and details of test extensions provided by Django that simplify the task of testing Django applications. You will see an illustration of how external tools that provide even more sophisticated testing features can be integrated into Django's framework.
On the debugging front, the book illustrates how to interpret the extensive debugging information provided by Django's debug error pages, and how to utilize logging and other external tools to learn what code is doing.
This book is a step-by-step guide to running tests using Django's test support and making best use of Django and Python debugging tools.
What this book covers
In , Django Testing Overview , we begin development of a sample Django survey application. The example tests automatically generated by Django are described and run. All of the options available for running tests are covered.
In , Does This Code Work? Doctests in Depth , the models used by the sample application are developed. Using doctests to test models is illustrated by example. The pros and cons of doctests are discussed. Specific caveats for using doctests with Django applications are presented.
In , Testing 1, 2, 3: Basic Unit Testing , the doctests implemented in the previous chapter are re-implemented as unit tests and assessed in light of the pros, cons, and caveats of doctests discussed in the previous chapter. Additional tests are developed that need to make use of test data. Using fixture files to load such data is demonstrated. In addition, some tests where fixture files are inappropriate for test data are developed.
In , Getting Fancier: Django Unit Test Extensions , we begin to write the views that serve up web pages for the application. The number of tests is starting to become significant, so this chapter begins by showing how to replace use of a single tests.py
file for tests with a tests directory, so that tests may be kept well-organized. Then, tests for views are developed that illustrate how unit test extensions provided by Django simplify the task of testing web applications. Testing form behavior is demonstrated by development of a test for an admin customization made in this chapter.
, Filling in the Blanks: Integrating Django and Other Test Tools , shows how Django supports integration of other test tools into its framework. Two examples are presented. The first illustrates how an add-on application can be used to generate test coverage information while the second demonstrates how use of the