English 中文(简体)
How can I access response.context when testing a Jinja2 powered Django view
原标题:

When I use the Django test.client and I do something like:

class MyTestCase(TestCase):
    def test_this(self):
        c = self.client
        response = c.get( / )
        assert False, response.context[ name ]

I get an error:

assert False, response.context[ name ]
TypeError:  NoneType  object is unsubscriptable

My only guess is something with using Jinja2 is preventing the context from showing up when I test.

Note this test is intentionally rigged to fail.

问题回答

I ve been meaning to readup on TestCase. After perusing the docs it looks you might have an error. Assertions are methods of the TestCase class.

class MyTestCase(TestCase):
  def test_this(self):
    response=self.client.get( / )
    self.assertEquals(response.context[ name ], Jim ) 

Django s monkey patches the Template class overriding the render method to be able to send the template_rendered signal and populate response.context.

If you dig the code you will be able to do this for Jinja2 s Template class.

I ve done what @Rho has suggested this way (in the beginning of the page load tests file)

from jinja2 import Template as Jinja2Template
from django.test import signals

#note - this code can be run only once
ORIGINAL_JINJA2_RENDERER = Jinja2Template.render
def instrumented_render(template_object, *args, **kwargs):
    context = dict(*args, **kwargs)
    signals.template_rendered.send(
                            sender=template_object,
                            template=template_object,
                            context=context
                        )
    return ORIGINAL_JINJA2_RENDERER(template_object, *args, **kwargs)
Jinja2Template.render = instrumented_render

Then you can pick out the response context and template name (however response.template is not a list in this case) and instead of response.template[0].name you ll need to use response.template.name.

Jinja sets context_data variable, not context:

response = client.get( / )
print response.context_data




相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...

热门标签