6
如何unittest jinja2?如何單元測試jinja2?谷歌應用程序引擎,蟒蛇
我一直在使用webtest進行單元測試,但只有當我使用谷歌應用程序引擎模板時,我纔到目前爲止。
唯一的相關信息,我發現how to unittest the template variables passed to jinja2 template from webapp2 request handler
然而,我發現很難消化。
網絡上也沒有這方面的信息。
我的代碼在我的處理程序之一的片段是:
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))
是否有檢查模板值,而不只是打印出響應的好方法?
正如在其他答案中提到的,它需要使用模擬BaseHandler.render_template來檢查模板值而不是打印HTML響應。 https://pypi.python.org/pypi/mock是現在在Python 3中默認的框架(你可以通過Python在Python 2中使用它)。不幸的是,我現在沒有一個完整的例子,但我可能會稍後更新這個問題。 – Adam