在Django中以Agile / BDD方式使用Doctests的示例 [英] Examples of using Doctests in Django in an Agile / BDD way

查看:231
本文介绍了在Django中以Agile / BDD方式使用Doctests的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣以更敏捷/ BDD的方式学习如何进行学习和单元测试。
我发现一些看起来很合理的教程,但它们只是缩略图。
我真正希望看到的是开发BDD样式的一些Django项目的源代码。

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.

我不清楚的事情是怎么做的您处理请求对象等
我有一个情况,我已经部署了我的应用程序,我在生产中完全不同的行为,我在开发中甚至从生产服务器上的Python shell。我希望一些Doctests会帮助我诊断这个,打开一个更敏捷的编写测试过程的门。

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}) 

如何创建一个Doctest以确保索引返回10个对象?

产品查询似乎在生产服务器上的shell中正常工作。实际的服务器根本没有返回任何产品。

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.

推荐答案

我以前问过自己的问题。我发现,对于像观点,模型方法和管理者这样的东西,doctests是有限的,因为

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. 你需要能够设置和拆分实际用于测试的测试数据集

  2. 视图需要获取请求对象。在一个doctest,哪里来自哪里?

因此,我一直使用Django 单元测试框架,为您处理所有这些。不幸的是,尽管如此,您不会得到这些措施的一些好处,这使得TDD / BDD更难做到。接下来的内容是关于如何实现这一目标的纯粹的猜测

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:

我想你想要从各自的模块中获取doctests,功能并在单元测试框架内执行它们。这将照顾测试数据设置/拆卸。如果你的doctests是从Django的unittest.TestCase子类的测试方法中执行的,那么他们就可以使用该测试数据库。您还可以将模拟请求对象传递到doc测试的执行上下文中。以下是 Django snippet ,它提供了一个模拟请求对象, info 上。假设您想从所有应用程序视图中测试docstrings。您可以在tests.py中执行此操作:

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)

这个应该允许你这样做:

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.

这篇关于在Django中以Agile / BDD方式使用Doctests的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