我有兴趣学习如何以更敏捷 / 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 正常工作。实际的服务器根本不返回任何产品。