Django 1.1 Testing and Debugging - Karen M. Tracey - E-Book

Django 1.1 Testing and Debugging E-Book

Karen M. Tracey

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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 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 test and debugging tools for Python and Django applications. The book starts with a basic overview of testing. It will highlight areas to look out for while testing. You will learn about different kinds of tests available, and the pros and cons of each, and also 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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 579

Veröffentlichungsjahr: 2010

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Django 1.1 Testing and Debugging
Credits
About the Author
About the Reviewer
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Errata
Piracy
Questions
1. Django Testing Overview
Getting started: Creating a new application
Understanding the sample unit test
Understanding the sample doctest
Running the sample tests
Breaking things on purpose
Test errors versus test failures
Command line options for running tests
Verbosity
Settings
Pythonpath
Traceback
Noinput
Version
Summary
2. Does This Code Work? Doctests in Depth
The Survey application models
Testing the Survey model
Testing Survey model creation
Is that test useful?
Developing a custom Survey save method
Deciding what to test
Some pros and cons of doctests so far
Additional doctest caveats
Beware of environmental dependence
Beware of database dependence
Beware of test interdependence
Beware of Unicode
Summary
3. Testing 1, 2, 3: Basic Unit Testing
Unit tests for the Survey save override method
Pros of the unit test version
Cons of the unit test version
Revisiting the doctest caveats
Environmental dependence
Database dependence
Test interdependence
Unicode
Providing data for unit tests
Providing data in test fixtures
Example test that needs test data
Using the admin application to create test data
Writing the function itself
Writing a test that uses the test data
Extracting the test data from the database
Getting the test data loaded during the test run
Creating data during test set up
Summary
4. Getting Fancier: Django Unit Test Extensions
Organizing tests
Creating the survey application home page
Defining the survey application URLs
Developing views to serve pages
Creating templates for pages
Testing the survey home page
Creating the survey detail pages
Refining the survey detail view
Templates for the survey detail pages
Basic testing of the survey detail pages
Customizing the admin add and change survey pages
Developing a custom survey form
Configuring admin to use the custom form
Testing the admin customization
Additional test support
Supporting additional HTTP methods
Maintaining persistent state
E-mail services
Providing test-specific URL configuration
Response context and template information
Testing transactional behavior
Summary
5. Filling in the Blanks: Integrating Django and Other Test Tools
Problems of integration
Specifying an alternative test runner
Creating a new management command
How much of the code are we testing?
Using coverage standalone
Integrating coverage into a Django project
The twill web browsing and testing tool
Using the twill command line program
Using twill in a TestCase
Summary
6. Django Debugging Overview
Django debug settings
The DEBUG and TEMPLATE_DEBUG settings
The TEMPLATE_STRING_IF_INVALID setting
Debug error pages
Database query history
Debug support in the development server
Handling problems in production
Creating general error pages
Reporting production error information
Internal server error notifications
Page not found notifications
Summary
7. When the Wheels Fall Off: Understanding a Django Debug Page
Starting the Survey voting implementation
Creating test data for voting
Defining a question form for voting
Debug page #1: TypeError at /
Elements of the debug page
Basic error information
Traceback
Request information
GET
POST
FILES
COOKIES
META
Settings
Understanding and fixing the TypeError
Handling multiple Survey questions
Creating the data for multiple questions
Coding support for multiple questions
Debug page #2: TemplateSyntaxError at /1/
Understanding and fixing the TemplateSyntaxError
Recording Survey responses
Coding support for recording Survey responses
Debug page #3: NoReverseMatch at /1/
Understanding and fixing the NoReverseMatch exception
Debug page #4: TemplateDoesNotExist at /thanks/1/
Understanding and fixing TemplateDoesNotExist
Handling invalid Survey submissions
Coding custom error message and placement
Debug page #5: Another TemplateSyntaxError
Fixing the second TemplateSyntaxError
Summary
8. When Problems Hide: Getting More Information
Tracking SQL queries for a request
Settings for accessing query history in templates
SQL queries for the home page
Packaging the template query display for reuse
Testing the repackaged template code
SQL queries for the active Survey form display page
SQL queries for posting survey answers
The Django Debug Toolbar
Installing the Django Debug Toolbar
Debug toolbar appearance
The SQL panel
The Time panel
The Settings panel
The HTTP Headers panel
The Request Vars panel
The Templates panel
The Signals panel
The Logging panel
Redirect handling by the debug toolbar
Tracking internal code state
Resist the urge to sprinkle prints
Simple logging configuring for development
Deciding what to log
Decorators to log function entry and exit
Applying the decorators to the Survey code
Logging in the debug toolbar
Summary
9. When You Don't Even Know What to Log: Using Debuggers
Implementing the Survey results display
Results display using pygooglechart
Getting started with the debugger
The list command
The where command
The args command
The whatis command
The print and pp commands
Debugging the pygooglechart results display
The step and next commands
The continue command
The jump command
The break command
The clear command
Fixing the pygooglechart results display
The up and down commands
The return command
Results display using matplotlib
Improving the matplotlib approach
Setting up static file serving
Dynamically generating image files
Dealing with race conditions
Using the debugger to force race situations
Notes on using graphical debuggers
Summary
10. When All Else Fails: Getting Outside Help
Tracking down a problem in Django
Revisiting the Chapter 7 voting form
Is the right code actually running?
Is the code correct as per the documentation?
Searching for a matching problem report
Another way to search for a matching problem report
Determining the release that contains a fix
What if a fix hasn't been released yet?
What if a fix hasn't been committed yet?
What if a ticket has been closed without a fix?
Tracking down unreported problems
Where to ask questions
Tips on asking questions that will get good answers
Opening a new ticket to report a problem
Summary
11. When it's Time to Go Live: Moving to Production
Developing an Apache/mod_wsgi configuration
Creating the WSGI script for the marketr project
Creating an Apache VirtualHost for the marketr project
Activating the new Apache configuration
Debugging the new Apache configuration
Configuring Apache to serve static files
Testing multithreaded behavior
Generating load with siege
Load testing the results recording code
Fixing the results recording code
Additional load testing notes
Using Apache/mod_wsgi during development
Summary
Index

