7

I've been using webtest for unit testing, but that only takes me so far when using templating with google app engine.

The only related info I found was how to unittest the template variables passed to jinja2 template from webapp2 request handler

However, I found it hard to digest.

The web lacks info on this as well.

A snippet of my code in one of my handlers is:

template_values = {
        "message": "Ello Mate",
        "errors": self.error_message,
        "stats" : self.statuses
    }
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.out.write(template.render(template_values))

Is there a good way to check the template values without just printing out the response?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • As mentioned in the other answer it's required to use a mock for BaseHandler.render_template to check the template values instead of printing the HTML response. https://pypi.python.org/pypi/mock is the framework which is now the default in Python 3 (you can use this in Python 2 via pip). Unfortunately I don't have a complete example right now but I may update this question later. – Adam Jan 12 '16 at 00:27

1 Answers1

0

Adam is right; you could mock it, and that might suit your needs perfectly. Alternatively, this post has information on getting Jinja2 working without mocking it. Then you could use HTMLParser to look for the template_values in the HTML response (give their html elements unique id's to make that easier).

Community
  • 1
  • 1
Brendan Goggin
  • 2,061
  • 14
  • 14