Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Django 1.1 Testing and Debugging
Django 1.1 Testing and Debugging

Django 1.1 Testing and Debugging: Building rigorously tested and bug-free Django applications

eBook
NZ$57.99 NZ$64.99
Paperback
NZ$80.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Django 1.1 Testing and Debugging

Chapter 2. Does This Code Work? Doctests in Depth

In the first chapter, we learned how to run the sample tests created by manage.py startapp. Although we used a Django utility to run the tests, there was nothing specific to Django about the sample tests themselves. In this chapter, we will start getting into details of how to write tests for a Django application. We will:

  • Begin writing the market research project created in the first chapter by developing some basic models that will be used by the project

  • Experiment with adding doctests to one of the models

  • Begin to learn the kinds of tests that are useful, and the kinds that just add clutter to the code

  • Discover some of the advantages and disadvantages of doctests

While the previous chapter mentioned both doctests and unit tests, the focus for this chapter will be on doctests exclusively. Developing unit tests for Django applications will be the focus of Chapter 3, Testing 1, 2, 3: Basic Unit Testing and Chapter 4, Getting Fancier: Django Unit...

The Survey application models


A common place to start development of a new Django application is with the models: the basic building blocks of data that are going to be manipulated and stored by the application. A cornerstone model for our example market research survey application will be the Survey model.

A Survey is going to be similar to the Django tutorial Poll model, except that:

  • Where the tutorial Poll only contains one question, a Survey will have multiple questions.

  • A Survey will have a title for reference purposes. For the tutorial Poll, a single question could be used for this.

  • A Survey will only be open for responses for a limited (and variable, depending on the Survey instance) time. While the Poll model has a pub_date field, it is not used for anything other than ordering Polls on the index page. Thus, Survey will need two date fields where Poll has only one, and the Survey date fields will be used more than the Poll pub_date field is used.

Given just these few simple requirements...

Testing the Survey model


If you are at all like me, at this point you might want to start verifying that what you've got so far is correct. True, there is not much code yet, but particularly when just starting out on a project I like to make sure, early and often, that what I've got so far is valid. So, how do we start testing at this point? First, we can verify that we've got no syntax errors by running manage.py syncdb, which will also let us start experimenting with these models in a Python shell. Let's do that. Since this is the first time we've run syncdb for this project, we'll get messages about creating tables for the other applications listed in INSTALLED_APPS, and we'll be asked if we want to create a superuser, which we may as well go ahead and do also.

Testing Survey model creation

Now, what might we do with these models to test them in a Python shell? Really, not much beyond creating each, perhaps verifying that if we don't specify one of the fields we get an error, or the correct...

Additional doctest caveats


Doctests have some additional disadvantages that we haven't necessarily run into or noticed yet. Some of these are just things we need to watch out for if we want to make sure our doctests will work properly in a wide variety of environments and as code surrounding our code changes. Others are more serious issues that are most easily solved by switching to unit tests instead of doctests for at least the affected tests. In this section, we will list many of the additional doctest issues to watch out for, and give guidance on what to do to avoid or overcome them.

Beware of environmental dependence

It is very easy for doctests to be unintentionally dependent on implementation details of code other than the code that is actually being tested. We have some of this already in the save override tests, though we have not tripped over it yet. The dependence we have is actually a very specific form of environmental dependence—database dependence. As database dependence is...

Summary


Our exploration of doctests for Django applications is now complete. In this chapter, we:

  • Began to develop some models for our Django survey application

  • Experimented with adding doctests to one of these models—the Survey model

  • Learned what sorts of doctests are useful and which simply add clutter to the code

  • Experienced some of the advantages of doctests, namely the easy re-use of Python shell session work and convenient use of doctests as documentation

  • Ran afoul of many of the disadvantages of doctests, and learned how to avoid or overcome them

In the next chapter, we will begin to explore unit tests. While unit tests may not offer some of the easy re-use features of doctests, they also do not suffer from many of the disadvantages of doctests. Furthermore, the overall unit test framework allows Django to provide convenient support specifically useful for web applications, which will be covered in detail in Chapter 4.

Left arrow icon Right arrow icon

Key benefits

  • 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

