【发布时间】:2009-12-15 13:51:49
【问题描述】:
我有兴趣学习如何以更敏捷/BDD 的方式进行 Doctests 和单元测试。 我发现了一些看似合理的教程,但它们只是缩略图。 我真正想看的是一些以BDD风格开发的Django项目的源代码。
我不清楚的是你如何处理请求对象等。 我有一种情况,我已经部署了我的应用程序,并且我在生产中得到了完全不同的行为,我在开发中甚至从生产服务器上的 Python shell 中得到了完全不同的行为。我希望一些 Doctests 能帮助我诊断这个问题,并为更敏捷的首先编写测试的过程打开大门。
具体来说,这是我要测试的代码:
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})
如何创建确保索引返回 10 个对象的 Doctest?
产品查询似乎可以从生产服务器上的 shell 正常工作。实际的服务器根本没有返回任何产品。
【问题讨论】:
-
如果你想要一个 BDD 工具,试试 lettuce。
标签: python django agile doctest