O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Django 1.1 Testing and Debugging

Book Description

Building rigorously tested and bug-free Django applications

  • Develop Django applications quickly with fewer bugs through effective use of automated testing and debugging tools.

  • Ensure your code is accurate and stable throughout development and production by using Django's test framework.

  • Understand the working of code and its generated output with the help of debugging tools.

  • Packed with detailed working examples that illustrate the techniques and tools for debugging

  • In Detail

    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.

    A step-by-step guide to running tests using Django's test support and making best use of Django and Python debugging tools

    Table of Contents

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