Unittest Jinja2和Webapp2:找不到模板 [英] Unittest Jinja2 and Webapp2 : template not found
问题描述
我有一个基 RequestHandler
,如 webapp2_extras.jinja2
:
import webapp2
from webapp2_extras import jinja2
$ b def jinja2_factory(app):
为Jinja设置配置环境。
config = {my config ... }
j = jinja2.Jinja2(app,config = config)
return j
class BaseHandler(webapp2.RequestHandler):
@ webapp2.cached_property
def jinja2(self):
#返回缓存在应用程序注册表中的Jinja2渲染器。
return jinja2.get_jinja2(factory = jinja2_factory,app = self.app)
$ b $ def render_response(self,_template,** context):
#渲染一个模板并写入导致回应。
rv = self.jinja2.render_template(_template,** context)
self.response.write(rv)
以及一个视图处理程序:
class MyHandler(BaseHandler):
def get(self):
context = {'message':'Hello,world!'}
self.render_response('my_template.html',** context)
我的模板位于默认位置( templates
)。
该应用在dev服务器上运行良好,模板正确呈现。
但是当我尝试单元测试 MyHandler
with
import unittest
import webapp2
import webstest
$ b $ class MyHandlerTest(unittest.TestCase):
$ b $ def setUp(self):
application = webapp2.WSGIApplication([('/',MyHandler)])
self.testapp = webtest.TestApp(应用程序)
def test_response(self):
response = application.get_response('/')
...
application.get_response('/ my-view')
引发一个异常: TemplateNotFound:my_template.html
。
有什么我错过了吗?像jinja2环境或模板加载器配置?
问题来源:
Jinja2 default加载器在相对 ./ templates /
目录中搜索文件。在开发服务器上运行GAE应用程序时,此路径与应用程序的根目录相关。但是当你运行你的unittests这个路径是相对于你的单元测试文件。
解决方案:
不是一个理想的解决方案,但这里是我解决我的问题的一个技巧。
我更新了jinja2工厂以添加一个动态模板路径,并在app配置中设置:
def jinja2_factory(app):
为Jinja设置配置环境。
config = {'template_path':app .config.get('templates_path','templates'),}
j = jinja2.Jinja2(app,config = config)
return j
我在我的unittest的setUp中设置了模板的绝对路径:
class MyHandlerTest(unittest.TestCase):
$ b $ def setUp(self):
#设置加载程序的模板路径
start = os.path.dirname(__ file__ )
rel_path = os.path.join(start,'../../templates')#我的模板路径
abs_path = os.path.realpath(rel _path)
application.config.update({'templates_path':abs_path})
I use Jinja2 with Webapp2 on a GAE project.
I have a base RequestHandler
as describe in webapp2_extras.jinja2
:
import webapp2
from webapp2_extras import jinja2
def jinja2_factory(app):
"""Set configuration environment for Jinja."""
config = {my config...}
j = jinja2.Jinja2(app, config=config)
return j
class BaseHandler(webapp2.RequestHandler):
@webapp2.cached_property
def jinja2(self):
# Returns a Jinja2 renderer cached in the app registry.
return jinja2.get_jinja2(factory=jinja2_factory, app=self.app)
def render_response(self, _template, **context):
# Renders a template and writes the result to the response.
rv = self.jinja2.render_template(_template, **context)
self.response.write(rv)
And a view handler as:
class MyHandler(BaseHandler):
def get(self):
context = {'message': 'Hello, world!'}
self.render_response('my_template.html', **context)
My templates are in the default location (templates
).
The app works well on dev server, and the template is correctly rendered.
But when I try to unittest MyHandler
with
import unittest
import webapp2
import webstest
class MyHandlerTest(unittest.TestCase):
def setUp(self):
application = webapp2.WSGIApplication([('/', MyHandler)])
self.testapp = webtest.TestApp(application)
def test_response(self):
response = application.get_response('/')
...
application.get_response('/my-view')
raise an exception: TemplateNotFound: my_template.html
.
Is there something I missed? Like a jinja2 environment or template loader configuration?
Problem origin:
Jinja2 default loader searches files in a relative ./templates/
directory. When you run your GAE application on the development server this path is relative to the root of your application. But when you run your unittests this path is relative to your unittest files.
Solution: Not really an ideal solution, but here a trick I did to solve my problem.
I updated the jinja2 factory to add a dynamic template path, set in app config:
def jinja2_factory(app):
"""Set configuration environment for Jinja."""
config = {'template_path': app.config.get('templates_path', 'templates'),}
j = jinja2.Jinja2(app, config=config)
return j
And I set an absolute path to the templates in the setUp of my unittests:
class MyHandlerTest(unittest.TestCase):
def setUp(self):
# Set template path for loader
start = os.path.dirname(__file__)
rel_path = os.path.join(start, '../../templates') # Path to my template
abs_path = os.path.realpath(rel_path)
application.config.update({'templates_path': abs_path})
这篇关于Unittest Jinja2和Webapp2:找不到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!