Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 502417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:17:21+00:00 2026-05-13T06:17:21+00:00

I’m interested in learning how to Doctests and Unit tests in a more Agile

  • 0

I’m interested in learning how to Doctests and Unit tests in a more Agile / BDD way.
I’ve found a few tutorials that seem reasonable, but they are just thumbnails.
What I would really like to see is the source code of some Django projects that were developed BDD style.

The things I’m unclear about are how do you handle request objects etc.
I have a situation where I have deployed my app and I’m getting completely different behavior in production that I did in development or even from the Python shell on the production server. I’m hoping some Doctests will help me diagnose this and well as open the door for a more Agile process of writing the tests first.

Specifically, here is the code I’m trying to test:

def match_pictures_with_products( queryset, number_of_images = 3):      
    products = []  
    i = 0    
    for product in queryset:  
       if i < ( number_of_images ):  
           image =  product.imagemain_set.all()[:1]  
           product.photo_url = image[0].photo.url  

       products.append(product)  
       i += 1  

    return products  

def index(request):  
    """returns the top 10 most clicked products"""     
    products = Product.objects.all()[:10]  
    products = match_pictures_with_products( products, 10)  .  
    return render_to_response('products/product_list.html', {'products': products}) 

How do I create a Doctest that ensures that index returns 10 objects?
The Product queries seem to work fine from the shell on the production server. The actual server is not returning any products at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:17:21+00:00Added an answer on May 13, 2026 at 6:17 am

    I’ve asked myself the same question before. I’ve found doctests to be of limited utility for things like views, model methods and managers because

    1. You need to be able to setup and teardown a test data set to actually use for testing
    2. Views need to take a request object. In a doctest, where does that come from?

    For that reason, I’ve always used the Django unit testing framework which handles all this for you. Unfortunately, though, you don’t get some of the benefits of the doctests and it makes TDD/BDD harder to do. What follows next is pure speculation about how you might make this work:

    I think you’d want to grab doctests from their respective modules and functions and execute them within the unit testing framework. This would take care of test data setup/teardown. If your doctests were executed from within a test method of something that subclasses Django’s unittest.TestCase they’d be able to use that test DB. You’d also be able to pass a mock request object into the doc test’s execution context. Here’s a Django snippet that provides a mock request object and info on it. Let’s say you wanted to test the docstrings from all of an applications views. You could do something like this in tests.py :

    from ??? import RequestFactory
    from doctest import testmod, DocTestFailure
    from django.test import TestCase
    
    from myapp import views
    
    class MyAppTest(TestCase):
    
        fixtures = ['test_data.json']
    
        def test_doctests(self):                
            try:
                testmod(views, extraglobs={
                    'REQUEST': RequestFactory()
                }, raise_on_error=True)
            except DocTestFailure, e:
                self.fail(e)
    

    This should allow you to do something like this:

    def index(request):  
        """
        returns the top 10 most clicked products
    
        >>> response = index(REQUEST)
        >>> [test response content here]
    
        """     
        products = Product.objects.all()[:10]  
        products = match_pictures_with_products( products, 10)  .  
        return render_to_response('products/product_list.html', {'products': products})
    

    Again, this is just off the top of my head and not at all tested, but it’s the only way that I think you could what you want without just putting all your view tests in the unit testing framework.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.