Django 1.1 Testing and Debugging

Karen M. Tracey

Django 1.1 Testing and Debugging

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: April 2010

Production Reference: 1120410

Published by Packt Publishing Ltd.

32 Lincoln Road

Olton

Birmingham, B27 6PA, UK.

ISBN 978-1-847197-56-6

www.packtpub.com

Cover Image by Raj Kataria (<[email protected]>)

Credits

Author

Karen M. Tracey

Reviewer

Benjamin A. Slavin

Acquisition Editor

Steven Wilding

Development Editor

Neha Patwari

Technical Editor

Conrad Sardinha

Indexers

Hemangini Bari

Rekha Nair

Editorial Team Leader

Mithun Sehgal

Project Team Leader

Priya Mukherji

Project Coordinator

Leena Purkait

Proofreader

Aaron Nash

Production Coordinator

Shantanu Zagade

Cover Work

Shantanu Zagade

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.

I’d 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 Chapter 1, 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 Chapter 2, 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 Chapter 3, 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 Chapter 4, 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.

Chapter 5, 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 twill test tool (which allows for much easier testing of form behavior) can be integrated into Django application tests.

Chapter 6, Django Debugging Overview, provides an introduction to the topic of debugging Django applications. All of the settings relevant for debugging are described. Debug error pages are introduced. The database query history maintained by Django when debugging is turned on is described, as well as features of the development server that aid in debugging. Finally, the handling of errors that occur during production (when debug is off) is detailed, and all the settings necessary to ensure that information about such errors is captured and sent to the appropriate people are mentioned.

In Chapter 7, When the Wheels Fall Off: Understanding a Django Debug Page, development of the sample application continues, making some typical mistakes along the way. These mistakes result in Django debug pages. All of the information available on these pages is described, and guidance on what pieces are likely most helpful to look at in what situations is given. Several different kinds of debug pages are encountered and discussed in depth.

Chapter 8, When Problems Hide: Getting More Information, focuses on how to get more information about how code is behaving in cases where a problem doesn't result in a debug error page. It walks through the development of a template tag to embed the query history for a view in the rendered page, and then shows how the Django debug toolbar can be used to get the same information, in addition to much more. Finally, some logging utilities are developed.

Chapter 9, When You Don't Even Know What to Log: Using Debuggers, walks through examples of using the Python debugger (pdb) to track down what is going wrong in cases where no debug page appears and even logging isn't helpful. All of the most useful pdb commands are illustrated by example. In addition, we see how pdb can be used to ensure correct code behavior for code that is subject to multi-process race conditions.

Chapter 10, When All Else Fails: Getting Outside Help, describes what to do when none of the techniques covered so far have solved a problem. Possibly, it is a bug in external code: tips are given on how to search to see if others have experienced the same and if there are any fixes available. Possibly it's a bug in our code or a misunderstanding about how some things work; avenues for asking questions and tips on writing good questions are included.

In Chapter 11, When it's Time to Go Live: Moving to Production, we move the sample application into production, using Apache and mod_wsgi instead of the development server. Several of the most common problems encountered during this step are covered. In addition, the option of using Apache with mod_wsgi during development is discussed.

What you need for this book

You will need a computer running a Django 1.1 release—the latest 1.1.X release is recommended. You will also need an editor to edit code files and a web browser. You may choose to use whatever operating system, editing, and browsing tools you are most comfortable with, so long as you choose an operating system that can run Django. For more information on Django's requirements, consult http://docs.djangoproject.com/en/1.1/intro/install/.

For your reference, the example console output and screenshots in this book are all taken from a machine running:

Ubuntu 8.10Python 2.5.2Django 1.1 (early in the book) and 1.1.1 (later in the book)Firefox 3.5.7

You can use any database supported by Django. For illustration purposes, different databases (SQLite, MySQL, PostgreSQL) are used at different points in the book. You will likely prefer to choose one to use throughout.

Additional software is used at specific points in the book. Wherever a software package is introduced, notes on where to obtain it for installation are included. For your reference, the following is a list of additional software packages and the versions used in the book:

