Opened 18 years ago

Closed 14 years ago

Last modified 14 years ago

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

testing_skipping_manage_py.diff (611 bytes ) - added by Joshua Russo 15 years ago.
Added a note about setup_test_environment()
3051_testing.diff (976 bytes ) - added by Daniel Roseman 14 years ago.
Better patch with more explanation and links to reference.

Download all attachments as: .zip

Change History (15)

comment:1 by anonymous, 18 years ago

Cc: django@… added

Sorry -- somehow failed to put my email address....

comment:2 by Russell Keith-Magee, 18 years ago

Resolution: invalid
Status: newclosed

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 adamtwiss, 16 years ago

Needs documentation: set
Resolution: invalid
Status: closedreopened

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:

http://docs.findjango.com/topics/testing.html

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 Alex Gaynor, 15 years ago

Component: Testing frameworkDocumentation
Triage Stage: UnreviewedAccepted

by Joshua Russo, 15 years ago

Added a note about setup_test_environment()

comment:5 by Joshua Russo, 15 years ago

milestone: 1.2
Needs documentation: unset

comment:6 by Joshua Russo, 15 years ago

Has patch: set

comment:7 by Ramiro Morales, 15 years ago

Triage Stage: AcceptedReady for checkin

Patch by Rupe also applies to 1.1.X branch with -37 line offset level 2 fuzz.

comment:8 by Brian Rosner, 15 years ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

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.

comment:9 by Russell Keith-Magee, 15 years ago

milestone: 1.2

Deferring due to the absence of a trunk-ready patch.

by Daniel Roseman, 14 years ago

Attachment: 3051_testing.diff added

Better patch with more explanation and links to reference.

comment:10 by Daniel Roseman, 14 years ago

Patch needs improvement: unset

Patch updated with more explanation and links.

comment:11 by Łukasz Rekucki, 14 years ago

Triage Stage: AcceptedReady for checkin

Patch looks good and applies cleanly.

comment:12 by Russell Keith-Magee, 14 years ago

Resolution: fixed
Status: reopenedclosed

(In [13626]) Fixed #3051 -- Documented the requirements for standalone testing. Thanks to Daniel Roseman for the draft text.

comment:13 by Russell Keith-Magee, 14 years ago

(In [13627]) [1.2.X] Fixed #3051 -- Documented the requirements for standalone testing. Thanks to Daniel Roseman for the draft text.

Backport of r13626 from trunk.

Note: See TracTickets for help on using tickets.
Back to Top