Description

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.

Who is this book 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 testing 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.

What you will learn

  • Build a complete application in manageable pieces that can be written, tested, and debugged individually. Come to grips with the nuances of testing and the pros and cons of each type of test Simplify the task of testing web applications by using specific test extensions provided by Django. Integrate other test tools into Django s framework to obtain test coverage information and more easily test forms. Analyze the copious debug information provided by Django s debug error pages. Write your own add-on debugging aids. Easily acquire enormous and important information with the help of external tools such as the Django debug toolbar. Decipher code behavior by using logging and effectively debug problems in production, when debug error pages are not available.Learn what your code and other library support code actually does by skilled use of a debugger. Tackle problems external to your code with available fixes. Debug common problems that arise during the move from development to production.

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 19, 2010
Length: 436 pages
Edition : 1st
Language : English
ISBN-13 : 9781847197573
Vendor :
Django
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Apr 19, 2010
Length: 436 pages
Edition : 1st
Language : English
ISBN-13 : 9781847197573
Vendor :
Django
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total NZ$ 233.97
Django 1.1 Testing and Debugging
NZ$80.99
WiX: A Developer's Guide to Windows Installer XML
NZ$80.99
Programming ArcGIS 10.1 with Python Cookbook
NZ$71.99
Total NZ$ 233.97 Stars icon

Table of Contents

11 Chapters
Django Testing Overview Chevron down icon Chevron up icon
Does This Code Work? Doctests in Depth Chevron down icon Chevron up icon
Testing 1, 2, 3: Basic Unit Testing Chevron down icon Chevron up icon
Getting Fancier: Django Unit Test Extensions Chevron down icon Chevron up icon
Filling in the Blanks: Integrating Django and Other Test Tools Chevron down icon Chevron up icon
Django Debugging Overview Chevron down icon Chevron up icon
When the Wheels Fall Off: Understanding a Django Debug Page Chevron down icon Chevron up icon
When Problems Hide: Getting More Information Chevron down icon Chevron up icon
When You Don't Even Know What to Log: Using Debuggers Chevron down icon Chevron up icon
When All Else Fails: Getting Outside Help Chevron down icon Chevron up icon
When it's Time to Go Live: Moving to Production Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(11 Ratings)
5 star 63.6%
4 star 27.3%
3 star 0%
2 star 0%
1 star 9.1%
Filter icon Filter
Top Reviews

Filter reviews by




