简体   繁体   English

如何对 jinja2 进行单元测试?

[英]How to unittest jinja2?

I've been using webtest for unit testing, but that only takes me so far when using templating with google app engine.我一直在使用webtest进行单元测试,但是当使用 google app engine 模板时,这只需要我到目前为止。

The only related info I found was how to unittest the template variables passed to jinja2 template from webapp2 request handler我发现的唯一相关信息是如何对从 webapp2 请求处理程序传递给 jinja2 模板的模板变量进行单元测试

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?有没有一种不用打印响应就可以检查模板值的好方法?

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.或者, 这篇文章提供了有关在不模拟 Jinja2 的情况下使其工作的信息。 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).然后您可以使用HTMLParser在 HTML 响应中查找template_values (为它们的 html 元素提供唯一的 ID 以使其更容易)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM