#3051 closed defect (fixed)
django.test.client.Client() does not set HttpResponse context and template properly
Reported by: | anonymous | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | normal | Keywords: | |
Cc: | django@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Using a view function that relies upon render_to_response(template, context), I run a doctest over the view function utilizing the django.test.client.Client class, however when I get my response object back, the context and template attributes are None.
I thought it might be specific to the doctest, however running the test by hand in a python shell yields the same result.
My view:
# URL re is (r'^(?P<mp_slug>\w+)/(?P<product_class_slug_string>[^_][a-zA-Z0-9_/]+)/_(?P<product_slug>\w+)/$', 'product_detail'), def product_detail(request, mp_slug, product_class_slug_string, product_slug): manipulator = marketplace.ShoppingCartItem.AddManipulator() mp = get_object_or_404(marketplace.Marketplace, slug__exact=mp_slug) product = get_object_or_404(marketplace.Product, slug__exact=product_slug, product_class__marketplace__id__exact=mp.id) product_class_slug_list = product_class_slug_string.split('/') assert product.product_class.marketplace == mp assert product_class_slug_list == map(lambda pc: pc.slug, product.product_class.get_ancestry()) product_class = product.product_class data_copy = {} if request.method == 'POST': data_copy = request.POST.copy() shopping_cart = marketplace.ShoppingCart.objects.get_cart(mp, request.session) errors = manipulator.get_validation_errors(data_copy) if not errors: manipulator.do_html2python(data_copy) shopping_cart.add_item_to_cart(product, data_copy['quantity'], data_copy['size_option']) return HttpResponseRedirect(shopping_cart.get_absolute_url()) else: errors = {} form = forms.FormWrapper(manipulator, data_copy, errors) return render_to_response('product_detail.djt', {'form': form, 'marketplace': mp, 'product': product, 'product_class': product_class, }) >>> from fsforg.marketplace import models as marketplace >>> from django.test.client import Client >>> mp = marketplace.Marketplace(name='Test MP', slug='test_mp') >>> mp.save() >>> stuff = marketplace.ProductClass(name='Stuff', slug='stuff', parent=None, marketplace=mp) >>> stuff.save() >>> thing = marketplace.Product(name='Thing', slug='thing', short_desc='Thing', long_desc='Thing', ... unit_price=123.45, photo_url='http://www.gnu.org/graphics/gnu-head-banner.png', ... product_class=stuff, weight=0.0) >>> >>> thing.save() >>> thing.get_absolute_url() '/marketplaces/test_mp/stuff/_thing/' >>> c = Client() >>> response = c.get(thing.get_absolute_url()) >>> response.status_code 200 >>> response.context >>> response.template
Attachments (2)
Change History (15)
comment:1 by , 18 years ago
Cc: | added |
---|
comment:2 by , 18 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
The test instrumentation that gathers template/context details is only installed if (1) you run your doctests through manage.py, or (2) you run django.test.utils.setup_test_environment(). Your sample python shell doesn't call the setup method. If you aren't getting the template/context details in a doctest, I'm guessing that you aren't doing (1), either.
I'm marking this as invalid; if you still get this problem when you use manage.py and/or call setup_test_environment(), feel free to reopen the ticket.
comment:3 by , 16 years ago
Needs documentation: | set |
---|---|
Resolution: | invalid |
Status: | closed → reopened |
I've just come across this same problem, and spent an hour or so scratching my head. .As russellm says it is easy to fix by calling django.test.utils.setup_test_environment() first.
However, this is not really mentioned in the documentation at all. The function is mentioned at:
But it isn't in anyway clear that you have to call it for certain things to work, and none of the example code calls it.
Surely -at the very least, the documentation/examples need to make this clear.
comment:4 by , 15 years ago
Component: | Testing framework → Documentation |
---|---|
Triage Stage: | Unreviewed → Accepted |
by , 15 years ago
Attachment: | testing_skipping_manage_py.diff added |
---|
Added a note about setup_test_environment()
comment:5 by , 15 years ago
milestone: | → 1.2 |
---|---|
Needs documentation: | unset |
comment:6 by , 15 years ago
Has patch: | set |
---|
comment:7 by , 15 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Patch by Rupe also applies to 1.1.X branch with -37 line offset level 2 fuzz.
comment:8 by , 15 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → Accepted |
I don't feel like this patch is ready. While the section within testing.txt is correct it lacks much context. This patch needs to do a better job of explaining where setup_test_environment
is required. Provide reference links for setup_test_environment
as well.
by , 14 years ago
Attachment: | 3051_testing.diff added |
---|
Better patch with more explanation and links to reference.
comment:10 by , 14 years ago
Patch needs improvement: | unset |
---|
Patch updated with more explanation and links.
comment:11 by , 14 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Patch looks good and applies cleanly.
comment:12 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
Sorry -- somehow failed to put my email address....