Chris Lawlor May 01, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Don't let the Django 1.1 label on the cover fool you, this is still the best book available on testing Django applications.
Amazon Verified review Amazon
J. Mccollum May 28, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is nothing if not ambitious. Weighing in at over 400 pages, it aims to highlight the development of an entire web app, start to finish, with a particular emphasis on the testing and debugging tools that Django provides.Starting with the fundamentals of doctests and unit tests, the book also discusses what should be tested - not just how. And in doing this, the book reveals its target audience. I would particularly recommend this book for the following groups of people: developers who are relatively new to Django, and developers who are new to MVC frameworks in general.The book then moves on to describe some of the tools you can use to extend Django's testing and debugging capabilities - the django-debug-toolbar, and Twill to name two.A detailed discussion of Django error pages comes next, before what was, for me, the highlight of the book: an examination of PDB - the Python Debugger. If, like me, your initial exposure to Python came through Django, then you might well have missed some of the gems that the standard library contains, such as PDB. The book contains a detailed walkthrough of how to use PDB, and if you haven't used it before, well, you'll love it.The book ends with a chapter on deployment, even including a section on load testing. Advanced topics such as testing threading issues are covered here too, ensuring that even seasoned Django developers will learn something from this book.The book's greatest strength is its breadth - covering the entire development process from start to finish. If there's one flaw though, it is that it goes into a little too much detail in places. Like Juho, I could have done without the section on reporting bugs in Django.You shouldn't let that put you off though - if the worst criticism I can muster is 'too much detail', that has to be a good thing! In particular, if you want to learn how to test your Django applications properly, or are new to Django and want to see the testing and debugging tools on offer, I would wholeheartedly recommend this book.
Amazon Verified review Amazon
Randall Degges Aug 15, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
First, a little about my background: I've been using Django for approximately 8 months, both professionally and for fun.This book really blew me away. I learned more new things in this book than I have reading the past 5 or so tech books alone. This book is extremely useful for any developers who would like to be better all around Django programmers, as it provides a solid, proven system for developing, testing, and debugging Django applications.The rest of my review will be spent breaking this book down for anyone interested.TARGET AUDIENCE---------------The target audience for this book are developers comfortable writing Django websites. This includes proficiency with:* models* forms* views* templates* settings (settings.py)* python modules* the concept of unit testing* the concept of 'agile development'If you are decent at any of the above, or can at least understand all of the terminology, and know WHY things work the way they do, then this book will be extremely useful to you.If you aren't really sure you know the topics above, consider reading another Django book before this one (but still buy this one).TOPICS COVERED--------------The book covers the following topics in-depth:* Running tests on Django websites / applications.* How to write useful doctests.* How to write useful unit tests.* Doctest dependence and creation / specification.* Unicode problems with doctests, and how to fix them.* Basic, intermediate, and advanced unit testing.* What to unit test, what not to worry about. (EG: Don't test native parts of Django, it is already tested.)* Environmental dependence in tests: database dependence, test interdependence, etc.* How to generate test fixtures.* How to use test fixtures.* Testing Django admin applications.* Testing emails.* Testing other HTTP methods.* Testing URL configurations.* Testing templates with Django's test client, and twil.* Using nose as a test runner.* How to understand Django's debug pages.* How to use PDB to do intensive low-level debugging of running applications.* How to perform performance / stress tests using siege.* Fixing multi-threaded coding errors.* Tracking SQL queries and optimizing SQL requests.* Using Django Debug Toolbar.* How to use the Django ticketing system.* How to deploy using mod_wsgi + apache for fast websites.* How to log code effectively using the native python logger.* How to get *real* coverage reports using django-coverage.OVERALL REVIEW--------------This book is by far the best book on agile development with Django that has been written. If it were up to me, I would require every programmer who uses Django to read this book before writing production code. It outlines the best practices for developing with Django (with a strong focus on testing), and WHY they are best practices.The author is an extremely good technical writer, and her focus and clarity is easily identifiable in the text. She is able to elegantly describe even the most complex thoughts, actions, and results with little reader friction.If you work with Django (for fun or profit), and haven't read this book, do yourself a favor and buy it. It will make a world of difference in your day-to-day work, and will make future programmers working on your code love you.It also covers details such as the best way to test models, how to test forms, etc., in a straightforward, easy to understand method.
Amazon Verified review Amazon
Marcel Sjohann Chastain Dec 29, 2012
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Just finished <Django 1.1 Testing and Debugging> by Karen M. Tracy.The chapter on using the Python debugger alone was worth the cost of the entire book. Even though it (obviously) doesn't include information about Django's newer testing features/integration, all the information is incredibly relevant.The author walks the reader through building a simple survey application, addressing best practices on debugging, testing, efficiency and resiliency along the way. It reads like a brisk but casual conversation with the author -- I feel almost surprised at the amount of technical knowledge conveyed so effortlessly. No words wasted, no pointless ramblings.Highly recommended (if you're into this kinda thing).
Amazon Verified review Amazon
Leon Matthews Feb 28, 2011
Full star icon Full star icon Full star icon Full star icon Full star icon 5
A fantastic introduction, and deep delve into Django test-driven development. Much of the material applies to plain Python programming as well. If I had to pick just one book for a new Django developer, it would be this one, no question.Only relatively recently has the Django community started to develop a good test-driven methodology, and I suspect this book might be part of the reason why -- it's a brilliant book.Don't let the slightly unfortunate title put you off, it's just as useful to users of Django 1.3 -- the only outdated advice are a couple of places where the author points out something that doesn't quite work yet, that now works in the latest versions of Django.There are relatively few nits. The visual presentation isn't great, and it can sometimes be a little difficult to follow along with the examples -- you have to make the same (deliberate) mistakes she does. Not knowing where the example was going, I had to back-track more than once and add the error I'd absentmindedly fixed!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.