pytest.raises(UndefinedError, t.render, var=0)
def test_none_gives_proper_error(self):
- try:
+ with pytest.raises(UndefinedError, match= "'None' has no attribute 'split'"):
Environment().getattr(None, 'split')()
- except UndefinedError as e:
- assert e.message == "'None' has no attribute 'split'"
- else:
- assert False, 'expected exception'
def test_object_repr(self):
- try:
+ with pytest.raises(UndefinedError, match="'int object' has no attribute 'upper'"):
Undefined(obj=42, name='upper')()
- except UndefinedError as e:
- assert e.message == "'int object' has no attribute 'upper'"
- else:
- assert False, 'expected exception'
@pytest.mark.api
t = test_env_async.from_string('{% include ["missing", "missing2"] %}')
pytest.raises(TemplateNotFound, t.render)
- try:
+ with pytest.raises(TemplatesNotFound) as e:
t.render()
- except TemplatesNotFound as e:
- assert e.templates == ['missing', 'missing2']
- assert e.name == 'missing2'
- else:
- assert False, 'thou shalt raise'
+
+ assert e.value.templates == ['missing', 'missing2']
+ assert e.value.name == 'missing2'
def test_includes(t, **ctx):
ctx['foo'] = 42
t = test_env.from_string('{% include ["missing", "missing2"] %}')
pytest.raises(TemplateNotFound, t.render)
- try:
+ with pytest.raises(TemplatesNotFound) as e:
t.render()
- except TemplatesNotFound as e:
- assert e.templates == ['missing', 'missing2']
- assert e.name == 'missing2'
- else:
- assert False, 'thou shalt raise'
+
+ assert e.value.templates == ['missing', 'missing2']
+ assert e.value.name == 'missing2'
def test_includes(t, **ctx):
ctx['foo'] = 42
"""Ensures that a template with more than 1 {% extends ... %} usage
raises a ``TemplateError``.
"""
- try:
+ with pytest.raises(TemplateError):
tmpl = env.get_template('doublee')
- except Exception as e:
- assert isinstance(e, TemplateError)
def test_error_messages(self, env):
def assert_error(code, expected):
- try:
+ with pytest.raises(TemplateSyntaxError, match=expected):
Template(code)
- except TemplateSyntaxError as e:
- assert str(e) == expected, 'unexpected error message'
- else:
- assert False, 'that was supposed to be an error'
assert_error('{% for item in seq %}...{% endif %}',
"Encountered unknown tag 'endif'. Jinja was looking "
env = Environment(loader=PrefixLoader({
'foo': DictLoader({})
}))
- try:
+ with pytest.raises(TemplateNotFound) as e:
env.get_template('foo/bar.html')
- except TemplateNotFound as e:
- assert e.name == 'foo/bar.html'
- else:
- assert False, 'expected error here'
+
+ assert e.value.name == 'foo/bar.html'
def test_contextfunction_callable_classes(self, env):
from jinja2.utils import contextfunction