Chapter 5, Filling in the Blanks: Integrating Django and Other Test Tools, uses:
coverage 3.2django_coverage 1.0.1twill 0.9 (and latest development level)
Chapter 8, When Problems Hide: Getting More Information, uses:
django-debug-toolbar 0.8.0
Chapter 9, When You Don't Even Know What to Log: Using Debuggers, uses:
pygooglechart 0.2.0matplotlib 0.98.3
Chapter 11, When it's Time to Go Live: Moving to Production, uses:
Apache 2.2mod_wsgi 2.3siege 2.6.6

Note that you do not need to have any of these additional packages installed when you start working through this book, they can each be added at the specific point where you want to start using them. The versions listed are those used for the output shown in the book; it is expected that later versions will work as well, though the output produced may be slightly different if you use a newer version.

Who this book is for

If you are a Django application developer who wants to create robust applications quickly that work well and are easy to maintain in the long term, this book is for you. This book is the right pick if you want to be smartly tutored to make best use of Django's rich testing and debugging support and make development an effortless task.

Basic knowledge of Python, Django, and the overall structure of a database-driven web application is assumed. However, the code samples are fully explained so that even beginners who are new to the area can learn a great deal from this book. If you are new to Django, it is recommended that you work through the online Django tutorial before beginning this book.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Note

Downloading the example code for the book

Visit http://www.packtpub.com/files/code/7566_Code.zip to directly download the example code.

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

Chapter 1. Django Testing Overview

How do you know when code you have written is working as intended? Well, you test it. But how? For a web application, you can test the code by manually bringing up the pages of your application in a web browser and verifying that they are correct. This involves more than a quick glance to see whether they have the correct content, as you must also ensure, for example, that all the links work and that any forms work properly. As you can imagine, this sort of manual testing quickly becomes impossible to rely on as an application grows beyond a few simple pages. For any non-trivial application, automated testing is essential.

Automated testing of Django applications makes use of the fundamental test support built-in to the Python language: doctests and unit tests. When you create a new Django application with manage.py startapp, one of the generated files contains a sample doctest and unit test, intended to jump-start your own test writing. In this chapter, we will begin our study of testing Django applications. Specifically, we will:

Examine in detail the contents of the sample tests.py file, reviewing the fundamentals of Python's test support as we do soSee how to use Django utilities to run the tests contained in tests.pyLearn how to interpret the output of the tests, both when the tests succeed and when they failReview the effects of the various command-line options that can be used when testing

Getting started: Creating a new application

Let's get started by creating a new Django project and application. Just so we have something consistent to work with throughout this book, let's assume we are setting out to create a new market-research type website. At this point, we don't need to decide much about this site except some names for the Django project and at least one application that it will include. As market_research is a bit long, let's shorten that to marketr for the project name. We can use django-admin.py to create a new Django project:

kmt@lbox:/dj_projects$ django-admin.py startproject marketr

Then, from within the new marketr directory, we can create a new Django application using the manage.py utility. One of the core applications for our market research project will be a survey application, so we will start by creating it:

kmt@lbox:/dj_projects/marketr$ python manage.py startapp survey

Now we have the basic skeleton of a Django project and application: a settings.py file, a urls.py file, the manage.py utility, and a survey directory containing .py files for models, views, and tests. There is nothing of substance placed in the auto-generated models and views files, but in the tests.py file there are two sample tests: one unit test and one doctest. We will examine each in detail next.

Understanding the sample unit test

The unit test is the first test contained in tests.py, which begins:

""" This file demonstrates two different styles of tests (one doctest and one unittest). These will both pass when you run "manage.py test". Replace these with more appropriate tests for your application. """ from django.test import TestCase class SimpleTest(TestCase): def test_basic_addition(self): """ Tests that 1 + 1 always equals 2. """ self.failUnlessEqual(1 + 1, 2)

The unit test starts by importing TestCase from django.test. The django.test.TestCase class is based on Python's unittest.TestCase, so it provides everything from the underlying Python unittest.TestCase plus features useful for testing Django applications. These Django extensions to unittest.TestCase will be covered in detail in Chapter 3, Testing 1, 2, 3: Basic Unit Testing and Chapter 4, Getting Fancier: Django Unit Test Extensions. The sample unit test here doesn't actually need any of that support, but it does not hurt to base the sample test case on the Django class anyway.

The sample unit test then declares aSimpleTest class based on Django's TestCase, and defines a test method named test_basic_addition within that class. That method contains a single statement:

self.failUnlessEqual(1 + 1, 2)

As you might expect, that statement will cause the test case to report a failure unless the two provided arguments are equal. As coded, we'd expect that test to succeed. We'll verify that later in this chapter, when we get to actually running the tests. But first, let's take a closer look at the sample doctest.

Summary

The overview of Django testing is now complete. In this chapter, we:

Looked in detail at the sample tests.py file generated when a new Django application is createdLearned how to run the provided sample testsExperimented with introducing deliberate mistakes into the tests in order to see and understand what information is provided when tests fail or encounter errorsFinally, we examined all of the command line options that may be used with manage.py test

We will continue to build on this knowledge in the next chapter, as we focus on doctests in depth.